Linux服務(wù)器時間同步那些事
內(nèi)外網(wǎng)集群的時間同步①
- Server端:可以訪問外網(wǎng)的機器
- Client端:在內(nèi)網(wǎng)里的機器
一、server端安裝ntp校時包,修改獨立主機
rm -rf /etc/localtime #先清除原有的鏈接文件
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區(qū)到東8區(qū)。
date -R #查看的時區(qū)設(shè)置。
接下來調(diào)整系統(tǒng)時間與時間服務(wù)器同步
Debian系統(tǒng)安裝NTP校時包:
代碼如下:
- apt-get install ntpdate #安裝ntp
CentOS系統(tǒng)安裝NTP校時包:
代碼如下:
- yum -y install ntpdate ntp #安裝ntp
Ubuntu系統(tǒng)安裝NTP校時包:
代碼如下:
- sudo apt-get install -y ntpdate ntp
二、server端修改/etc/ntp.conf配置文件
編輯 /etc/ntp.conf
- server cn.pool.ntp.org
- restrict default nomodifynotrapnoquery
- restrict 127.0.0.1 # 開啟內(nèi)部遞歸網(wǎng)絡(luò)接口 lo
- restrict 192.168.9.0 mask 255.255.255.0 nomodify notrap #在內(nèi)部子網(wǎng)里面的客戶端可以 進行網(wǎng)絡(luò)校時,但不能修改NTP服務(wù)器的時間參數(shù)
由于配置的是本地時間 ,所以還需要配置一個使用系統(tǒng)時鐘作為時間源的NTP服務(wù)器,需要在/etc/ntp.conf文件中添加如下的記錄:
- server 127.127.1.0
- fudge 127.127.1.0 stratum 10
然后保存退出
接著輸入下面的命令:
- ntpdate -d cn.pool.ntp.org #調(diào)試查看時間差異
- ntpdate cn.pool.ntp.org #手動校準系統(tǒng)時間
- date -R # 檢查時間是否同步
三、server端修改ntpd 的系統(tǒng)配置
- vi /etc/sysconfig/ntpd
- SYNC_HWCLOCK=yes #同步獨立主機的硬件時鐘
然后:wq退出
- chkconfig --levels 235 ntpd on #配置開機啟動ntp服務(wù),定期同步時間
- /etc/init.d/ntpd start#啟動ntp同步
四、client端配置ntpd服務(wù)
注:client端保證與server端的網(wǎng)絡(luò)通暢
編輯/etc/ntp.conf文件,前面的這兩行注釋掉
- #restrict default kod nomodify notrap nopeer noquery
- #restrict -6 default kod nomodify notrap nopeer noquery
- #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
在里面加上
restrict 服務(wù)端地址 nomodify
server 服務(wù)端地址 prefer
service ntpd start啟動ntpd服務(wù)
chkconfig ntpd on 加入開機啟動項
五、檢查ntpd的工作情況(網(wǎng)絡(luò)連通性(是否開放了123端口)和同步情況)
ntpq -p查看是否能正確輸出
date -R 每臺機器查看時間
linux下防火墻規(guī)則如果極嚴格的話可能會影響ntpd對時,打開 sport 123 即可
- iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT
內(nèi)網(wǎng)linux服務(wù)器之間時間同步②
首先設(shè)置主機器,先設(shè)置時區(qū),在修改配置文件
- rm -rf /etc/localtime #先清除原有的鏈接文件
- ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區(qū)到東8區(qū)。
- date -R #查看的時區(qū)設(shè)置。將本機時間修改為標準時間
hwclock --systohc && hwclock -w 使用系統(tǒng)時間刷入硬件時間
編輯/etc/ntp.conf文件
在里面加上
- restrict 127.0.0.1 # 開啟內(nèi)部遞歸網(wǎng)絡(luò)接口 lo
- restrict 192.168.5.0 mask 255.255.255.0 nomodify #在內(nèi)部子網(wǎng)里面的客戶端可以 進行網(wǎng)絡(luò)校時,但不能修改NTP服務(wù)器的時間參數(shù)
由于配置的是本地時間 ,所以還需要配置一個使用系統(tǒng)時鐘作為時間源的NTP服務(wù)器,需要在/etc/ntp.conf文件中添加如下的記錄:
- server 127.127.1.0
- fudge 127.127.1.0 stratun 10
在以上的記錄中:
指定的IP地址是一個“偽”IP地址,確定本地系統(tǒng)為時間源。
指定的IP地址127.127.1.1告訴NTP使用內(nèi)部時鐘作為時間源。
"fudge"定義了這個時鐘的級別,如果沒有這個記錄,節(jié)點就是一級服務(wù)器。將級別重新定義為10是個好的辦法,這樣客戶端在查詢這個服務(wù)器的時候就會知道這個服務(wù)器不是一個可靠的時間源.
這種時間同步的方式只應(yīng)該在本地的網(wǎng)絡(luò)不能使用外部的時間源的時候使用
啟動ntp服務(wù)
- service ntpd start
設(shè)置開機啟動
- chkconfig ntpd on
然后配置客戶端,在192.168.5.103這臺主機上面,編輯/etc/crontab這個文件,用于設(shè)置此節(jié)點每分鐘要像主服務(wù)器同步時間
設(shè)置如下:
- * * * * * root /usr/sbin/ntpdate 192.168.5.102;/sbin/hwclock -w
查看時間是否一致
***提及一點,ntp服務(wù),默認只會同步系統(tǒng)時間。如果想要讓ntp同時同步硬件時間,可以設(shè)置/etc/sysconfig/ntpd文件。
在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可以讓硬件時間與系統(tǒng)時間一起同步。
自動腳本如下:
- #!/bin/bash
- cat << EOF >> /etc/ntp.conf
- restrict 127.0.0.1
- restrict 192.168.5.0 mask 255.255.255.0 nomodify
- server 127.127.1.0
- fudge 127.127.1.0 stratun 10
- EOF
- service ntp restart
- chkconfig ntpd on
- #客戶端修改內(nèi)容
- echo >>
更新腳本2
- #!/bin/bash
- #備份源文件
- mv /etc/localtime /etc/localtimebak
- #修改時區(qū)為東八區(qū)
- ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- #校準當前時間
- date -s "2017-10-24 12:09:38"
- #使用系統(tǒng)時間刷入硬件時間
- hwclock --systohc
- #修改配置文件
- cat << EOF >> /etc/ntp.conf
- restrict 127.0.0.1
- restrict 192.168.9.0 mask 255.255.255.0 nomodify
- server 127.127.1.0
- fudge 127.127.1.0 stratun 10
- EOF
- #重啟開機自啟
- service ntpd restart
- chkconfig ntpd on
外網(wǎng)單臺機器設(shè)置時間同步③
一、安裝ntp校時包,修改獨立主機
- rm -rf /etc/localtime #先清除原有的鏈接文件
- ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區(qū)到東8區(qū)。
- date -R #查看的時區(qū)設(shè)置。
接下來調(diào)整系統(tǒng)時間與時間服務(wù)器同步
Debian系統(tǒng)安裝NTP校時包:
代碼如下:
- apt-get install ntpdate #安裝ntp
CentOS系統(tǒng)安裝NTP校時包:
代碼如下:
- yum -y install ntpdate ntp #安裝ntp
Ubuntu系統(tǒng)安裝NTP校時包:
代碼如下:
- sudo apt-get install -y ntpdate ntp
二、修改/etc/ntp.conf配置文件
vi /etc/ntp.conf 就會看到以下內(nèi)容:
- server 0.centos.ntp.org
- server time.windows.com
- server time.nist.gov
這是默認的ntp同步服務(wù)器,大家可以自己改,全球ntp服務(wù)器地址:http://www.pool.ntp.org/
cn.pool.ntp.org //這中國的ntp服務(wù)器
我改成了:
- server cn.pool.ntp.org
- server time-a.nist.gov
- server time.windows.com
- server time.nist.gov
然后保存退出
接著輸入下面的命令:
- ntpdate -d cn.pool.ntp.org #調(diào)試查看時間差異
- ntpdate cn.pool.ntp.org #同步時間
- date -R # 檢查時間是否同步
三、修改ntp 的系統(tǒng)配置
- vi /etc/sysconfig/ntpd
- SYNC_HWCLOCK=yes #同步獨立主機的硬件時鐘
然后:wq退出
- chkconfig --levels 235 ntpd on #配置開機啟動ntp服務(wù),定期同步時間
- /etc/init.d/ntpd start#啟動ntp同步
自動腳本如下:
- #!/bin/bash
- #備份源文件
- mv /etc/localtime /etc/localtimebak
- #修改時區(qū)為東八區(qū)
- ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- #安裝ntp服務(wù)
- yum -y install ntpdate ntp
- #修改/etc/ntp.conf
- cat << EOF >> /etc/ntp.conf
- server cn.pool.ntp.org
- server time-a.nist.gov
- server time.windows.com
- server time.nist.gov
- EOF
- #調(diào)試查看時間差異
- ntpdate -d cn.pool.ntp.org
- #同步時間
- ntpdate cn.pool.ntp.org && echo "SYNC_HWCLOCK=yes" >>/etc/sysconfig/ntpd || echo "Setting Filed!"
- #自啟動
- chkconfig --levels 235 ntpd on
- /etc/init.d/ntpd start
- echo `date`