NTP时间对时服务

一. 为什么需要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

猜你喜欢

转载自www.cnblogs.com/jiawei2527/p/13173554.html