服务器端配置:
IP 172.16.1.53
安装ntp
[[email protected] ~]# yum install ntp
或者rpm安装
[[email protected] Packages]# rpm -ivhntp-4.2.4p8-2.el6.centos.x86_64.rpm
[[email protected] ~]# vim /etc/ntp.conf
# permit the source to query or modify the service on thissystem.
# restrict default kodnomodify notrap nopeer noquery 这行注释掉
restrict default nomodify 所有IP都能连接这台时间服务器
或者
restrict 172.16.1.0 mask255.255.255.0 nomodify 指定IP段连接这台时间服务器
restrict -6 default kod nomodify notrap nopeer noquery
# nomodify 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
# notrap 不提供trap 这个运程事件登入的功能
# noquery 客户端不能够使用ntpc 与ntpq等指令来查询时间服务器,不提供NTP的网络校时
# nopeer:不与其他同一层的NTP服务器进行时间同步
# Permit all access over the loopback interface. This could
#server 0.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
server 210.72.145.44 prefer 210.72.145.44中国国家授时中心服务器
这里指定上级时间服务器,系统自带的可以注销,也可以保留,perfer是优先级的意思
# and when no outside source of synchronized time is available.
server 127.127.1.0 # local clock
fudge 127.127.1.0stratum 10
这两行的注释去掉,意思是当连接上级时间服务器失败,就以本地时间为客户端提供时间服务
# Enable public key cryptography.
[[email protected] ~]# /etc/init.d/ntpd start
正在启动 ntpd: [确定]
[[email protected] ~]# chkconfig ntpd on
[[email protected] ~]# chkconfig --list ntpd
ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
客户端配置:
IP 172.16.1.59
安装ntp
[[email protected] ~]# yum install ntp
或者rpm安装
[[email protected] Packages]# rpm -ivhntp-4.2.4p8-2.el6.centos.x86_64.rpm
[[email protected] ~]# vim /etc/ntp.conf
#server 0.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
server 172.16.1.53
在这里添加远程时间服务器地址即可
[[email protected] ~]# /etc/init.d/ntpd start
正在启动 ntpd: [OK]
[[email protected] ~]# chkconfig ntpd on 设置服务开机自启动
[[email protected] ~]# chkconfig --list ntpd 查看开机自启情况 3和5是on就行
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
初始同步时间需要5分钟左右,如果立即手工同步会有下面的报错
[[email protected] ~]# ntpdate 172.16.1.53
21 Jul 07:39:58 ntpdate[1184]: no server suitable forsynchronization found
[[email protected] ~]# ntpdate 172.16.1.53 手工同步时间服务器
21 Jul 07:43:24 ntpdate[1212]: the NTP socket is in use,exiting
检查时间服务器是否正确同步
[[email protected] ~]# ntpq -p
remote refid st t whenpoll reach delay offset jitter
==============================================================================
*172.16.1.53 LOCAL(0) 11 u 6 64 377 0.238 -4.623 2.174
remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
如果有[*] 代表目前正在使用当中的上层NTP服务器。
如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选ntp服务器 refid :指的是给上层NTP服务器提供时间校对的服务器。
St:上层NTP服务器的级别。
When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
Poll :下一次本地主机与上层NTP服务器进行时间校对的时间(单位:s)
reach:已经向上层 NTP 服务器要求更新的次数
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位与 10^(-3) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间,单位为 10^(-6) 秒
设置自动更新时间
以root身份运行周期性任务:
[[email protected] ~]# crontab -e
15 * * * * ntpdate172.16.1.53 每15分钟更新一下时间
:wq
到此全部配置完成