Centos6.5時間服務器NTP搭建
第1章 Server端的安裝與配置
1.1 查看系統是否已經安裝ntp服務組件
rpm -qa | grep "ntp" #<==查看是否已經安裝ntp組件,有如下兩個組件說明已經安裝 ntpdate-4.2.6p5-1.el6.centos.x86_64 ntp-4.2.6p5-1.el6.centos.x86_64
1.2 yum安裝
yum -y install ntp
1.3 ntp服務器端的配置
1.3.1 配置文件
? 配置文件
/etc/ntp.conf
? 配置文件默認內容
egrep -v "^$|#" ntp.conf #<==排除默認註釋及空行 driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery #<==對默認的client拒絕所有操作 restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 #<==允許本地地址的一切操作 restrict -6 ::1 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 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys
? 修改配置
cat /etc/ntp.conf driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery #<==對默認的client拒絕所有操作 restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 #<==允許本機的一切操作 restrict 192.168.1.0 mask 255.255.255.0 nomodify #<==允許局域網內所有client連接到這臺服務器 restrict -6 ::1 同步時間.但是拒絕讓他們修改服務器上的時間 #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 ntp1.aliyun.com #<==同步公網的時間 server 127.127.1.0 #<==如果無法連接公網,則使用自己本身的時間作為標準時間。 fudge 127.127.1.0 stratum 10 #<==設置stratum級別 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys
1.3.2 restrict安全相關參數
ignore :關閉所有的 NTP 聯機服務
nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。
notrust :客戶端除非通過認證,否則該客戶端來源將被視為不信任子網
noquery :不提供客戶端的時間查詢
第2章 Client的安裝與配置
客戶端的安裝同服務器,如上。
2.1 配置文件
driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 restrict -6 ::1 #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 ntpserver #<==添加自己的時間服務器 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys
第3章 服務啟動
3.1 服務器段啟動
在服務器端啟動NTP服務,命令如下:
/etc/init.d/ntpd start
添加開啟啟動:
chkconfig ntpd on
3.2 客戶端同步
3.2.1 ntpdate命令同步
在客戶端運行時間同步命令ntpdate,測試服務器是否工作。該方法可以不用再客戶端啟動ntpd服務,可以將ntpd服務做成定時任務來同步時間服務:
命令格式:ntpdate server
ntpdate master1 19 Dec 17:46:03 ntpdate[20131]: adjust time server 192.168.1.211 offset -0.000042 sec
註:ntpdate命令需要客戶端不能運行ntpd服務,否則會報如下錯誤:
ntpdate master1 19 Dec 17:45:51 ntpdate[20110]: the NTP socket is in use, exiting
3.2.2 ntpdate定時任務
echo '*/10 * * * * /usr/sbin/ntpdate master1 &>/dev/null' >> /var/spool/cron/root
該命令是做一個每10分鐘同步一次時間服務器的任務,其中master1是自己配置的時間服務器,&>/dev/null表示不打印日誌。
3.2.3 ntpd服務同步
啟動ntpd服務:
/etc/init.d/ntpd start
查看同步的狀態:ntpq -p
ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== time5.aliyun.co 10.137.38.86 2 u 27 64 1 52.650 3657191 0.000 *LOCAL(0) .LOCL. 10 l 26 64 1 0.000 0.000 0.000
各項參數的含義如下:
服務參數 | 含義 |
remote | 本地機器連接的遠程NTP服務器。 |
refid | 給遠程服務器提供時間同步的服務器。 |
st | 遠程服務器的層級別(stratum)。由於NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端,所以服務器從高到低級別可以設定為1-16。 |
t | 本地NTP服務器與遠程NTP服務器的通信方式,u:單播;b:廣播;i:本地 |
when | 本地機器和遠程服務器進行一次時間同步的剩余時間。 |
poll | 本地機和遠程服務器多少時間進行一次同步(單位為秒) |
reach | 測試能夠和服務器連接,每成功一次它的值就會增加 |
delay | 從本地機發送同步要求到服務器的round trip time |
offset | 本地機和服務器之間的時間差別。Offset接近0,就和時間服務器的時間越接近。 |
jitter | 統計了再特定個連續的連接數裏offset的分布情況。 |
+ | 它將作為輔助的NTP Server和帶有*號的服務器一起為我們提供同步服務. 當*號服務器不可用時它就可以接管 |
* | 遠端的服務器已經被確認為我們的主NTP Server,我們系統的時間將由這臺機器所提供 |
- | 遠程服務器被clustering algorithm認為是不合格的NTP Server |
x | 遠程服務器不可用 |
3.3 同步機器時間
linux除了系統時間之外,還有機器時間,這兩者可能不同一,可以用hwclock查看機器時間:
hwclock Tue 19 Dec 2017 06:27:40 PM CST -0.627173 seconds
使用hwclock –w命令將系統時間同步到機器時間
hwclock –w
Centos6.5時間服務器NTP搭建