linux基础协议之NTP(网络时间同步协议)的配置

一、 时间服务器的作用

  1. 设备集群需要有一个统一的时间,来确保所执行的操作的时序正确。
  2. 例如服务器集群进行文件创建、数据库处理等操作的时间。

二、时区管理

  1. 查看当前时区:timedatectl
[root@Tyson'sComputer ~]# timedatectl
      Local time: Sat 2019-02-16 23:07:46 CST
  Universal time: Sat 2019-02-16 15:07:46 UTC
        RTC time: Sat 2019-02-16 15:07:45
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
  1. 查看当前可用时区:timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
...
  1. 设置时区:timedatectl set-timezone Asia/Shanghai

三、配置ntp服务

  • 最终目的:将我们的一台主机配置成ntp服务器,同网段的其他主机可以通过ntpdate -u host-addr命令以ntp服务器的时间来进行客户端的时间同步。
3.1 服务端
  1. 工具安装
  • 我们需要先安装ntp服务和ntpdate工具:yum install ntp ntpdate -y
  • 即使是作为服务端的主机,在必要时刻也是需要向公用的ntp服务器进行时间同步的。
  1. 进行ntp服务文件配置
  • vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
logfile /var/log/ntpd.log #日志目录

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1
restrict ::1
restrict 172.16.199.0 mask 255.255.255.0 nomodify notrap #授权下述几个网段上所有的机器允许从ntp服务器上查询和同步时间
restrict 172.16.200.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.210.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.220.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.230.0 mask 255.255.255.0 nomodify notrap

server 0.cn.pool.ntp.org iburst #增加下述几个时间服务器列表
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
server 172.16.199.200 iburst #172.16.199.200是本地ntp服务器的IP地址,这两行内容表示当外部时间不可用时,使用本地时间

fudge 127.0.0.1 stratum 10

restrict 0.cn.pool.ntp.org nomodify notrap noquery #下述三行表示允许上层服务器修改本机时间
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
  1. 初始化用ntp服务
  • 使服务端服务ntp的守护进程ntpd生效:systemctl enable ntpd
  • 使客户端工具ntpdate工具生效:systemctl enable ntpdate
  • 检查:systemctl is-enabled ntpd
  1. 启用ntp服务前进行时间同步
[root@Tyson'sComputer ~]# ntpdate -u 1.cn.pool.ntp.org
16 Feb 23:18:16 ntpdate[10171]: adjust time server 173.255.246.13 offset -0.014712 sec
  • 若本机与上层服务器的时间相差过大,无法自动同步,需要进行手动设置。

5.启用ntp服务和ntpdate工具

[root@slave ~]# systemctl start ntpdate
[root@slave ~]# systemctl start ntpd
[root@slave ~]# ps -ef|grep ntp       
root      1517  1411  0 11:22 pts/1    00:00:00 ping 1.cn.pool.ntp.org
ntp       1681     1  0 11:26 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root      1683  1608  0 11:26 pts/2    00:00:00 grep --color=auto ntp
  1. 设置防火墙,放行ntp协议请求

  2. 设置硬件时间

  • linux的时间分为系统时间和硬件时间。
  • 系统时间,通常在开机时复制硬件时间,之后独立运行并保存了时间、时区和夏令时设置。通过date命令设置。
  • 硬件时间(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。通过hwclock命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w
  • 同步前需要查看守护进程ntpd状态和测试ntp上层服务器的连通性
[root@Tyson'sComputer ~]#  ss -tlunp | grep ntp
udp    UNCONN     0      0      172.16.230.41:123                   *:*                   users:(("ntpd",pid=10628,fd=20))
udp    UNCONN     0      0      192.168.1.101:123                   *:*                   users:(("ntpd",pid=10628,fd=19))
udp    UNCONN     0      0      127.0.0.1:123                   *:*                   users:(("ntpd",pid=10628,fd=18))
udp    UNCONN     0      0         *:123                   *:*                   users:(("ntpd",pid=10628,fd=16))
udp    UNCONN     0      0      fe80::3eae:dcdd:5ed8:5736%ens37:123                  :::*                   users:(("ntpd",pid=10628,fd=22))
udp    UNCONN     0      0       ::1:123                  :::*                   users:(("ntpd",pid=10628,fd=21))
udp    UNCONN     0      0        :::123                  :::*                   users:(("ntpd",pid=10628,fd=17))

[root@Tyson'sComputer ~]# ntpq -p 
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp6.flashdance 192.36.143.234   2 u   23   64    3  313.600   15.809   4.556
*biisoni.miuku.n 204.123.2.72     2 u   26   64    3  158.215    8.264   3.032
+ntp1.ams1.nl.le 130.133.1.10     2 u   25   64    3  217.083    9.420  83.968
 85.199.214.100  .GPS.            1 u   15   64    1  260.569   30.174   1.939
 172.16.230.77   .INIT.          16 u    -   64    0    0.000    0.000   0.000
  • remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
  • refid:参考上一层ntp主机地址
  • st:stratum阶层
  • when:多少秒前曾经同步过时间
  • poll:下次更新在多少秒后
  • reach:已经向上层ntp服务器要求更新的次数
  • delay:网络延迟
  • offset:时间补偿
  • jitter:系统时间与bios时间差
3.2 客户端
  1. 安装ntp服务和ntpdate工具:yum install ntp ntpdate -y
  2. 配置用于同步的服务器:echo "server 172.16.230.77" >/etc/ntp.conf
  3. 重启服务以使配置生效:systemctl restart ntpd
  4. 定时执行:crontab -e
0 0 * * * /usr/sbin/ntpdate -u 172.16.230.77;hwclock -w
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值