Linux服务器时间同步配置

时间:2023-03-10 06:28:17
Linux服务器时间同步配置

Linux服务器时间同步配置

以CentOS7 做时间服务器,其他服务器(Centos 6、RHEL7)同步该服务器时间

RHEL 7、CentOS 7 默认的网络时间协议 为Chrony

本教程时间服务器服务端主机名:CentOS7-Node3    IP:192.168.231.130

客户端1主机名:RHEL7-Node1

客户端2主机名:CentOS6-Node2

服务端:

先查询下chrony服务状态

[root@CentOS7-Node3 ~]# systemctl status chronyd
Unit chronyd.service could not be found.

如果查询没有该服务,可yum安装

[root@CentOS7-Node3 ~]# yum install -y chrony

启动chrony服务

[root@CentOS7-Node3 ~]# systemctl start chronyd

查询状态

[root@CentOS7-Node3 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-06-07 10:20:28 CST; 2s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 6031 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 6027 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 6029 (chronyd)
CGroup: /system.slice/chronyd.service
└─6029 /usr/sbin/chronyd Jun 07 10:20:28 CentOS7-Node3 systemd[1]: Starting NTP client/server...
Jun 07 10:20:28 CentOS7-Node3 chronyd[6029]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +...DEBUG)
Jun 07 10:20:28 CentOS7-Node3 chronyd[6029]: Initial frequency -29.914 ppm
Jun 07 10:20:28 CentOS7-Node3 systemd[1]: Started NTP client/server.
Jun 07 10:20:33 CentOS7-Node3 chronyd[6029]: Selected source 37.218.240.198
Jun 07 10:20:33 CentOS7-Node3 chronyd[6029]: System clock wrong by -3.020089 seconds, adjustment started
Jun 07 10:20:30 CentOS7-Node3 chronyd[6029]: System clock was stepped by -3.020089 seconds
Hint: Some lines were ellipsized, use -l to show in full.

作为时间服务器做如下配置:

 [root@CentOS7-Node3 ~]# vim /etc/chrony.conf
25 # Allow NTP client access from local network.
26 allow 192.168.231.0/24
27
28 # Serve time even if not synchronized to a time source.
29 local stratum 10

三台服务器时间,CentOS7-Node3为正常时间

Linux服务器时间同步配置

客户端配置:

客户端1:

将 server 0.rhel.pool.ntp.org iburst ---server 3.rhel.pool.ntp.org iburst 删除或注释

新增\修改配置:

server 192.168.231.130 iburt

allow 192.168.231.0/24

[root@RHEL7-Node1 ~]# vim /etc/chrony.conf
1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
3 #server 0.rhel.pool.ntp.org iburst
4 #server 1.rhel.pool.ntp.org iburst
5 #server 2.rhel.pool.ntp.org iburst
6 #server 3.rhel.pool.ntp.org iburst
7 server 192.168.231.130 iburst
8 # Ignore stratum in source selection.
9 stratumweight 0
10
11 # Record the rate at which the system clock gains/losses time.
12 driftfile /var/lib/chrony/drift
13
14 # Enable kernel RTC synchronization.
15 rtcsync
16
17 # In first three updates step the system clock instead of slew
18 # if the adjustment is larger than 10 seconds.
19 makestep 10 3
20
21 # Allow NTP client access from local network.
22 allow 192.168.231.0/24

修改完成后,保存,重启chrony服务即可生效

[root@RHEL7-Node1 ~]# systemctl restart chronyd

客户端2 CentOS6-Node2

CentOS 6 为NTP

执行 ntpdate 192.168.231.130

[root@CentOS6-Node2 ~]# ntpdate 192.168.231.130
7 Jun 16:59:34 ntpdate[1563]: the NTP socket is in use, exiting

若出现上述错误,将ntp服务停止,再执行ntpdate 192.168.231.130

[root@CentOS6-Node2 ~]# service ntpd stop
Shutting down ntpd: [ OK ]
[root@CentOS6-Node2 ~]# ntpdate 192.168.231.130
7 Jun 17:00:19 ntpdate[1578]: step time server 192.168.231.130 offset -13589998.9
54539 sec[root@CentOS6-Node2 ~]# service ntpd status
ntpd is stopped
[root@CentOS6-Node2 ~]# date
Thu Jun 7 17:00:37 CST 2018

随后在计划任务中添加时间同步

[root@CentOS6-Node2 ~]# crontab -e
0-59/10 * * * * /usr/sbin/ntpdate 192.168.231.130

10分钟同步一次

现在查看下三台服务器时间是一致的了

Linux服务器时间同步配置