|
配置NTP-server
环境:
[root@LNMP ~]# cat/etc/redhat-release
CentOS release 6.5 (Final)
[root@LNMP ~]# uname -r
2.6.32-431.el6.x86_64
[root@LNMP ~]# uname -m
x86_64
安装NTP软件:
[root@LNMP ~]# yum install -yntp 检查远程ntp服务器是否可用:
[root@LNMP ~]# ntpdate -q ntp5.aliyun.com
server 182.92.12.11, stratum2, offset -0.011808, delay 0.03224
20 Apr 21:15:37ntpdate[3429]: adjust time server 182.92.12.11 offset -0.011808 sec 配置ntp服务之前,先手动同步一下时间:
[root@LNMP ~]# ntpdate ntp5.aliyun.com 编辑NTP配置文件:(文件位置在/etc/ntp.conf,编辑前备份)
[root@LNMP ~]# cp/etc/ntp.conf /etc/ntp.conf.ori
[root@LNMP ~]# vim/etc/ntp.conf
restrict 192.168.22.0 mask255.255.255.0 nomodify notrap
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
server ntp4.aliyun.com
server ntp5.aliyun.com
server ntp6.aliyun.com
server ntp7.aliyun.com
# UndisciplinedLocal Clock. This is a fake driver intended for backup
# and when nooutside source of synchronized time is available.
# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10 把之前的参数注释掉:
#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 检查服务状态:
[root@LNMP ~]# netstat-lntup|grep ntp
udp 0 0 192.168.22.134:123 0.0.0.0:* 1186/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 1186/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 1186/ntpd
udp 0 0 fe80::20c:29ff:fe51:259b:123 :::* 1186/ntpd
udp 0 0 ::1:123 :::* 1186/ntpd
udp 0 0 :::123 :::* 1186/ntpd[root@LNMP ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*time5.aliyun.co10.137.38.86 2 u 181 256 377 9.293 -1.990 1.154
+120.25.115.19 10.137.38.86 2 u 138 256 377 45.598 -1.924 1.010
+120.25.115.20 10.137.38.86 2 u 157 256 377 45.795 -1.713 1.113
-time4.aliyun.co10.137.38.86 2 u 247 256 377 45.650 -5.136 2.278
-time6.aliyun.co10.137.38.86 2 u 6 256 377 74.053 8.728 3.008
LOCAL(0) .LOCL. 10 l 128m 64 0 0.000 0.000 0.000 ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
[root@LNMP ~]# ntpstat
synchronised to NTP server(182.92.12.11) at stratum 3
time correct to within 30 ms
polling server every 256 s 如果服务器当前时间和远程ntp服务器时间相差很多或者服务器时间比远程ntp服务器时间要快的话,那么服务器是不会与远程ntp服务器同步时间的
需要在ntp.conf中加上一行参数:
tinker panic 0 将同步的时间与硬件时钟进行同步:
[root@LNMP ~]# vim /etc/sysconfig/ntpd
添加一行
SYNC_HWCLOCK="yes"
保存退出,重启ntp服务 至此,NTP-server时间服务器配置完成!
配置内网NTP-client
首先需要安装NTPD服务,然后配置为自启动(与NTP-Server完全一样)。然后找其中一台配置/etc/ntp.conf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。
[root@Proxy ~]# grep -Ev '#'/etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
server 192.168.22.134
restrict 192.168.22.134nomodify notrap noquery
fudge 127.127.1.0 stratum 10
includefile/etc/ntp/crypto/pw
keys /etc/ntp/keys 将同步的时间与硬件时钟进行同步:
[root@Proxy ~]# vim/etc/sysconfig/ntpd
添加一行:
SYNC_HWCLOCK="yes"
保存退出,重启ntp服务。 检查ntp服务状态:
[root@Proxy ~]# netstat-lntup|grep ntp
udp 0 0 192.168.22.136:123 0.0.0.0:* 2833/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2833/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2833/ntpd
udp 0 0 fe80::20c:29ff:feff:3bf5:123 :::* 2833/ntpd
udp 0 0 ::1:123 :::* 2833/ntpd
udp 0 0 :::123 :::* 2833/ntpd
[root@Proxy ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.22.134 173.255.246.13 3 u 20 64 17 0.218 24.195 0.084
*LOCAL(0) .LOCL. 10 l 24 64 17 0.000 0.000 0.000
[root@Proxy ~]# ntpstat
synchronised to NTP server(192.168.22.134) at stratum 4
time correct to within 53 ms
polling server every 128 s |
|
|
|
|
|
|