一 NTP時間服務器
1 局域網內的NTP同步配置
注意 所有配置操作必須是root用戶 ,局域網內node21作為NTP Server,node22,node23作為NTP Client與服務器進行時鍾同步,ntpd Server,用於和外部公共ntpd同步標准時間,同時作為內網的Server,ntpd Client,用於與ntpd Server同步時間
| node21 | 192.168.100.21 |
| node22 | 192.168.100.22 |
| node23 | 192.168.100.23 |
2 檢查ntp是否安裝
[root@node21 ~]# rpm -qa|grep ntp 如果已安裝顯示 ntp-4.2.6p5-10.el6.centos.x86_64 fontpackages-filesystem-1.41-1.1.el6.noarch ntpdate-4.2.6p5-10.el6.centos.x86_64
在線安裝設置開機啟動ntp服務
yum -y install ntp #啟動ntp服務 systemctl start ntpd #設置開機啟動服務 systemctl enable ntpd.service
3 防火牆配置
4 配置內網服務器
4.1 配置服務端
配置前先使用命令:ntpdate -u cn.pool.ntp.org,同步服務器
替換成中國時間服務器:http://www.pool.ntp.org/zone/cn
ntpd Server:192.168.100.21 修改/etc/ntp.conf文件,紅色字體是修改的內容
# For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 允許內網其他機器同步時間,如果不添加該約束默認允許所有IP訪問本機同步服務 restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst # 配置和上游標准時間同步 server 210.72.145.44 # 中國國家授時中心 server 133.100.11.8 #日本[福岡大學] server 0.cn.pool.ntp.org server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org # 配置允許上游時間服務器主動修改本機(內網ntp Server)的時間 restrict 210.72.145.44 nomodify notrap noquery restrict 133.100.11.8 nomodify notrap noquery restrict 0.cn.pool.ntp.org nomodify notrap noquery restrict 1.cn.pool.ntp.org nomodify notrap noquery restrict 2.cn.pool.ntp.org nomodify notrap noquery restrict 3.cn.pool.ntp.org nomodify notrap noquery # 確保localhost有足夠權限,使用沒有任何限制關鍵詞的語法。 # 外部時間服務器不可用時,以本地時間作為時間服務。 # 注意:這里不能改,必須使用127.127.1.0,否則會導致無法 #在ntp客戶端運行ntpdate serverIP,出現no server suitable for synchronization found的錯誤。 #在ntp客戶端用ntpdate –d serverIP查看,發現有“Server dropped: strata too high”的錯誤,並且顯示“stratum 16”。而正常情況下stratum這個值得范圍是“0~15”。 #這是因為NTP server還沒有和其自身或者它的server同步上。 #以下的定義是讓NTP Server和其自身保持同步,如果在ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp客戶端。 #下面這個配置,建議NTP Client關閉,建議NTP Server打開。因為Client如果打開,可能導致NTP自動選擇合適的最近的NTP Server、也就有可能選擇了LOCAL作為Server進行同步,而不與遠程Server進行同步。 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
4.2 重啟服務端ntpd服務:
[root@node21 ~]# systemctl restart ntpd
4.3查看NTP服務器
查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系:
[root@node21 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 210.72.145.44 .INIT. 16 u - 64 0 0.000 0.000 0.000 clock.tl.fukuok .INIT. 16 u - 64 0 0.000 0.000 0.000 gus.buptnet.edu 10.3.8.150 5 u 53 64 7 113.234 -29.011 5.500 cn.ntp.faelix.n 185.134.196.169 2 u 54 64 7 154.859 -67.243 18.652 static-5-103-13 .GPS. 1 u 53 64 7 406.416 -92.420 21.929 *LOCAL(0) .LOCL. 10 l 60 64 7 0.000 0.000 0.000
4.4 查看時間同步狀態
[root@node21 ~]# ntpstat synchronised to local net at stratum 11 time correct to within 7948 ms polling server every 64 s
[root@node21 ~]# ntpstat unsynchronised time server re-starting polling server every 64 s
[root@node21 ~]# ntpstat synchronised to NTP server (5.103.139.163) at stratum 2 time correct to within 456 ms polling server every 64 s
4.5 配置客戶端
node22,node23為客戶端。
1)檢查ntp是否安裝,以及是否設置了自啟動,參考ntpd Server的ntp安裝檢查。
2)修改/etc/ntp.conf文件,紅色字體是修改的內容:
# For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst #配置上游時間服務器為本地的ntpd Server服務器 server 192.168.100.21 # 配置允許上游時間服務器主動修改本機的時間 restrict 192.168.100.21 nomodify notrap noquery #下面這個配置,建議NTP Client關閉,建議NTP Server打開。因為Client如果打開,可能導致NTP自動選擇合適的最近的NTP Server、也就有可能選擇了LOCAL作為Server進行同步,而不與遠程Server進行同步。 #server 127.127.1.0 # local clock #fudge 127.127.1.0 stratum 10 #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
4.6 重啟客戶端ntpd服務
[root@node22 ~]# systemctl restart ntpd
4.7查看客戶端狀態
[root@node22 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== node21 LOCAL(0) 11 u 2 64 0 0.000 0.000 0.000
4.8 修改時區為北京時間
[root@node21 ~]# date Thu May 31 02:11:12 EDT 2018 [root@node21 ~]# date -R Thu, 31 May 2018 02:15:32 -0400 [root@node21 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime cp: overwrite ‘/etc/localtime’? y [root@node21 ~]# date -R Thu, 31 May 2018 14:16:42 +0800 [root@node21 ~]# date Thu May 31 14:16:47 CST 2018
二 定時任務Crontab命令詳解
linux 系統則是由 cron (crond) 這個系統服務來控制的。Linux 系統上面原本就有非常多的計划性工作,因此這個系統服務是默認啟動的。另 外, 由於使用者自己也可以設置計划任務,所以, Linux 系統也提供了使用者控制計划任務的命令 :crontab 命令。
1 crond簡介
crond 是linux下用來周期性的執行某種任務或等待處理某些事件的一個守護進程,與windows下的計划任務類似,當安裝完成操作系統后,默認會安裝此服務 工具,並且會自動啟動crond進程,crond進程每分鍾會定期檢查是否有要執行的任務,如果有要執行的任務,則自動執行該任務。
Linux下的任務調度分為兩類,系統任務調度和用戶任務調度。
系統任務調度:系統周期性所要執行的工作,比如寫緩存數據到硬盤、日志清理等。在/etc目錄下有一個crontab文件,這個就是系統任務調度的配置文件。
/etc/crontab文件包括下面幾行:
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=HOME=/
# run-parts
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
前 四行是用來配置crond任務運行的環境變量,第一行SHELL變量指定了系統要使用哪個shell,這里是bash,第二行PATH變量指定了系統執行 命令的路徑,第三行MAILTO變量指定了crond的任務執行信息將通過電子郵件發送給root用戶,如果MAILTO變量的值為空,則表示不發送任務 執行信息給用戶,第四行的HOME變量指定了在執行命令或者腳本時使用的主目錄。第六至九行表示的含義將在下個小節詳細講述。這里不在多說。
用戶任務調度:用戶定期要執行的工作,比如用戶數據備份、定時郵件提醒等。用戶可以使用 crontab 工具來定制自己的計划任務。所有用戶定義的crontab 文件都被保存在 /var/spool/cron目錄中。其文件名與用戶名一致。
使用者權限文件:
文件:
/etc/cron.deny
說明:
該文件中所列用戶不允許使用crontab命令
文件:
/etc/cron.allow
說明:
該文件中所列用戶允許使用crontab命令
文件:
/var/spool/cron/
說明:
所有用戶crontab文件存放的目錄,以用戶名命名
crontab文件的含義:
用戶所建立的crontab文件中,每一行都代表一項任務,每行的每個字段代表一項設置,它的格式共分為六個字段,前五段是時間設定段,第六段是要執行的命令段,格式如下:
minute hour day month week command
其中:
minute: 表示分鍾,可以是從0到59之間的任何整數。
hour:表示小時,可以是從0到23之間的任何整數。
day:表示日期,可以是從1到31之間的任何整數。
month:表示月份,可以是從1到12之間的任何整數。
week:表示星期幾,可以是從0到7之間的任何整數,這里的0或7代表星期日。
command:要執行的命令,可以是系統命令,也可以是自己編寫的腳本文件。

在以上各個字段中,還可以使用以下特殊字符:
星號(*):代表所有可能的值,例如month字段如果是星號,則表示在滿足其它字段的制約條件后每月都執行該命令操作。
逗號(,):可以用逗號隔開的值指定一個列表范圍,例如,“1,2,5,7,8,9”
中杠(-):可以用整數之間的中杠表示一個整數范圍,例如“2-6”表示“2,3,4,5,6”
正斜線(/):可以用正斜線指定時間的間隔頻率,例如“0-23/2”表示每兩小時執行一次。同時正斜線可以和星號一起使用,例如*/10,如果用在minute字段,表示每十分鍾執行一次。
2 crond服務
安裝crontab:
yum install crontabs
服務操作說明:
/sbin/service crond start //啟動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啟服務
/sbin/service crond reload //重新載入配置
/sbin/service crond status //啟動服務
查看crontab服務是否已設置為開機啟動,執行命令:
ntsysv
加入開機自動啟動:
chkconfig –level 35 crond on
3 crontab命令詳解
1.命令格式:
crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
2.命令功能:
通過crontab 命令,我們可以在固定的間隔時間執行指定的系統指令或 shell script腳本。時間間隔的單位可以是分鍾、小時、日、月、周及以上的任意組合。這個命令非常設合周期性的日志分析或數據備份等工作。
3.命令參數:
-u user:用來設定某個用戶的crontab服務,例如,“-u ixdba”表示設定ixdba用戶的crontab服務,此參數一般有root用戶來運行。
file:file是命令文件的名字,表示將file做為crontab的任務列表文件並載入crontab。如果在命令行中沒有指定這個文件,crontab命令將接受標准輸入(鍵盤)上鍵入的命令,並將它們載入crontab。
-e:編輯某個用戶的crontab文件內容。如果不指定用戶,則表示編輯當前用戶的crontab文件。
-l:顯示某個用戶的crontab文件內容,如果不指定用戶,則表示顯示當前用戶的crontab文件內容。
-r:從/var/spool/cron目錄中刪除某個用戶的crontab文件,如果不指定用戶,則默認刪除當前用戶的crontab文件。
-i:在刪除用戶的crontab文件時給確認提示。
4.常用方法:
1). 創建一個新的crontab文件
在 考慮向cron進程提交一個crontab文件之前,首先要做的一件事情就是設置環境變量EDITOR。cron進程根據它來確定使用哪個編輯器編輯 crontab文件。9 9 %的UNIX和LINUX用戶都使用vi,如果你也是這樣,那么你就編輯$ HOME目錄下的. profile文件,在其 中加入這樣一行:
EDITOR=vi; export EDITOR
然后保存並退出。不妨創建一個名為<user> cron的文件,其中<user>是用戶名,例如, davecron。在該文件中加入如下的內容。
# (put your own initials here)echo the date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/console
保存並退出。確信前面5個域用空格分隔。
在 上面的例子中,系統將每隔1 5分鍾向控制台輸出一次當前時間。如果系統崩潰或掛起,從最后所顯示的時間就可以一眼看出系統是什么時間停止工作的。在有些 系統中,用tty1來表示控制台,可以根據實際情況對上面的例子進行相應的修改。為了提交你剛剛創建的crontab文件,可以把這個新創建的文件作為 cron命令的參數:
$ crontab davecron
現在該文件已經提交給cron進程,它將每隔1 5分鍾運行一次。
同時,新創建文件的一個副本已經被放在/var/spool/cron目錄中,文件名就是用戶名(即dave)。
2). 列出crontab文件
為了列出crontab文件,可以用:
$ crontab -l
0,15,30,45,18-06 * * * /bin/echo `date` > dev/tty1
你將會看到和上面類似的內容。可以使用這種方法在$ H O M E目錄中對crontab文件做一備份:
$ crontab -l > $HOME/mycron
這樣,一旦不小心誤刪了crontab文件,可以用上一節所講述的方法迅速恢復。
3). 編輯crontab文件
如果希望添加、刪除或編輯crontab文件中的條目,而E D I TO R環境變量又設置為v i,那么就可以用v i來編輯crontab文件,相應的命令為:
$ crontab -e
可以像使用v i編輯其他任何文件那樣修改crontab文件並退出。如果修改了某些條目或添加了新的條目,那么在保存該文件時, c r o n會對其進行必要的完整性檢查。如果其中的某個域出現了超出允許范圍的值,它會提示你。
我們在編輯crontab文件時,沒准會加入新的條目。例如,加入下面的一條:
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
現在保存並退出。最好在crontab文件的每一個條目之上加入一條注釋,這樣就可以知道它的功能、運行時間,更為重要的是,知道這是哪位用戶的作業。
現在讓我們使用前面講過的crontab -l命令列出它的全部信息:
$ crontab -l
# (crondave installed on Tue May 4 13:07:43 1999)
# DT:ech the date to the console every 30 minites
0,15,30,45 18-06 * * * /bin/echo `date` > /dev/tty1
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
4 刪除crontab文件
要刪除crontab文件,可以用:
$ crontab -r
5). 恢復丟失的crontab文件
如果不小心誤刪了crontab文件,假設你在自己的$ H O M E目錄下還有一個備份,那么可以將其拷貝到/var/spool/cron/<username>,其中<username>是用戶名。如果由於權限問題無法完成拷貝,可以用:
$ crontab <filename>
其中,<filename>是你在$ H O M E目錄中副本的文件名。
我建議你在自己的$ H O M E目錄中保存一個該文件的副本。我就有過類似的經歷,有數次誤刪了crontab文件(因為r鍵緊挨在e鍵的右邊)。這就是為什么有些系統文檔建議不要直接編輯crontab文件,而是編輯該文件的一個副本,然后重新提交新的文件。
有些crontab的變體有些怪異,所以在使用crontab命令時要格外小心。如果遺漏了任何選項,crontab可能會打開一個空文件,或者看起來像是個空文件。這時敲delete鍵退出,不要按<Ctrl-D>,否則你將丟失crontab文件。
5 使用實例
實例1:每1分鍾執行一次command
命令:
* * * * * command
實例2:每小時的第3和第15分鍾執行
命令:
3,15 * * * * command
實例3:在上午8點到11點的第3和第15分鍾執行
命令:
3,15 8-11 * * * command
實例4:每隔兩天的上午8點到11點的第3和第15分鍾執行
命令:
3,15 8-11 */2 * * command
實例5:每個星期一的上午8點到11點的第3和第15分鍾執行
命令:
3,15 8-11 * * 1 command
實例6:每晚的21:30重啟smb
命令:
30 21 * * * /etc/init.d/smb restart
實例7:每月1、10、22日的4 : 45重啟smb
命令:
45 4 1,10,22 * * /etc/init.d/smb restart
實例8:每周六、周日的1 : 10重啟smb
命令:
10 1 * * 6,0 /etc/init.d/smb restart
實例9:每天18 : 00至23 : 00之間每隔30分鍾重啟smb
命令:
0,30 18-23 * * * /etc/init.d/smb restart
實例10:每星期六的晚上11 : 00 pm重啟smb
命令:
0 23 * * 6 /etc/init.d/smb restart
實例11:每一小時重啟smb
命令:
* */1 * * * /etc/init.d/smb restart
實例12:晚上11點到早上7點之間,每隔一小時重啟smb
命令:
* 23-7/1 * * * /etc/init.d/smb restart
實例13:每月的4號與每周一到周三的11點重啟smb
命令:
0 11 4 * mon-wed /etc/init.d/smb restart
實例14:一月一號的4點重啟smb
命令:
0 4 1 jan * /etc/init.d/smb restart
實例15:每小時執行/etc/cron.hourly目錄內的腳本
命令:
01 * * * * root run-parts /etc/cron.hourly
說明:
run-parts這個參數了,如果去掉這個參數的話,后面就可以寫要運行的某個腳本名,而不是目錄名了
6 使用注意事項
注意環境變量問題
有時我們創建了一個crontab,但是這個任務卻無法自動執行,而手動執行這個任務卻沒有問題,這種情況一般是由於在crontab文件中沒有配置環境變量引起的。
在 crontab文件中定義多個調度任務時,需要特別注意的一個問題就是環境變量的設置,因為我們手動執行某個任務時,是在當前shell環境下進行的,程 序當然能找到環境變量,而系統自動執行任務調度時,是不會加載任何環境變量的,因此,就需要在crontab文件中指定任務運行所需的所有環境變量,這 樣,系統執行任務調度時就沒有問題了。
不要假定cron知道所需要的特殊環境,它其實並不知道。所以你要保證在shelll腳本中提供所有必要的路徑和環境變量,除了一些自動設置的全局變量。所以注意如下3點:
1)腳本中涉及文件路徑時寫全局路徑;
2)腳本執行要用到java或其他環境變量時,通過source命令引入環境變量,如:
cat start_cbp.sh
#!/bin/sh
source /etc/profile
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev &
3)當手動執行腳本OK,但是crontab死活不執行時。這時必須大膽懷疑是環境變量惹的禍,並可以嘗試在crontab中直接引入環境變量解決問題。如:
0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh
注意清理系統用戶的郵件日志
每條任務調度執行完畢,系統都會將任務輸出信息通過電子郵件的形式發送給當前系統用戶,這樣日積月累,日志信息會非常大,可能會影響系統的正常運行,因此,將每條任務進行重定向處理非常重要。
例如,可以在crontab文件中設置如下形式,忽略日志輸出:
0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
“/dev/null 2>&1”表示先將標准輸出重定向到/dev/null,然后將標准錯誤重定向到標准輸出,由於標准輸出已經重定向到了/dev/null,因此標准錯誤也會重定向到/dev/null,這樣日志輸出問題就解決了。
系統級任務調度與用戶級任務調度
系 統級任務調度主要完成系統的一些維護操作,用戶級任務調度主要完成用戶自定義的一些任務,可以將用戶級任務調度放到系統級任務調度來完成(不建議這么 做),但是反過來卻不行,root用戶的任務調度操作可以通過“crontab –uroot –e”來設置,也可以將調度任務直接寫入/etc /crontab文件,需要注意的是,如果要定義一個定時重啟系統的任務,就必須將任務放到/etc/crontab文件,即使在root用戶下創建一個 定時重啟系統的任務也是無效的。
其他注意事項
新創建的cron job,不會馬上執行,至少要過2分鍾才執行。如果重啟cron則馬上執行。
當crontab突然失效時,可以嘗試/etc/init.d/crond restart解決問題。或者查看日志看某個job有沒有執行/報錯tail -f /var/log/cron。
千萬別亂運行crontab -r。它從Crontab目錄(/var/spool/cron)中刪除用戶的Crontab文件。刪除了該用戶的所有crontab都沒了。
在crontab中%是有特殊含義的,表示換行的意思。如果要用的話必須進行轉義\%,如經常用的date ‘+%Y%m%d’在crontab里是不會執行的,應該換成date ‘+\%Y\%m\%d’。
