linux 局域网时钟同步

Linux时钟同步

一 需求

​ 以172.20.1.152作为时钟服务器,其他服务器根据这台服务器进行时钟同步。

二 时钟服务器172.20.1.152实施

2.1 安装ntp

#检查是否已经安装,出现了红框中的就说明已经安装了
rpm -qa | grep ntp
#安装ntp
yum install ntp

2.2 配置时钟服务器

vi /etc/ntp.conf
#代表允许172.20网段的服务器与此服务器进行时间同步,根据需要进行替换id和子网掩码即可
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
restrict 172.20.1.0 mask 255.255.255.192 nomodify notrap
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

2.3 开启服务

service ntpd start

2.4 设置ntp开机自启动

chkconfig ntpd on

2.5 开放访问端口权限

#之后还需要对ntp的默认端口123进行放行,这个一定要配,除非你关了防火墙,否则会报错,no server suitable for #synchronization found
iptables -I INPUT -p tcp --dport 123 -j ACCEPT
iptables -I INPUT -p udp --dport 123 -j ACCEPT

2.6 本服务器时间可以根据互联网时钟或者数据库手动调整

三 时钟客户端172.20.1.158实施

3.1 安装ntp

#检查是否已经安装,出现了红框中的就说明已经安装了
rpm -qa | grep ntp
#安装ntp
yum install ntp

3.2 配置

vi /etc/ntp.conf

记得把地址替换为你自己的那台时间服务器的地址

server 172.20.1.152
fudge 172.20.1.152 stratum 10

3.3 启动

service ntpd start

3.4 开机启动

chkconfig ntpd on

3.5 手动同步下测试

ntpdate -d 172.20.1.152
#显示以下内容代表成功
[root@host-172-20-1-158 tomcat9-llqt-7003]# ntpdate -d 172.20.1.152
25 Jun 09:58:57 ntpdate[13538]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1)
Looking for host 172.20.1.152 and service ntp
host found : 172.20.1.152
transmit(172.20.1.152)
receive(172.20.1.152)
transmit(172.20.1.152)
receive(172.20.1.152)
transmit(172.20.1.152)
receive(172.20.1.152)
transmit(172.20.1.152)
receive(172.20.1.152)
server 172.20.1.152, port 123
stratum 11, precision -24, leap 00, trust 000
refid [172.20.1.152], delay 0.02586, dispersion 0.00000
transmitted 4, in filter 4
reference time:    e660efe3.56e033ce  Sat, Jun 25 2022 10:22:27.339
originate timestamp: e660f01a.2e8a535d  Sat, Jun 25 2022 10:23:22.181
transmit timestamp:  e660ea67.47801f45  Sat, Jun 25 2022  9:59:03.279
filter delay:  0.02621  0.02591  0.02586  0.02589 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 1458.902 1458.902 1458.902 1458.902
         0.000000 0.000000 0.000000 0.000000
delay 0.02586, dispersion 0.00000
offset 1458.902283

25 Jun 09:59:03 ntpdate[13538]: step time server 172.20.1.152 offset 1458.902283 sec

3.6 查看同步状态

ntpq -p 
#when代表最近同步时间

3.7 创建定时任务,自动同步

#查看已有定时任务
crontab -l
#添加定时任务
crontab -e
#每天凌晨一点20,自动从172.20.1.152同步一次
20 1 * * * ntpdate 172.20.1.152

手工更改时钟服务器时间

date -s 17:12:40

持续显示系统时间
```linux
watch -n 1 "date +%T"
posted @ 2022-06-25 11:26  qiaoyizhi  阅读(2262)  评论(0编辑  收藏  举报