若服务器不方便安装ntp服务,可参考《Linux 配置chrony时间同步》。
一、环境
操作系统:CentOS Linux 7 (Core)
CPU:x86_64
ntp 服务端机器:192.168.100.26
ntp 客户端机器:192.168.100.27
所有节点确保 ntp 已安装
--确认 ntp 已安装
rpm -qa|grep ntp--若没有则安装
yum install -y ntp
所有节点确保防火墙已关闭或开放相关端口
--防火墙开端口
firewall-cmd --permanent --add-port=123/udp
firewall-cmd --add-port=123/udp--关闭禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
二、服务端机器配置
--修改 ntp 配置文件,添加优先同步网上的时间服务器,并放开权限允许其他机器同步自己时间
vi /etc/ntp.confserver ntp.aliyun.com prefer
server 127.0.0.1 prefer
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap--启动 ntp 服务
systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd--如上同步的为网络系统时间,需同步硬件时间可添加如下参数
vi /etc/sysconfig/ntpdSYNC_HWCLOCK=yes
系统硬件时间介绍可参考:《Linux硬件时间和系统时间》
三、客户端配置
3.1 方式一:ntp 服务同步
--修改 ntp 配置文件,添加优先同步网上的时间服务器或上述搭建的ntp服务器时间
vi /etc/ntp.confserver ntp.aliyun.com prefer
server 192.168.100.26 prefer--启动 ntp 服务
systemctl start ntpd
systemctl enable ntpd
systemctl status ntpd--如上同步的为网络系统时间,需同步硬件时间可添加如下参数
vi /etc/sysconfig/ntpdSYNC_HWCLOCK=yes--客户端验证时间已同步
ntpq -p
或
ntpstat
3.2 方式二:ntpdate 命令同步
--需使用 ntpdate 命令,没有的,需要安装
yum install -y ntpdate--确认 ntpdate 命令所在路径
whereis ntpdate--添加到系统定时任务,每五分钟执行一次
crontab -e*/5 * * * * /usr/sbin/ntpdate 192.168.100.26