时间服务器 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。 NTP服务器就是利用NTP协议提供时间同步服务的。

ntp软件(支持ntp协议) CentOS6自带 CentOS7需要安装的


chrony软件(支持ntp协议) CentOS7自带


环境准备 [root@Cobbler ~]# getenforce Disabled [root@Cobbler ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1) [root@m02 ~]# hostname -I 10.0.0.62 172.16.1.62

第一种方法:NTP

服务端部署

  1. 下载软件 [root@m02 ~]# yum install -y ntp [root@m02 ~]# rpm -qa ntp ntp-4.2.6p5-25.el7.centos.2.x86_64

  2. 配置ntp文件 备份 [root@m02 ~]# cp /etc/ntp.conf{,.ori} 修改 [root@m02 ~]# vim /etc/ntp.conf

8 #restrict default nomodify notrap nopeer noquery 9 restrict default nomodify

21 #server 0.centos.pool.ntp.org iburst 22 #server 1.centos.pool.ntp.org iburst 23 #server 2.centos.pool.ntp.org iburst 24 #server 3.centos.pool.ntp.org iburst 25 server ntp1.aliyun.com

  1. 与备份文件对比 [root@m02 ~]# diff /etc/ntp.conf{,.ori} 8,9c8,9 < #restrict default nomodify notrap nopeer noquery < restrict default nomodify

restrict default nomodify notrap nopeer noquery

21,25c21,25 < #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 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

  1. 启动服务

[root@m02 ~]# systemctl start ntpd 5. 检查ntp是否能同步 [root@m02 ~]# ntpq -p remote refid st t when poll reach delay offset jitter


time5.aliyun.co 10.137.38.86 2 u 36 64 1 52.686 -1371.9 0.000

[root@m02 ~]# ntpstat synchronised to NTP server (182.92.12.11) at stratum 3 time correct to within 982 ms polling server every 64 s

客户端部署

  1. 下载软件 [root@Cobbler ~]# yum -y install ntpdate

  2. 测试是否同步 [root@Cobbler ~]# ntpdate 172.16.1.62 10 Jan 09:07:47 ntpdate[1504]: adjust time server 172.16.1.62 offset -0.001513 sec

  3. 定时任务

[root@Cobbler ~]# crontab -l #时间同步 */5 * * * * /usr/sbin/ntpdate 172.16.1.62 &>/dev/null

第二种方法:chrony

服务端部署

  1. 修改chrony配置文件 [root@m02 ~]# vim /etc/chrony.conf 1 # Use public servers from the pool.ntp.org project. 2 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 3 #server 0.centos.pool.ntp.org iburst 4 #server 1.centos.pool.ntp.org iburst 5 #server 2.centos.pool.ntp.org iburst 6 #server 3.centos.pool.ntp.org iburst 7 server ntp1.aliyun.com

26 # Allow NTP client access from local network. 27 #allow 192.168.0.0/16 28 allow 172.16.1.0/24

  1. 开启服务 [root@m02 ~]# systemctl start chronyd 客户端部署
  2. 修改配置文件 [root@Cobbler ~]# vim /etc/chrony.conf

1 # Use public servers from the pool.ntp.org project. 2 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 3 #server 0.centos.pool.ntp.org iburst 4 #server 1.centos.pool.ntp.org iburst 5 #server 2.centos.pool.ntp.org iburst 6 #server 3.centos.pool.ntp.org iburst 7 server 172.16.1.62

  1. 开启服务 [root@Cobbler ~]# systemctl start chronyd

  2. 修改时间测试 [root@Cobbler ~]# date -s 1111 Wed Jan 10 11:11:00 CST 2018

  3. 查看日志 [root@Cobbler ~]# tailf /var/log/messages Jan 10 09:30:26 Cobbler chronyd[1593]: Selected source 172.16.1.62 Jan 10 09:30:26 Cobbler chronyd[1593]: System clock wrong by -6348.794966 seconds, adjustment started Jan 10 09:30:26 Cob[root@Cobbler ~]# date -s 1111 Wed Jan 10 11:11:00 CST 2018

5.查看时间 [root@Cobbler ~]# date Wed Jan 10 11:15:47 CST 2018 [root@Cobbler ~]# date Wed Jan 10 09:30:37 CST 2018