简介
NTP服务(Network Time Protocol)是用来使计算机时间同步化的一种协议,ntpd是ntp-daemon(ntp服务)的简写。
搭建ntpd
实验IP | 说明 |
---|---|
192.168.0.123 | 本地服务端 |
服务端配置
[root@localhost ~]# egrep -v '^$|^#' /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict -6 default nomodify notrap nopeer noquery
restrict 127.0.0.1 #允许本机时间同步请求
restrict -6 ::1 #不允许本机IPv6的时间同步请求
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap #允许同一局域网内主机的时间同步请求
server 192.168.0.123 perfer #优先选择的上级时间服务器
server 127.127.1.0 #同步上级时间不成功时,使用本机时间
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
客户端配置
[root@localhost ~]# egrep -v '^$|^#' /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict -6 default nomodify notrap nopeer noquery
restrict 127.0.0.1 #允许本机时间同步请求
restrict -6 ::1 #不允许本机IPv6的时间同步请求
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap #允许同一局域网内主机的时间同步请求
server 192.168.0.123 perfer #优先选择的上级时间服务器
server 127.127.1.0 #同步上级时间不成功时,使用本机时间
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
- 列表内容
同步状态检查
下面介绍下查看ntpserver状态的两条命令及其差别。
1. ntpstat
ntpstat查看时间同步状态,一般需要5-10分钟后才能成功连接和同步。
刚启动的时候,一般是:
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后:
# ntpstat
synchronised to NTP server (192.168.0.123) at stratum 4
time correct to within 1192 ms
polling server every 64 s
2. ntpq -p
查看时间同步服务器状态。
注意:NTP服务端重启后,客户机要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。等待的时间可以通过命令 watch ntpq -p来监控。
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
=======================================================
*192.168.0.123 1 u 107 128 377 397.503 -24.042 0.538
说明:
- * 表示目前使用的ntp server,这里选择的192.168.0.123;
- st:即stratum阶层,值越小表示ntp serve的精准度越高;
- when:几秒前曾做过时间同步更新的操作;
- poll表示,每隔多少毫秒与ntp server同步一次;
- reach:已经向上层NTP服务器要求更新的次数;
- delay:网络传输过程钟延迟的时间;
- offset:时间补偿的结果;
- jitter:Linux系统时间与BIOS硬件时间的差异时间
将时间写入bios
hwclock --systohc #将系统时间同步到硬件时间 system to hardware clock