配置时间同步服务器
系统环境:CentOS release 6.5
源码:ntp-4.2.8p5.tar.gz
http://www.ntp.org/downloads.html
[zhaojq@virtual-machine]# tar -xzvf ntp-4.2.8p5.tar.gz
[zhaojq@virtual-machine]# mkdir /usr/ntp
/usr/ntp为ntpd的安装后文件路径
[zhaojq@virtual-machine]# cd ntp-4.2.8p5
[zhaojq@virtual-machine]# ./configure --prefix=/usr/ntp --enable-all-clocks --enable-parse-clocks
[zhaojq@virtual-machine]# make && make install
安装成功
修改ntp.conf配置文件
vi /etc/ntp.conf
1、允许任何IP的客户机都可以进行时间同步
将语句
restrict default kod nomodify notrap nopeer noquery
修改为
restrict default nomodify
2、只允许192.168.1.***网段的客户机进行时间同步
在restrict default nomodify notrap noquery(表示默认拒绝所有IP的时间同步)之后增加一行:
restrict 192.168.1.0 mask 255.255.255.0 nomodify
3、修改时间服务器池
ntp.org现在提供NTP POOL PROJECT,即ntp 服务器池项目。在中国区域,配置如下即可:
server 0.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.org
其中0.cn 是中国服务器池,*.asia是亚洲的服务器池。
最新列表见:http://www.pool.ntp.org/zone/cn
以守护进程启动ntpd
/usr/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid
ntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。
配置时间同步客户机
客户端机器使用ntpdate 程序来执行同步。 格式如下:
ntpdate ntp服务器IP
注:如果客户机没有ntpdate,可以将ntp-4.2.8p5中的ntpdate复制到客户机的/usr/sbin/目录
配置客户机定时进行时间同步
vi /etc/crontab
增加一行,在每天的5点10分、9点10分、14点10分、19点10分与时间同步服务器进行同步
10 5,9,14,19 * * * /usr/sbin/ntpdate ntp服务器IP
客户机用 ntpdate -d ntp服务器IP命令 进行同步测试,查看返回信息:
导致 no server suitable for synchronization found 的错误的信息:Server dropped: no data
客户机用 ntpdate -d ntp服务器IP命令,有错误信息如下:
[zhaojq@virtual-machine]# /home/ntp/ntpdate -d 61.147.198.101
20 Jan 14:30:52 ntpdate[3436]: ntpdate 4.2.6p4@1.2324 Thu Aug 6 12:00:35 UTC 2015 (1)
Looking for host 61.147.198.101 and service ntp
host found : 61.147.198.101
transmit(61.147.198.101)
transmit(61.147.198.101)
transmit(61.147.198.101)
transmit(61.147.198.101)
transmit(61.147.198.101)
61.147.198.101: Server dropped: no data
server 61.147.198.101, port 123
stratum 0, precision 0, leap 00, trust 000
refid [61.147.198.101], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
transmit timestamp: da49a82c.aff08e8c Wed, Jan 20 2016 14:31:08.687
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
20 Jan 14:31:10 ntpdate[3436]: no server suitable for synchronization found
出现这个问题的原因可能有2:
1.检查ntp的版本.如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。
使用以下命令检查ntp的版本:
# ntpq -c version
下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".
In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd
解决:把notrust去掉。
2.检查ntp server的防火墙.可能是server的防火墙屏蔽了udp 123端口。
可以用命令
[zhaojq@virtual-machine]# service iptables stop
来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。
[zhaojq@virtual-machine]# iptables -I INPUT -p udp --dport 123 -j ACCEPT
将UTC时间修改为CST时间
[zhaojq@virtual-machine]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Tue May 7 22:43:11 CST 2019.
Universal Time is now: Tue May 7 14:43:11 UTC 2019.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai