一、环境

系统:Centos 7.4
以两台服务器为例,其中一台服务器为server,一台为client:
ntp_server IP:192.168.79.14.2
ntp_client IP:192.168.79.143

二、ntp安装服务

centos 下执行 : yum install ntp
debian 下执行 : apt install ntp
查看服务 : service ntpd status
查看当前时区:timedatectl status :
image.png

三、修改配置/etc/ntp.conf文件

执行命令前 先备份ntp.conf 文件 : cp ntp.conf ntp.conf.bat
执行命令:vim /etc/ntp.conf

server服务器配置:

image.png
注释掉原有的
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
其中, restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap #表示允许集群192.168.0.0网 段连接同步时间,拒绝client修改服务器时间
server 127.127.1.0 #本地时间 fudge 127.127.1.0 stratum 10 #时间服务器的层次。可设置0-15,设为0则为顶级

client服务器配置:(如果有多台client服务器,亦可如此配置)

image.png

其中,server 192.168.79.142 #时间同步server服务器地址

四、启动NTP服务&开机启动设置(包括server服务器和client服务器)

启动NTP服务
systemctl start ntpd

将NTP服务设置为开机启动
systemctl enable ntpd

服务重启后,检查ntp服务是否加入开机自启动(如果自启动失效,详见异常情况说明)
systemctl status ntpd
重启后执行
systemctl status ntpd 命令后的效果,如下图所示:
image.png

五、server服务器防火墙配置

开放NTP端口
firewall-cmd —add-port=123/udp —permanent

重载防火墙规则
firewall-cmd —reload

六、client服务器手动从server服务器进行时间同步

验证是否能同步成功,可现在客户端date -s 随意设置时间;
image.png
如果时区不对,设置时区 timedatectl set-timezone Asia/Shanghai

client服务器执行命令进行时间同步
ntpdate -u 192.168.79.142
image.png

七、client服务器设置定时同步时间任务

crontab -e
2 /usr/sbin/ntpdate -u 192.168.79.142 >/dev/null 2>&1

此处192.168.79.142为server服务器ip地址
此时,可在client设置任意时间,如:date -s “20190315 15:12:23”,隔五分钟查看时间是否与server服务器时间一致

八.附加说明

systemctl start ntpd #启动ntp服务器
systemctl stop ntpd #停止ntp服务器
crontab -l 查看 定时任务
# 将硬件时间同步到系统时间: hwclock —hctosys
# 将系统时间同步到硬件时间 hwclock —systohc
# 强制把系统时间写入CMOS clock -w
# 显示硬件时间 hwclock —show
# 设置硬件时间 hwclock —set —date ‘08/02/2012 12:00:00’
# 更改时区 ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime

九.UOS同步centos NTP服务器

1.安装NTP
apt install ntp
2.vim /etc/ntp.conf
如下图
image.png
3.启动NTP
systemctl status ntp
systemctl start ntp
systemctl stop ntp

4.手动同步
ntpdate -u 192.168.79.142

5.date 查看

十.UOS系统下区分架构

使用命令uname -a :有amd和arm区分
image.png
image.png

体现在NTP服务器deb包上:
https://pkgs.org/download/ntp

image.png