CentOS7之NTP服務器簡單配置
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
Active: inactive (dead)
Docs: man:firewalld(1)
[root@m02 ~]# hostname -I
10.0.0.62 172.16.1.62
第一種方法:NTP
服務端部署
-
下載軟件
[root@m02 ~]# yum install -y ntp
[root@m02 ~]# rpm -qa ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64 - 配置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
- 與備份文件對比
[root@m02 ~]# diff /etc/ntp.conf{,.ori}
< #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.comserver 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@m02 ~]# systemctl start ntpd
- 檢查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
客戶端部署
-
下載軟件
[root@Cobbler ~]# yum -y install ntpdate -
測試是否同步
[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 - 定時任務
[root@Cobbler ~]# crontab -l
#時間同步
/5 * /usr/sbin/ntpdate 172.16.1.62 &>/dev/null
第二種方法:chrony
服務端部署
-
修改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.com26 # Allow NTP client access from local network.
27 #allow 192.168.0.0/16
28 allow 172.16.1.0/24 - 開啟服務
[root@m02 ~]# systemctl start chronyd
客戶端部署 - 修改配置文件
[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
-
開啟服務
[root@Cobbler ~]# systemctl start chronyd -
修改時間測試
[root@Cobbler ~]# date -s 1111
Wed Jan 10 11:11:00 CST 2018 - 查看日誌
[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
CentOS7之NTP服務器簡單配置