2017年1月30日 星期一

Linux 時間管理常用指令收集

1. date

[說明]

查看或設定時間。


[範例]

查看時間

cy@cyvm:~$ date
公曆 20十七年 一月 卅日 週一 十六時47分廿四秒

查看時間,顯示成英文格式

cy@cyvm:~$ LANG=en_US ; date
Mon Jan 30 16:53:18 CST 2017

cy@cyvm:~$ LANG=en ; date
Mon Jan 30 16:53:31 CST 2017

查看時間,格式化顯示時間

cy@cyvm:~$ date +"%Y-%m-%d %H:%M:%S"
2017-01-30 17:05:53
cy@cyvm:~$ date +"%s"
1485767170

  • 一般常用年月日時分秒組合數字格式
    %Ffull date; same as %Y-%m-%d
    %Ttime; same as %H:%M:%S
    %Yyear
    %mmonth (01..12)
    %dday of month (e.g., 01)
    %Hhour (00..23)
    %Mminute (00..59)
    %Ssecond (00..60)
    %Nnanoseconds (000000000..999999999)

  • 月份文字
    %Blocale's full month name (e.g., January)
    %blocale's abbreviated month name (e.g., Jan)
    %hsame as %b

  • 星期文字
    %Alocale's full weekday name (e.g., Sunday)
    %alocale's abbreviated weekday name (e.g., Sun)

  • 時區
    %z+hhmm numeric time zone (e.g., -0400)
    %:z+hh:mm numeric time zone (e.g., -04:00)
    %::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
    %:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
    %Zalphabetic time zone abbreviation (e.g., EDT)

  • UNIX Timestamp (Epoch time)
    %sseconds since 1970-01-01 00:00:00 UTC

  • 年或週的週期日數
    %jday of year (001..366)
    %uday of week (1..7); 1 is Monday

  • 週數
    %Uweek number of year, with Sunday as first day of week (00..53)
    %Wweek number of year, with Monday as first day of week (00..53)

  • ISO week number
    %Gyear of ISO week number (see %V); normally useful only with %V
    %glast two digits of year of ISO week number (see %G)
    %VISO week number, with Monday as first day of week (01..53)

  • 世紀
    %Ccentury; like %Y, except omit last two digits (e.g., 20)
    %ylast two digits of year (00..99)

  • 12小時制
    %Ihour (01..12)
    %plocale's equivalent of either AM or PM; blank if not known
    %Plike %p, but lower case

  • 地區限定格式
    %clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
    %xlocale's date representation (e.g., 12/31/99)
    %Xlocale's time representation (e.g., 23:13:48)
    %rlocale's 12-hour clock time (e.g., 11:11:04 PM)

  • 特殊文字
    %%a literal %
    %na newline
    %ta tab

  • 不常用或有其他替代方案的格式
    %Ddate; same as %m/%d/%y
    %R24-hour hour and minute; same as %H:%M
    %eday of month, space padded; same as %_d
    %khour, space padded ( 0..23); same as %_H
    %lhour, space padded ( 1..12); same as %_I
    %wday of week (0..6); 0 is Sunday


設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo date +"%Y-%m-%d %H:%M:%S" -s "2016-10-05 12:24:36"
2016-10-05 12:24:36



2. ntpdate

[說明]

經由 NTP (Network Time Protocol,RFC 1305),設定系統時間。


[範例]

設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo ntpdate -s time.stdtime.gov.tw



3. hwclock

[說明]

在 Linux 下操作時間,一般都只是寫入作業系統,若要同步寫入硬體(RTC, real-time clock),需要使用此指令。


[範例]

查看時間

cy@cyvm:~$ sudo hwclock -r
Mon Jan 30 05:35:39 2017  .262043 seconds

設定時間

cy@cyvm:~$ sudo hwclock -w

2017年1月24日 星期二

在 Ubuntu 上安裝 Samba Server

1. 安裝相關套件

sudo apt update
sudo apt upgrade
sudo apt install samba
sudo apt install libnss-winbind
其中,libnss-winbind 可讓 Linux 解析 Windows 的主機名。

2. 相關設定檔

2.1. smb.conf

/etc/samba/smb.conf

2.1.1. 一般使用者「家」目錄設定

[homes]
   comment = Home Directories
   browseable = yes
   read only  = no
   create mode    = 0600
   directory mode = 0700

2.2. nsswitch.conf

/etc/nsswitch.conf

2.2.1. 讓 Linux 解析 Windows 的主機名

hosts:          files mdns4_minimal [NOTFOUND=return] dns wins
在 hosts 後面多加一個 wins。

3. 資源管理

3.1. 新增使用者

sudo pdbedit -a -u user_name
其中,user_name為你要給定的使用者帳號名稱。
實作範例:
cy@cyvm:~$ sudo pdbedit -a -u cy
new password:
retype new password:
Unix username:        cy
NT username:
Account Flags:        [U          ]
User SID:             S-1-5-12-12345678-123456789-123456789-1234
Primary Group SID:    S-1-5-12-12345678-123456789-123456789-321
Full Name:            CY
Home Directory:       \\cyvm\cy
HomeDir Drive:
Logon Script:
Profile Path:         \\cyvm\cy\profile
Domain:               CYVM
Account desc:
Workstations:
Munged dial:
Logon time:           0
Logoff time:          三, 06  二  2036 23:06:39 CST
Kickoff time:         三, 06  二  2036 23:06:39 CST
Password last set:    二, 24  一  2017 23:46:36 CST
Password can change:  二, 24  一  2017 23:46:36 CST
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF


4. 測試

4.1. 測試 smb.conf 設定是否正確

testparm
實作範例:
cy@cyvm:~$ testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
WARNING: The "syslog" option is deprecated
Processing section "[homes]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
        server string = %h server (Samba, Ubuntu)
        server role = standalone server
        map to guest = Bad User
        obey pam restrictions = Yes
        pam password change = Yes
        passwd program = /usr/bin/passwd %u
        passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
        unix password sync = Yes
        syslog = 0
        log file = /var/log/samba/log.%m
        max log size = 1000
        dns proxy = No
        usershare allow guests = Yes
        panic action = /usr/share/samba/panic-action %d
        idmap config * : backend = tdb


[homes]
        comment = Home Directories
        read only = No
        create mask = 0600
        directory mask = 0700
        directory mode = 0700


5. 服務管理

systemctl status smbd.service
systemctl restart smbd.service
systemctl enable smbd.service


2017年1月23日 星期一

shell script 特殊技巧收集

查看目前所使用的 shell 程式

cy@cyvm:~$ echo $0
-bash

在 Ubuntu 安裝 Node.js

1. 安裝相關套件

Node.js 6.x

sudo apt-get update
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install nodejs
sudo apt-get install build-essential

官方參考網站: Debian and Ubuntu based Linux distributions



2. 相關設定檔

(N/A)



3. 資源管理

(N/A)



4. 測試

4.1. 查看 Node.js 版本

cy@cysrv:~$ node --version
v6.9.4

4.1. 查看 npm 版本

cy@cysrv:~$ npm --version
3.10.10

在 Debian 安裝 SSH Server

1. 安裝相關套件

sudo apt-get update
sudo apt-get install openssh-server


2. 相關設定檔

2.1. sshd_config

/etc/ssh/sshd_config

2.1.1. 安全性設定

# What ports, IPs and protocols we listen for
Port 12345

SSH 預設使用 port 22,建議更改,特別在面向網路網路使用時,幾乎只要一上網就會被攻擊。埠號可自定,不要與使用中的埠號即可。

PermitRootLogin prohibit-password

建議不要使用 root 登入 SSH,所以可將 PermitRootLogin 設為 no。但讓 root 能登入有時候滿方便的,特別是有時需要傳一些需要 root 權限的檔案時,可以省掉一些操作上的麻煩。所以這裡使用 prohibit-password,再加上使用免密碼登入方式,就可以讓 root 能登入 SSH,且能保持一定程度的安全性。

#AllowUsers cy # NOTE! this will disable root login

可限定只有某些使用者登入,值得注意的是,若沒有加入 root,將會使 root 不能登入,即使 PermitRootLogin 為 yes 或 prohibit-password 亦然。在我的 Server 並未實作此項,所以前面加 #。

2.1.2. 效能性設定

UseDNS no

加了這個據說能加速登入速度。



3. 資源管理

3.1. 最好不要使用的帳號名

以下為我曾被攻擊的帳號名,最好不要使用這些帳號,特別是 admin:

0
0000
1111
1234
123456
adam
adm
admin
agent
anonymous
apache
bill
cisco
client
customer
default
edu
ftp
ftpuser
guest
jack
john
manager
mark
master
mike
nagios
operator
oracle
osmc
pi
plcmspip
raspberry
sales
scan
service
steve
super
support
Taiwan
test
testuser
tiffany
ubnt
uploader
user
user1
usuario
vyatta
www


4. 測試

4.1. ssh 登入測試:

cy@cyvm:~$ ssh -p 12345 cy@192.168.0.1

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
No mail.
Last login: Sat Mar 18 11:25:52 2017 from 192.168.0.2

4.2. ssh 登入測試:

cykuo@cyvm:~$ scp -P 12345 -i ~/keys/cy_ssh_key test.file root@192.168.0.1:~/root_work_dir
test.file                     100%  273     0.3KB/s   00:00


5. 服務管理

安裝完會自動啟動服務,其餘服務常用操作指令如下所列。

systemctl status sshd.service
systemctl restart sshd.service
systemctl enable sshd.service