1.实验环境的搭建
需要两台虚拟机,我们创建两台虚拟机,创建完成之后,查看两台虚拟机的网络配置,配置完成之后,两台虚拟机互相ping一下,查看是否互通。
IP 192.168.79.136
IP 192.168.79.135
这样,我们的实验环境就搭建完成了。
查看主机时间(IP 192.168.79.135)
[root@localhost ~]# date
Sun Nov 17 10:21:25 PM CST 2024
[root@localhost ~]# timedatectl
Local time: Sun 2024-11-17 22:22:01 CST
Universal time: Sun 2024-11-17 14:22:01 UTC
RTC time: Sun 2024-11-17 14:22:01
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@localhost ~]#
修改时间
在系统中开启时间同步服务后无法用命令修改时间,如果要修改时间必须关闭时间同步服务。
[root@localhost ~]# date
Sun Nov 17 10:28:12 PM CST 2024
[root@localhost ~]# timedatectl set-time "08:20:00"
Failed to set time: Automatic time synchronization is enabled
[root@localhost ~]# systemctl stop chronyd.service
[root@localhost ~]# date
Sun Nov 17 10:33:08 PM CST 2024
[root@localhost ~]# timedatectl set-time "2025-08-20 10:07:00"
[root@localhost ~]# timedatectl
Local time: Wed 2025-08-20 10:07:15 CST
Universal time: Wed 2025-08-20 02:07:15 UTC
RTC time: Wed 2025-08-20 02:07:15
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
[root@localhost ~]#
查看时区
root@localhost ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Juba
Africa/Kampala
Africa/Khartoum
Africa/Kigali
Africa/Kinshasa
Africa/Lagos
Africa/Libreville
Africa/Lome
Africa/Luanda
Africa/Lubumbashi
Africa/Lusaka
Africa/Malabo
Africa/Maputo
Africa/Maseru
修改时区
[root@localhost ~]# timedatectl set-timezone Asia/Tokyo
[root@localhost ~]# timedatectl
Local time: Wed 2025-08-20 11:12:33 JST
Universal time: Wed 2025-08-20 02:12:33 UTC
RTC time: Wed 2025-08-20 02:12:33
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
[root@localhost ~]#
如何使用公共时间源
公共
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
注意:使用公共时间源是,必须确保你的主机能够联通网络。
[root@localhost ~]# ping ntp.aliyun.com
PING ntp.aliyun.com (203.107.6.88) 56(84) bytes of data.
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=1 ttl=128 time=46.3 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=2 ttl=128 time=45.8 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=3 ttl=128 time=47.1 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=4 ttl=128 time=45.7 ms
^C
--- ntp.aliyun.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 45.683/46.226/47.134/0.572 ms
[root@localhost ~]# vim /etc/chrony.conf
修改,保存退出,然后重启服务。
[root@localhost ~]# systemctl restart chronyd.sevice
[root@localhost ~]# head /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
# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
[root@localhost ~]#
搭建本地时间源
在时间服务器端,谁要把时间共享出去就在那台主机中设定(IP 192.168.79.136)
[root@localhost ~]# date
Sun Nov 17 11:11:38 PM CST 2024
[root@localhost ~]# systemctl stop chronyd.service
[root@localhost ~]# timedatectl set-time "2025-10-07 08:20:00:
> ^C
[root@localhost ~]# timedatectl set-time "2025-10-07 08:20:00"
[root@localhost ~]# date
Tue Oct 7 08:20:07 AM CST 2025
[root@localhost ~]#
[root@localhost ~]# vim /etc/chrony.conf
[root@localhost ~]#
修改,然后保存退出,然后重启服务。
[root@localhost ~]# systemctl restart chronyd.service
[root@localhost ~]#
在客户端(IP 192.168.79.135)
重启服务
[root@localhost ~]# systemctl restart chronyd.service
[root@localhost ~]#
如果出现错误,建议查看防火墙是否关闭,如果开着,请关闭防火墙在进行时间同步服务。