Linux-搭建NTP时间同步服务器并配置客户端同步
一、安装NTP
1.1.查看系统是否安装了ntp,一般默认安装ntpdate
rpm -qa | grep ntp
1.2.安装NTP
yum -y install ntp ntpdate
二、配置NTP服务器
2.1配置ntp Server
#打开NTP服务配置文件
vim /etc/ntp.conf
#配置为所有IPv4和IPv6地址设置了一组默认的访问限制,目的是提高NTP服务器的安全性,防止未授权的修改、状态查询和对等连接,同时允许服务器响应正常的时间同步请求
restrict -4 default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# 允许内网范围
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
restrict 172.0.0.0 mask 255.0.0.0 nomodify notrap
#注释原有的服务器地址
#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
#配置NTP时间服务器(公网)
server ntp.aliyun.com iburst
#以本机的时钟为标准
#server 127.127.1.0 iburst
2.2开启ntp server
#启动NTP服务
systemctl start ntpd
#查看NTP服务的状态
systemctl status ntpd
#设置开机自启动
systemctl enable ntpd
2.3NTP常用命令
#手动同步系统时间
ntpdate
#在NTPf服务开启后手动同步
ntpd -q
#查询NTP服务器状态
ntpq -p
#查看当前时间设置
timedatectl status
#将系统时间写入硬件时钟
hwclock --systohc
#从硬件时钟设置系统时间
hwclock --hctosys
2.4配置ntp Client
#设置定时任务,定时执行命令
sudo crontab -e
#每分钟执行一次时间同步并将当前系统时间写入硬件时钟
* * * * * /usr/sbin/ntpd -q && /sbin/hwclock -w
* * * * * /usr/sbin/ntpdate ntp.aliyun.com && /sbin/hwclock -w
三、配置NTP客户端
3.1安装NTP
yum -y install ntp ntpdate
3.2配置NTP配置文件
#打开 /etc/ntp.conf 文件进行编辑
vim /etc/ntp.conf
#指定你的 NTP 服务器
server 192.168.1.100 iburst
3.3重启NTP服务
systemctl restart ntpd
3.4设置NTP服务开机启动
systemctl enable ntpd