前言:
在centos6的時候我們基本使用的是ntp服務用來做時間同步,但是在centos7后推薦是chrony作為時間同步器的服務端使用,避免的ntp在centos7支持的不是很友好 。
什么是chrony?
第一部分的第3條說明對比關系,詳見centos6和centos7的對比,連接地址:https://www.cnblogs.com/liych/p/11741632.html
在centos7上可以用它做時間服務器使用,是默認支持的,也是更新后centos7默認的時間服務器,另外還有就是,它作為網絡時間協議的客戶機/服務器,此程序保持計算機時鍾的准確性。它是專門為支持斷斷續續的互聯網連接而設計的,但它在永久連接的環境中也能很好地工作。它還可以使用硬件參考時鍾、系統實時時鍾或手動輸入作為時間參考。至此,推薦使用。
chrony有2個命令,詳見如下:
chronyd:是守護進程,主要用於調整內核中運行的系統時間和時間服務器同步。它確定計算機增減時間的比率,並對此進行調整補償。
chronyc:提供一個用戶界面,用於監控性能並進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一台不同的遠程計算機上工作。
開始部署chrony環境
1.環境說明
實現目的:需要兩台或集群內的多台設備時間同步一致。
這里准備兩台服務器:192.168.152.129 (bogon)做服務器端; 192.168.152.132 (liych-01)做客戶端。也可以多台集群使用。
[root@bogon ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
2.關閉防火牆
[root@bogon ~]# systemctl stop firewalld [root@bogon ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) since 四 2019-09-19 03:12:44 CST; 17s ago Docs: man:firewalld(1)
3.關閉selinux
[root@bogon ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config [root@bogon ~]# sestatus SELinux status: disabled
4.安裝chrony
這里注意的是需要把需要同步時間服務器均安裝chrony服務。
版本號:chrony.x86_64 0:3.4-1.el7
[root@bogon ~]# yum install chrony -y
4.1啟動服務
[root@bogon ~]# systemctl start chronyd [root@bogon ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since 四 2019-09-19 03:19:49 CST; 1 months 7 days ago
4.2更新配置文件
這里修改的是服務器端的配置文件,客戶端不需要修改。
時間服務器獲取地址:https://www.ntppool.org/zone/asia
[root@bogon ~]# cp /etc/chrony.conf{,.backup} [root@bogon ~]# cat > /etc/chrony.conf <<EOF server cn.pool.ntp.org server 0.asia.pool.ntp.org server 1.asia.pool.ntp.org server 2.asia.pool.ntp.org server 3.asia.pool.ntp.org server ntp1.aliyun.com server time1.aliyun.com stratumweight 0 driftfile /var/lib/chrony/drift makestep 1.0 3 rtcsync allow 192.168.152.0/24 bindcmdaddress 127.0.0.1 bindcmdaddress ::1 local stratum 10 keyfile /etc/chrony.keys commandkey 1 generatecommandkey noclientlog logchange 0.5 logdir /var/log/chrony EOF
4.3配置文件簡單介紹
可以通過"man chrony.conf"了解主配置文件的配置格式,需要注意的是增加allow后才會生效。
server | “必須以server”格式使用,添加的是時鍾服務器,可以是多個。 |
stratumweight 0 | 設置當chronyd從可用源中選擇同步源時,每個層應該添加多少距離到同步距離。默認設置為0,讓chronyd在選擇源時忽略源的層級。 |
driftfile /var/lib/chrony/drift | 根據實際時間計算出服務器增減時間的比率,然后記錄到一個文件中,在系統重啟后為系統做出最佳時間補償調整。 |
makestep 1.0 3 chronyd | 將根據需求通過減慢或加速時鍾,使得系統逐步糾正所有時間偏差 |
allow 192.168.152.0/24 deny 192.168.152.0/24 |
指定一台主機、子網,或者網絡以允許或拒絕NTP連接到時鍾服務器的機器 |
local stratum 10 | 這個功能開啟后,本機不去同步別人的時間到本機 |
logdir /var/log/chrony |
記錄日志 |
4.4重啟並檢查服務端口123,323
[root@bogon chrony]# systemctl restart chronyd [root@bogon chrony]# systemctl enable chronyd [root@bogon chrony]# netstat -lntup |grep chronyd udp 0 0 0.0.0.0:123 0.0.0.0:* 4078/chronyd udp 0 0 127.0.0.1:323 0.0.0.0:* 4078/chronyd udp6 0 0 ::1:323 :::* 4078/chronyd
5.客戶端(單台)啟用時間同步服務
單台配置很簡單,請繼續往下看吧。
5.1客戶端啟用時間同步服務
這里需要注意的是 時區為:Time zone: Asia/Shanghai (CST, +0800)然后執行下面法一同步時間。
法一:推薦使用 [root@liych-01 ~]# timedatectl set-timezone Asia/Shanghai;chronyc -a makestep [root@liych-01 ~]# yum install ntpdate -y;ntpdate 192.168.152.129 27 Oct 13:41:22 ntpdate[12018]: step time server 192.168.152.129 offset 62.490796 sec 法二: [root@liych-01 ~]# echo "server 192.168.152.129 iburst " >>/etc/chrony.conf [root@liych-01 ~]# systemctl restart chronyd [root@liych-01 ~]# systemctl status chronyd
6.客戶端(集群)啟用時間同步服務器
本機設備很舊了,使用多台的時候總是遲鈍,故此此處采用132一台做測試,多台就在host添加主機即可。
[root@bogon ~]# cat /etc/ansible/hosts [ntpcli] 192.168.152.132 #192.168.152.133 #192.168.152.134
命令集合:
[root@bogon ~]# ansible all -m ping [root@bogon ~]# echo "server 192.168.152.129 iburst " >/root/chrony.conf [root@bogon ~]# ansible ntpcli -m copy -a "src=/root/chrony.conf dest=/etc/" [root@bogon ~]# ansible ntpcli -m shell -a "systemctl start chronyd.service" [root@bogon ~]# ansible ntpcli -m shell -a "systemctl status chronyd.service" [root@bogon ~]# ansible ntpcli -m shell -a "ntpdate 192.168.152.129" [root@bogon ~]# ansible ntpcli -m shell -a "date" [root@bogon ~]# ansible ntpcli -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate 192.168.152.129 &>/dev/null'" [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l"
操作示例:
[root@bogon ~]# ansible all -m ping 192.168.152.132 | SUCCESS => { "changed": false, "ping": "pong" } [root@bogon ~]# echo "server 192.168.152.129 iburst " >/root/chrony.conf [root@bogon ~]# ansible ntpcli -m copy -a "src=/root/chrony.conf dest=/etc/" 192.168.152.132 | SUCCESS => { "changed": true, "checksum": "3ccbe3a0fc44dd0ce06623653f5a9c940f453bdb", "dest": "/etc/chrony.conf", "gid": 0, "group": "root", "md5sum": "bb305fff45f35fda88ed9ad01231a25c", "mode": "0644", "owner": "root", "size": 31, "src": "/root/.ansible/tmp/ansible-tmp-1572158118.63-84760854509278/source", "state": "file", "uid": 0 } [root@bogon ~]# ansible ntpcli -m shell -a "systemctl start chronyd.service" 192.168.152.132 | SUCCESS | rc=0 >> [root@bogon ~]# ansible ntpcli -m shell -a "systemctl status chronyd.service" 192.168.152.132 | SUCCESS | rc=0 >> ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since 日 2019-10-27 13:43:03 CST; 53min ago Docs: man:chronyd(8) man:chrony.conf(5) Process: 12034 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 12031 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 12033 (chronyd) CGroup: /system.slice/chronyd.service └─12033 /usr/sbin/chronyd 10月 27 13:43:03 liych-01 systemd[1]: Stopped NTP client/server. 10月 27 13:43:03 liych-01 systemd[1]: Starting NTP client/server... 10月 27 13:43:03 liych-01 chronyd[12033]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG) 10月 27 13:43:03 liych-01 chronyd[12033]: Initial frequency -5.115 ppm 10月 27 13:43:03 liych-01 systemd[1]: Started NTP client/server. 10月 27 13:43:08 liych-01 chronyd[12033]: Selected source 192.168.152.129 10月 27 13:43:08 liych-01 chronyd[12033]: System clock wrong by -2.747341 seconds, adjustment started 10月 27 13:44:14 liych-01 chronyd[12033]: System clock wrong by 2.501240 seconds, adjustment started 10月 27 13:45:19 liych-01 chronyd[12033]: System clock wrong by -2.304266 seconds, adjustment started 10月 27 13:45:33 liych-01 chronyd[12033]: System clock was stepped by -0.921109 seconds [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l" 192.168.152.132 | SUCCESS | rc=0 >> [root@bogon ~]# ansible ntpcli -m shell -a "ntpdate 192.168.152.129" 192.168.152.132 | SUCCESS | rc=0 >> 27 Oct 14:37:35 ntpdate[12702]: adjust time server 192.168.152.129 offset -0.000112 sec [root@bogon ~]# ansible ntpcli -m shell -a "date" 192.168.152.132 | SUCCESS | rc=0 >> 2019年 10月 27日 星期日 14:38:53 CST [root@bogon ~]# ansible ntpcli -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate 192.168.152.129 &>/dev/null'" 192.168.152.132 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "time sync" ] } [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l" 192.168.152.132 | SUCCESS | rc=0 >> #Ansible: time sync */5 * * * * /usr/sbin/ntpdate 192.168.152.129 &>/dev/null
7.防火牆設置
因NTP使用123/UDP端口協議,所以允許NTP服務即可。
[root@bogon ~]# firewall-cmd --add-service=ntp --permanent
[root@bogon ~]# firewall-cmd --reload
8.常用的服務器命令
8.1查看當前系統時區並設置時區常用命令
[root@liych-01 ~]# timedatectl #查看當前系統時區 [root@liych-01 ~]# timedatectl list-timezones | grep -E "Asia/S.*" [root@liych-01 ~]# timedatectl set-timezone Asia/Shanghai #設置當前系統為Asia/Shanghai上海時區 [root@liych-01 ~]# chronyc -a makestep #強制同步下系統時鍾
8.2查看系統同步時間
[root@liych-01 ~]# chronyc sources -v #查看時間同步源 [root@liych-01 ~]# chronyc sourcestats -v #查看時間同步源狀態 [root@liych-01 ~]# timedatectl set-local-rtc 1 #設置硬件時間,硬件時間默認為UTC [root@liych-01 ~]# timedatectl set-ntp yes #啟用NTP時間同步 [root@liych-01 ~]# chronyc tracking #校准同步時間服務器
chronyc參數 | 涵義 |
tracking(最為常用,其他的並不重要) | 顯示系統時間信息 |
activity | 顯示在線狀態 |
add sever | 添加時間服務器 |
delete | 移除時間服務器 |
clients | 查看報告 |
settime | 設置守護進程 |
accheck | 檢查特定主機是否可以用 |
參考鏈接地址:
https://wiki.archlinux.org/index.php/Chrony https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-using_chrony