CentOS系统NTP同步

CentOS系统NTP同步。

概述

  • 安装 NTP 服务。
  • 修改配置。
  • 开放 123 端口。
  • 启动服务。

安装

1
sudo yum -y install ntp

服务端

开机自启动:

1
2
systemctl enable ntpd
systemctl start ntpd

编辑 /etc/ntp.conf 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

#server 3.centos.pool.ntp.org iburst
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery

server 127.0.0.1 # local clock
fudge 127.0.0.1 stratum 10

重启:

1
systemctl restart ntpd

注:配置好服务重启后,似乎会自动同步。

防火墙

查看并开启123端口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# systemctl status firewalld

# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

# firewall-cmd --zone=public --add-port=123/udp --permanent

# firewall-cmd --reload

# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports: 123/udp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

客户端

编辑 /etc/ntp.conf 文件。

1
2
3
4
5
6
7
8
#server 3.centos.pool.ntp.org iburst

server 192.168.9.116

restrict 192.168.9.116 nomodify notrap noquery

server 127.0.0.1 # local clock
fudge 127.0.0.1 stratum 10

重启:

1
systemctl restart ntpde 

查看ntp服务器:

1
ntpq -p

查看同步状态:

1
ntpstat

ubuntu客户端

安装:

1
sudo apt-get install ntp ntpdate

单独测试:
使用 ntpdate 命令,后跟服务器IP即可:

1
2
ntpdate 192.168.9.116

编辑 /etc/ntp.conf,删除默认 ntp 地址,添加服务器IP:

1
2
3
4
5
6
7
8
9
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst

# by latelee
pool 192.168.9.116 iburst
# Use Ubuntu's ntp server as a fallback.
#pool ntp.ubuntu.com

重启:

1
/etc/init.d/ntp restart

测试

修改 NTP 服务器时间为2020年。稍等片刻,NTP 客户端会自动同步,时间亦为2020年。通过。