時間同步服務ntp

內外網集羣的時間同步①linux

  • Server端:能夠訪問外網的機器windows

  • Client端:在內網裏的機器centos

1、server端安裝ntp校時包,修改獨立主機bash

rm -rf /etc/localtime #先清除原有的連接文件服務器

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區到東8區。網絡

date -R #查看的時區設置。調試

接下來調整系統時間與時間服務器同步rest

 

Debian系統安裝NTP校時包:server

代碼以下:遞歸

apt-get install ntpdate #安裝ntp

 

CentOS系統安裝NTP校時包:

代碼以下:

yum -y install ntpdate ntp #安裝ntp

 

Ubuntu系統安裝NTP校時包:

代碼以下:

sudo apt-get install -y ntpdate ntp

2、server端修改/etc/ntp.conf配置文件

編輯 /etc/ntp.conf

 

server cn.pool.ntp.org

restrict default nomodifynotrapnoquery

restrict 127.0.0.1   # 開啓內部遞歸網絡接口 lo

restrict 192.168.9.0 mask 255.255.255.0 nomodify notrap #在內部子網裏面的客戶端能夠 進行網絡校時,但不能修改NTP服務器的時間參數

因爲配置的是本地時間 ,因此還須要配置一個使用系統時鐘做爲時間源的NTP服務器,須要在/etc/ntp.conf文件中添加以下的記錄:

 

server 127.127.1.0

fudge 127.127.1.0 stratum 10

而後保存退出

接着輸入下面的命令:

ntpdate -d cn.pool.ntp.org #調試查看時間差別

ntpdate cn.pool.ntp.org #手動校準系統時間

date -R # 檢查時間是否同步

 

3、server端修改ntpd 的系統配置

vi /etc/sysconfig/ntpd

SYNC_HWCLOCK=yes #同步獨立主機的硬件時鐘

而後:wq退出

chkconfig --levels 235 ntpd on #配置開機啓動ntp服務,按期同步時間

/etc/init.d/ntpd start#啓動ntp同步

 

4、client端配置ntpd服務

注:client端保證與server端的網絡通暢

編輯/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 服務端地址 nomodify

server 服務端地址 prefer

service ntpd start啓動ntpd服務

chkconfig ntpd on 加入開機啓動項

 

5、檢查ntpd的工做狀況(網絡連通性(是否開放了123端口)和同步狀況)

ntpq -p查看是否能正確輸出

date -R 每臺機器查看時間

linux下防火牆規則若是極嚴格的話可能會影響ntpd對時,打開 sport 123 便可

iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT

 

內網linux服務器之間時間同步②

 

首先設置主機器,先設置時區,在修改配置文件

rm -rf /etc/localtime #先清除原有的連接文件

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區到東8區。

date -R #查看的時區設置。將本機時間修改成標準時間

hwclock --systohc && hwclock -w 使用系統時間刷入硬件時間

編輯/etc/ntp.conf文件

在裏面加上

restrict 127.0.0.1   # 開啓內部遞歸網絡接口 lo

restrict 192.168.5.0 mask 255.255.255.0 nomodify #在內部子網裏面的客戶端能夠 進行網絡校時,但不能修改NTP服務器的時間參數

 

因爲配置的是本地時間 ,因此還須要配置一個使用系統時鐘做爲時間源的NTP服務器,須要在/etc/ntp.conf文件中添加以下的記錄:

server 127.127.1.0

fudge 127.127.1.0 stratun 10

在以上的記錄中:

指定的IP地址是一個「僞」IP地址,肯定本地系統爲時間源。

指定的IP地址127.127.1.1告訴NTP使用內部時鐘做爲時間源。

"fudge"定義了這個時鐘的級別,若是沒有這個記錄,節點就是一級服務器。將級別從新定義爲10是個好的辦法,這樣客戶端在查詢這個服務器的時候就會知道這個服務器不是一個可靠的時間源.

這種時間同步的方式只應該在本地的網絡不能使用外部的時間源的時候使用

 

啓動ntp服務

service ntpd start

設置開機啓動

chkconfig ntpd on

而後配置客戶端,在192.168.5.103這臺主機上面,編輯/etc/crontab這個文件,用於設置此節點每分鐘要像主服務器同步時間

設置以下:

* * * * * root /usr/sbin/ntpdate 192.168.5.102;/sbin/hwclock -w

查看時間是否一致

最後說起一點,ntp服務,默認只會同步系統時間。若是想要讓ntp同時同步硬件時間,能夠設置/etc/sysconfig/ntpd文件。

 

在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可讓硬件時間與系統時間一塊兒同步。

自動腳本以下:

#!/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

#客戶端修改內容

echo >> 

更新腳本2

#!/bin/bash

#備份源文件

mv /etc/localtime /etc/localtimebak

#修改時區爲東八區

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#校準當前時間

date -s "2017-10-24 12:09:38"

#使用系統時間刷入硬件時間

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

 

外網單臺機器設置時間同步③

1、安裝ntp校時包,修改獨立主機

rm -rf /etc/localtime #先清除原有的連接文件

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改時區到東8區。

date -R #查看的時區設置。

接下來調整系統時間與時間服務器同步

 

Debian系統安裝NTP校時包:

代碼以下:

apt-get install ntpdate #安裝ntp

 

CentOS系統安裝NTP校時包:

代碼以下:

yum -y install ntpdate ntp #安裝ntp

 

Ubuntu系統安裝NTP校時包:

代碼以下:

sudo apt-get install -y ntpdate ntp

2、修改/etc/ntp.conf配置文件

vi /etc/ntp.conf 就會看到如下內容:

 

server 0.centos.ntp.org

server time.windows.com

server time.nist.gov

這是默認的ntp同步服務器,你們能夠本身改,全球ntp服務器地址:http://www.pool.ntp.org/

cn.pool.ntp.org //這中國的ntp服務器

我改爲了:

 

server cn.pool.ntp.org

server time-a.nist.gov

server time.windows.com

server time.nist.gov

而後保存退出

接着輸入下面的命令:

ntpdate -d cn.pool.ntp.org #調試查看時間差別

ntpdate cn.pool.ntp.org #同步時間

date -R # 檢查時間是否同步

 

3、修改ntp 的系統配置

vi /etc/sysconfig/ntpd

SYNC_HWCLOCK=yes #同步獨立主機的硬件時鐘

而後:wq退出

chkconfig --levels 235 ntpd on #配置開機啓動ntp服務,按期同步時間

/etc/init.d/ntpd start#啓動ntp同步

 

自動腳本以下:

#!/bin/bash

#備份源文件

mv /etc/localtime /etc/localtimebak

#修改時區爲東八區

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#安裝ntp服務

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

#調試查看時間差別

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`

相關文章
相關標籤/搜索