时间同步服务器

一.时间同步服务的使用

1.系统时间及时区的管理

1.1.查看系统时间

命令:timedatectl

[root@wzzh ~]# timedatectl
               Local time: 六 2024-11-16 19:38:43 CST
           Universal time: 六 2024-11-16 11:38:43 UTC
                 RTC time: 六 2024-11-16 11:38:44
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

timedatectl命令的输出信息解释

‌Local time‌:本地时间。

‌Universal time‌:世界标准时间(UTC),东一区时间。

‌RTC time‌:硬件时钟的时间。

‌Time zone:时区。

‌System clock synchronized‌:系统时钟同步状态。

‌NTP service‌:NTP时钟同步服务

2.修改系统时间

命令:timedatectl set-time "时间"(注:修改系统时间必须关闭chronyd服务;命令是systemctl stop chronyd.server)

[root@wzzh ~]# systemctl stop chronyd.service
[root@wzzh ~]# timedatectl
               Local time: 六 2024-11-16 22:00:04 CST
           Universal time: 六 2024-11-16 14:00:04 UTC
                 RTC time: 六 2024-11-16 14:00:04
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

3.显示系统所有时区

命令:timedatectl list-timezones

[root@wzzh ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
#以下还有

4.修改时区

命令:timedatectl set-timezone “时区名”

[root@wzzh ~]# timedatectl set-timezone "Africa/Accra"
[root@wzzh ~]# timedatectl
               Local time: 六 2024-11-16 14:01:35 GMT
           Universal time: 六 2024-11-16 14:01:35 UTC
                 RTC time: 六 2024-11-16 14:01:35
                Time zone: Africa/Accra (GMT, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

5.设置及关闭rtc时间

设置rtc命令:timedatectl set-local-rtc 1

[root@wzzh ~]# timedatectl set-local-rtc 1
[root@wzzh ~]# timedatectl
               Local time: 六 2024-11-16 14:02:12 GMT
           Universal time: 六 2024-11-16 14:02:12 UTC
                 RTC time: 六 2024-11-16 14:02:12
                Time zone: Africa/Accra (GMT, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: yes

Warning: The system is configured to read the RTC time in the local time zone.
         This mode cannot be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

关闭rtc命令:timedatectl set-local-rtc 0

[root@wzzh ~]# timedatectl set-local-rtc 0
[root@wzzh ~]# timedatectl
               Local time: 六 2024-11-16 14:03:13 GMT
           Universal time: 六 2024-11-16 14:03:13 UTC
                 RTC time: 六 2024-11-16 14:03:14
                Time zone: Africa/Accra (GMT, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

二.使用公共ntp时间到本机


2.1公共ntp地址


1. www.ntppool.org : 项目是一个提供可靠易用的 NTP 服务的虚拟集群, [0-3].asia.pool.ntp.org
2. 阿里云公共 NTP 服务器
Unix/linux类: ntp.aliyun.com,ntp1-7.aliyun.com
windows类: time.pool.aliyun.com
国家授时中心服务器:ntp.ntsc.ac.cn


2.2配置公共ntp地址


2.2.1.配置文件

[root@wzzh ~]# cat /etc/chrony.conf | head -5
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 2.rhel.pool.ntp.org iburst

# Use NTP servers from DHCP.

2.2.2使用ntp.aliyun.com地址

[root@wzzh ~]# vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst

使用公共ntp地址后要重启chronyd.service服务;命令:systemctl restart chronyd.service

2.2.3.显示同步信息

命令:chronyc sources -v

[root@wzzh ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6    17     6   -354us[-2520us] +/-   42ms

M:时间源模式 ^表示服务器,=表示对等方,#表示本地连接的参考时钟
S:指源的状态
表示chronyd当前已经同步到的源。
+ 表示可接受的信号源,与选定的信号源组合在一起。
表示被合并算法排除的可接受源
? 指已失去连接性或者其数据包未通过所有测试的源。
x 表示chronyd认为时虚假行情的时钟,即标记该时间与其他多数时间不一致
~ 表示时间似乎具有太多可变性
Name/IP address:显示源的名称或IP地址
Stratum:显示时间来源的层
Poll:显示轮询源的速率
Reach:显示源的可达性寄存器以八进制数字打印
LastRx:显示多长时间前从来源接收到了最后一个好的样本
Last sample:此列显示上次测量时本地时钟与源之间的偏移

三.时间同步服务器的搭建

需要准备两台虚拟机,一个为客户端,一个为服务端以IP:192.168.94.100(客户端);192.168.94.200(服务端为例)

3.1设置查看ip

客户端:

[xiaohua@zh ~]$ ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:86:42:f1 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 192.168.94.100/24 brd 192.168.94.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::6e05:cd33:54b8:8e7e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

服务端:

[root@wzzh ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:e8:c3 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 192.168.94.200/24 brd 192.168.94.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::fc6f:15f4:6536:125/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

3.2修改服务端系统时间

[root@wzzh ~]# systemctl stop chronyd.service
[root@wzzh ~]# timedatectl set-time "2024-11-17 20:00:00"
[root@wzzh ~]# timedatectl
               Local time: 日 2024-11-17 20:00:05 GMT
           Universal time: 日 2024-11-17 20:00:05 UTC
                 RTC time: 日 2024-11-17 20:00:05
                Time zone: Africa/Accra (GMT, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

3.3修改服务端配置文件

allow 0.0.0.0/0表示全网段可以连接

local stratum 10 指即使server指令中时间服务器不可用 也允许将本地时间作为标准时间授时给其它客户端 vim /etc/chrony.conf

 25 # Allow NTP client access from local network.
 26 allow 0.0.0.0/0
 27 
 28 # Serve time even if not synchronized to a time source.
 29 local stratum 10

3.4修改客户端配置文件

将ntp地址改为客户端ip   vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 192.168.94.200 iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

3.5 重启chronyd服务并且显示同步信息

[xiaohua@zh ~]$ systemctl restart chronyd.service
[xiaohua@zh ~]$ chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.94.200                3   6    17     1    -43us[  -53us] +/-   41ms

左下方显示^*表示为连接成功,并且验证IP为服务端IP

你可能感兴趣的:(服务器,linux,运维)