一. 為什么需要NTP
• 為了讓服務器更安全
• 為了讓時間同步更准確
二. 時間同步軟件
NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的各個計算機時間同步的一種協議。它的用途是把計算機的時鍾同步到世界協調時UTC,其精度在局域網內可達0.1ms,在互聯網上絕大多數的地方其精度可以達到1-50ms。
NTP服務器就是利用NTP協議提供時間同步服務的
• NTP(CentOS6)
• Chrony (CentOS7)
二. 部署NTP服務器
1. 安裝NTP服務
[root@node01 ~]# yum -y install ntp
2. 修改NTP配置文件
[root@node01 ~]# mv /etc/ntp.conf /etc/ntp.conf.ori
[root@node01 ~]# cat >/etc/ntp.conf << EOF
> driftfile /var/lib/ntp/drift
>
> restrict default nomodify
> restrict 172.16.1.0/24 192.168.10.0/24
>
> restrict ::1
>
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> server ntp.aliyun.com iburst
> EOF
注:其中nomodify(表示客戶端可以同步) notrap nopeer noquery(表示安全,開啟后不能使用ntpstat ntpq -p這兩個命令)
3. 關閉chronyd
[root@node01 ~]# systemctl stop chronyd
[root@node01 ~]# systemctl disable chronyd
4. 啟動NTP服務
[root@node01 ~]# systemctl start ntpd.service
[root@node01 ~]# systemctl status ntpd.service
5. 測試NTP服務
[root@node01 ~]# ntpq -p #查看NTP服務器和哪些上游服務器同步時間
remote refid st t when poll reach delay offset jitter
==============================================================================
*203.107.6.88 10.137.38.86 2 u 4 64 3 32.120 38.238 1.300
st:即stratum階層,值越小表示ntp serve的精准度越高
when:幾秒前曾做過時間同步更新的操作
Poll表示,每隔多少毫秒與ntp server同步一次
reach:已經向上層NTP服務器要求更新的次數
delay:網絡傳輸過程鍾延遲的時間
offset:時間補償的結果
jitter:Linux系統時間與BIOS硬件時間的差異時間
==============================================================================
[root@node01 ~]# ntpstat # 顯示本機上一次和服務器同步時間的情況
synchronised to NTP server (203.107.6.88) at stratum 3
time correct to within 1005 ms
polling server every 64 s
四. 部署Chronyc服務器
1. 查看是否安裝chrony服務
[root@node01 ~]# rpm -qa chrony
chrony-3.4-1.el7.x86_64
2. 修改chronyc配置文件
[root@node01 ~]# vim /etc/chrony.conf
3. 運行程序
[root@node01 ~]# systemctl start chronyd
[root@node01 ~]# systemctl status chronyd
4. 測試
[root@node01 ~]# systemctl stop ntpd
[root@node01 ~]# chronyc sources -v
210 Number of sources = 2
.-- 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 167 26 +2845us[+2845us] +/- 21ms
^* 203.107.6.88 2 6 77 28 -2116us[-5914us] +/- 29ms
客戶端
[root@web01 ~]# ntpdate 10.0.0.61
21 Jun 18:47:08 ntpdate[6529]: step time server 10.0.0.61 offset 63.273886 sec
6. 幾個命令
chronyc sources -v - 查看時間同步源
chronyc sourcestats -v - 查看時間同步源狀態
imedatectl set-ntp yes - 啟用NTP時間同步
cronyc tracking - 顯示系統時間信息
chronyc accheck - 檢查NTP訪問是否對特定主機可用
chronyc activity - 該命令會顯示有多少NTP源在線/離線
chronyc add server - 手動添加一台新的NTP服務器。
chronyc clients - 在客戶端報告已訪問到服務器
chronyc delete - 手動移除NTP服務器或對等服務器
chronyc settime - 手動設置守護進程時間
timedatectl set-local-rtc 1 設置硬件時間,硬件時間默認為UTC