多个Linux之间同步时间(搭建ntp服务器)

  |   0 评论   |   0 浏览

摘自:https://www.cnblogs.com/big-cousin/p/10833687.html

客户端:192.168.44.131

服务端:192.168.44.130

服务端配置

安装 ntp

yum install ntp -y

配置文件修改

vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
# 允许特定网段 能同步时间
restrict 192.168.44.0 mask 255.255.255.0 nomodify notrap
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 如果网络时间同步服务器不可用,就以本地时间为标准向客户端提供服务
server 127.127.1.0
Fudge 127.127.1.0 stratum 10
server 0.cn.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

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

启动ntp服务器

[root@localhost ~]# service ntpd start
Redirecting to /bin/systemctl start ntpd.service
[root@localhost ~]# service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2021-08-24 16:18:19 CST; 6s ago
  Process: 1900 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1901 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─1901 /usr/sbin/ntpd -u ntp:ntp -g

8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listen and drop on 1 v6wildcard :: UDP 123
8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listen normally on 2 lo 127.0.0.1 UDP 123
8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listen normally on 3 ens33 192.168.44.130 UDP 123
8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listen normally on 4 lo ::1 UDP 123
8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listen normally on 5 ens33 fe80::20:2d38:88df:1e71 UDP 123
8月 24 16:18:19 localhost.localdomain ntpd[1901]: Listening on routing socket on fd #22 for interface updates
8月 24 16:18:19 localhost.localdomain ntpd[1901]: 0.0.0.0 c016 06 restart
8月 24 16:18:19 localhost.localdomain ntpd[1901]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
8月 24 16:18:19 localhost.localdomain ntpd[1901]: 0.0.0.0 c011 01 freq_not_set
8月 24 16:18:20 localhost.localdomain ntpd[1901]: 0.0.0.0 c514 04 freq_mode
[root@localhost ~]# 

监听的123端口 UDP协议

客户端配置

执行命令 ntpdate 192.168.44.130

日期同步

[root@localhost ~]# ntpdate 192.168.44.130
24 Aug 16:23:09 ntpdate[1623]: adjust time server 192.168.44.130 offset -0.043357 sec
[root@localhost ~]# 

定时同步时间

crontab -e

*/5 * * * * /usr/sbin/ntpdate 131.xx.xx.xx

标题:多个Linux之间同步时间(搭建ntp服务器)
作者:gitsilence
地址:https://lacknb.cn/articles/2021/08/24/1629793436937.html