centos7部署ntp服务

1、NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:

    直接同步:使用ntpdate命令,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,该命令多用于配置时钟同步服务时第一次同步时间时使用。
    平滑同步:使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。

2、 环境情况

主机类型主机IP作用
ntpd服务器192.168.5.136与外部公共ntpd同步标准时间
ntpd客户端192.168.5.135与ntpd服务器同步时间

3、 检查服务是否安装

    检查ntp包是否安装

# rpm -q ntp
ntp-4.2.6p5-19.el7.centos.3.x86_64

    若未安装,则进行安装

yum -y install ntp

    开机启动

systemctl enable ntpd

4、查看当前系统时区及其它关联信息

# timedatectl status
      Local time: Wed 2022-05-18 19:12:39 PDT
  Universal time: Thu 2022-05-19 02:12:39 UTC
        RTC time: Thu 2022-05-19 02:12:40
       Time zone: America/Los_Angeles (PDT, -0700)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2022-03-13 01:59:59 PST
                  Sun 2022-03-13 03:00:00 PDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2022-11-06 01:59:59 PDT
                  Sun 2022-11-06 01:00:00 PST

    设置对应时区(这里设置为Asia/Shanghai (CST, +0800))

timedatectl set-timezone Asia/Shanghai

    同步远程标准服务器

ntpdate -u cn.pool.ntp.org

    将系统时间同步到硬件时间

hwclock --systohc

    将系统时间同步到CMOS

clock -w

    重启之后,硬件时间就不会跳变回原来的时间了,那同步系统时间到硬件时间,为何要多此一举?

5、设置ntp服务器配置

# vim /etc/ntp.conf
...
#restrict 192.168.1.0 mask255.255.255.0 nomodify notrap
# 添加下面一行我的ip是192.168.5.135和192.168.5.136,下面的设置是否正确?
restrict 192.168.5.0 mask255.255.255.0 nomodify notrap
...
# 注释server域名配置
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# 添加远程服务
server 2.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.asia.pool.ntp.org nomodify notrap noquery

#设置本机为ntp服务端
server 127.127.1.0 # localclock
fudge 127.127.1.0 stratum 10
...

6、 设置ntp客户端配置

vim /etc/ntp.conf
...
# 注释这四行
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.5.136
restrict 192.168.5.136  nomodify notrap noquery

server 127.127.1.0 # localclock
fudge 127.127.1.0 stratum 10
...

7、启动ntpd服务

systemctl start ntpd

    查看网络中的ntp服务器

ntpq -p

    查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步
        刚启动时

# ntpstat
unsynchronised
 time server re-starting
  polling server every 64 s

        连接并同步后

# ntpstat
synchronised to NTP server(202.112.10.36) at stratum 10
  time correct to within 275 ms
  polling server every 256 s

部署完之后,还是迷糊状态,无法确定成功了没,哪位大佬可以指点完善,不胜感激!!
相关参数参考1
相关参数参考2
相关参数参考3

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是在 CentOS 7 上搭建 NTP 服务器的步骤: 1. 安装 NTP:运行以下命令安装 NTP: ``` sudo yum install ntp ``` 2. 配置 NTP:编辑 `/etc/ntp.conf` 文件,配置服务器地址和允许访问的客户端地址。例如,以下是一个示例配置: ``` server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict <客户端IP地址> mask <子网掩码> nomodify notrap ``` 其中,`server` 指定 NTP 服务器地址,`restrict` 用于限制访问控制。 3. 启动 NTP:运行以下命令启动 NTP 服务并设置为开机自启: ``` sudo systemctl start ntpd sudo systemctl enable ntpd ``` 4. 验证 NTP:使用 `ntpq` 命令验证 NTP 是否正常工作。例如,运行以下命令: ``` ntpq -p ``` 如果 NTP 正常工作,将显示类似以下内容的输出: ``` remote refid st t when poll reach delay offset jitter ============================================================================== *ntp1.example.c .GPS. 1 u 154 256 377 0.862 -30.200 1.835 +ntp2.example.c .PPS. 1 u 173 256 377 0.991 -30.230 1.309 -ntp3.example.c .GPS. 1 u 130 256 377 1.003 -29.990 1.935 ``` 其中,`*` 表示当前服务器为 NTP服务器,`+` 表示当前服务器为 NTP 辅助服务器,`-` 表示当前服务器为 NTP 被动服务器。 以上就是在 CentOS 7 上搭建 NTP 服务器的步骤。希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值