2021-05-12 14:32:11
Linux基礎入門教學-chrony時間同步服務
Chrony時間同步
NTP(Network Time Protocol,網路時間協定)是用來使網路中的各個計算機時間同步的一種協定.它的用於是把計算機的時鐘同步到世界協調時UTC,其精度在區域網內可達0.1ms,在網路網上絕大多數的地方其精度可以達到1-50ms.
1.chrony服務部署
//檢視一下由哪個包提供的
[root@ntp-server ~]# yum provides chronyd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
chrony-3.1-2.el7.x86_64 : An NTP client/server
Repo : dvd
Matched from:
Filename : /usr/sbin/chronyd
//安裝chrony服務
[root@ntp-server ~]# yum install chrony
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
dvd | 4.1 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.1-2.el7 will be installed
--> Processing Dependency: libseccomp.so.2()(64bit) for package: chrony-3.1-2.el7.x86_64
--> Running transaction check
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================
Package Arch Version Repository Size
==========================================================================================
Installing:
chrony x86_64 3.1-2.el7 dvd 237 k
Installing for dependencies:
libseccomp x86_64 2.3.1-3.el7 dvd 56 k
Transaction Summary
==========================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 293 k
Installed size: 761 k
Is this ok [y/d/N]: y
Downloading packages:
------------------------------------------------------------------------------------------
Total 1.6 MB/s | 293 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libseccomp-2.3.1-3.el7.x86_64 1/2
Installing : chrony-3.1-2.el7.x86_64 2/2
Verifying : chrony-3.1-2.el7.x86_64 1/2
Verifying : libseccomp-2.3.1-3.el7.x86_64 2/2
Installed:
chrony.x86_64 0:3.1-2.el7
Dependency Installed:
libseccomp.x86_64 0:2.3.1-3.el7
Complete!
//啟動服務
[root@ntp-server ~]# systemctl start chrony
chrony-dnssrv@ chronyd.service chrony-wait.service
[root@ntp-server ~]# systemctl start chronyd
chrony服務相關命令
引數 | 解釋 |
---|---|
list-timezones | 列出可用時區資訊 |
set-timezone | 設定時區 |
set-ntp | 啟用NTP |
status | 檢視狀態 |
[root@ntp-server ~]# timedatectl
Local time: Tue 2018-08-21 14:02:35 CST
Universal time: Tue 2018-08-21 06:02:35 UTC
RTC time: Tue 2018-08-21 06:02:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@ntp-server ~]# timedatectl
list-timezones set-ntp set-timezone
set-local-rtc set-time status
[root@ntp-server ~]# timedatectl list-timezones | grep -i shanghai
Asia/Shanghai
//關閉NTP
[root@ntp-server ~]# timedatectl set-ntp false
[root@ntp-server ~]# timedatectl
Local time: Tue 2018-08-21 14:15:36 CST
Universal time: Tue 2018-08-21 06:15:36 UTC
RTC time: Tue 2018-08-21 06:15:36
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
//啟用NTP
[root@ntp-server ~]# timedatectl set-ntp true
[root@ntp-server ~]# timedatectl
Local time: Tue 2018-08-21 14:16:02 CST
Universal time: Tue 2018-08-21 06:16:02 UTC
RTC time: Tue 2018-08-21 06:16:02
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
//檢視一下ntpdate由哪個包提供的
[root@ntp-server ~]# yum provides ntpdate
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
ntpdate-4.2.6p5-25.el7_3.2.x86_64 : Utility to set the date and time via NTP
Repo : dvd
//如果沒有請安裝ntpdate
[root@ntp-server ~]# yum install ntpdate.x86_64
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-25.el7_3.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================
Package Arch Version Repository Size
==========================================================================================
Installing:
ntpdate x86_64 4.2.6p5-25.el7_3.2 dvd 86 k
Transaction Summary
==========================================================================================
Install 1 Package
Total download size: 86 k
Installed size: 121 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ntpdate-4.2.6p5-25.el7_3.2.x86_64 1/1
Verifying : ntpdate-4.2.6p5-25.el7_3.2.x86_64 1/1
Installed:
ntpdate.x86_64 0:4.2.6p5-25.el7_3.2
Complete!
2.設定時間同步伺服器
[root@ntp-server ~]# vim /etc/chrony.conf
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst
allow 172.16.10.0/24
//驗證伺服器端是否能同步時間
[root@ntp-server ~]# chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- 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
===============================================================================
^* 120.25.115.20 2 6 17 22 -117us[ -948us] +/- 24ms
//看一下伺服器時間
[root@ntp-server ~]# date
Tue Aug 21 15:31:29 CST 2018
//看一下用戶端時間 時間不對需要同步
[root@ntp-client ~]# date
Fri Aug 20 14:10:28 CST 2010
//ntp伺服器地址
ens35: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.10.20 netmask 255.255.255.0 broadcast 172.16.10.255
inet6 fe80::da6c:394e:1ef7:c236 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:6e:66:65 txqueuelen 1000 (Ethernet)
RX packets 6 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 1614 (1.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
//用戶端同步,時間已經同步正確
[root@ntp-client ~]# ntpdate 172.16.10.20
21 Aug 15:35:10 ntpdate[2421]: step time server 172.16.10.20 offset 252552056.617240 sec
You have new mail in /var/spool/mail/root
[root@ntp-client ~]# date
Tue Aug 21 15:35:15 CST 2018
//第一種方式設定定時任務
[root@ntp-client ~]# which ntpdate
/usr/sbin/ntpdate
[root@ntp-client ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 172.16.10.20 &>/dev/null
//第二種方式用戶端編輯組態檔伺服器指向172.168.10.20
[root@ntp-client ~]# vim /etc/chrony.conf
server 172.16.10.20 iburst
//啟動服務檢視服務執行狀態
[root@ntp-client ~]# systemctl restart chronyd
[root@ntp-client ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-08-21 15:52:11 CST; 6s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 3012 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 3009 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 3011 (chronyd)
Tasks: 1
CGroup: /system.slice/chronyd.service
└─3011 /usr/sbin/chronyd
Aug 21 15:52:10 ntp-client systemd[1]: Starting NTP client/server...
Aug 21 15:52:11 ntp-client chronyd[3011]: chronyd version 3.2 starting (+CMDMON +NTP ...G)
Aug 21 15:52:11 ntp-client chronyd[3011]: Frequency -37.840 +/- 1000000.000 ppm read ...ft
Aug 21 15:52:11 ntp-client systemd[1]: Started NTP client/server.
Aug 21 15:52:15 ntp-client chronyd[3011]: Selected source 172.16.10.20
Hint: Some lines were ellipsized, use -l to show in full.
//差不多一分鐘同步了
[root@ntp-client ~]# date
Tue Aug 10 08:32:32 CST 2010
[root@ntp-client ~]# date
Tue Aug 10 08:32:33 CST 2010
[root@ntp-client ~]# date
Tue Aug 21 16:04:24 CST 2018
[root@ntp-client ~]# date
Tue Aug 21 16:04:25 CST 2018
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-09/154061.htm
相關文章