王海庆的云笔记

CentOS7搭建时间服务器-chrony


centos6可以安装ntp工具,centos7桌面版默认安装chrony工具。


服务端配置


关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
sed -i -e  's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0


换成阿里云的源

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

yum clean all
yum makecache fast -y


安装chrony 

yum install chrony -y


查看 chronyd 服务运行情况

systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd


chronyc查看当前同步情况

chronyc sources

^表示服务,*代表同步成功,?代表未同步


查看一下当前时间 

timedatectl


修改同步服务器为阿里云

sed -i 's/server 0.*/server ntp.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 1.*/server ntp1.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 2.*/server ntp2.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 3.*/server ntp3.aliyun.com iburst/g' /etc/chrony.conf


打开允许同步的IP (可选)

cat >> /etc/chrony.conf << 'EOF'

allow 172.18.0.0/20

EOF


重启chronyd服务

systemctl restart chronyd
systemctl enable chronyd
systemctl status chronyd


开放端口

systemctl status firewalld

firewall-cmd --permanent --add-port=123/udp
firewall-cmd --reload

netstat -naup


 客户端配置


安装chrony 

yum install chrony -y


查看 chronyd 服务运行情况

systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd


chronyc查看当前同步情况

chronyc sources


查看一下当前时间 

timedatectl


手动同步时间

ntpdate 172.18.0.117


自动同步时间

sed -i 's/server 0.*/server 172.18.0.117 iburst/g' /etc/chrony.conf
sed -i '/server 1\./d' /etc/chrony.conf
sed -i '/server 2\./d' /etc/chrony.conf
sed -i '/server 3\./d' /etc/chrony.conf


重启chronyd服务

systemctl restart chronyd
systemctl enable chronyd
systemctl status chronyd


chronyc查看当前同步情况

chronyc sources

^表示服务,*代表同步成功,?代表未同步


查看一下当前时间 

timedatectl


硬件时间

hwclock --show
hwclock --systohc # 将硬件时钟调整为与目前的系统时钟一致


无法修改时间,可以这样操作

timedatectl set-ntp no
timedatectl set-time '2018-10-11 09:00:00'

date -s '2018-10-11 09:00:00'



文章最后更新时间: 2021-11-16 17:00:28