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