CentOS搭建NTP服务器

要求
局域网中多台服务器,其中:
与外网同步时间的NTP服务器:192.168.1.135
内网中其他机器同步192.168.1.135的时间

检查时间,确认服务器所属的时区是否正确。(国内的一般是CST)

[root@ntpserver ~]#date                    //查看系统时间
[root@ntpserver ~]#hwclock               //查看硬件时间

如果发现时区不对,就得修正所属的时区。
//编辑硬件时钟文件

[root@ntpserver ~]#vim /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=true
ARC=false

覆盖系统时间文件

cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

执行完成后检查系统的时区是否是正常的。

安装NTP服务

一版CentOS都会自带ntp服务器,如果没有自带的话,执行以下命令安装即可

[root@ntpserver ~]# yum install -y ntp

校对当前时间

 使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步。
 [root@ntpserver ~]# ntpdate 1.cn.pool.ntp.org
注意:如果ntpd服务已经开启了,需关闭ntpd服务才可以使用 

修改配置文件

[root@ntpserver ~]# vim /etc/ntp.conf   //添加标红部分
restrict default kod nomodify notrap nopeer noquery             //服务默认拒绝所有NTP连线
restrict -6 default kod nomodify notrap nopeer noquery         //拒绝IPV6所有NTP连线

restrict 127.0.0.1
restrict -6 ::1
//以上两条表示允许本机访问
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap    
//允许192.168.0.0/24 网段的主机来进行对时,不允许客户端来修改,登录ntp服务器 

#server 0.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
#server 3.centos.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org prefer              //prefer优先使用

logfile /var/log/ntp.log

配置同步更新hwclock时间

[root@ntpserver ~]# vim /etc/sysconfig/ntpd

写入配置,并且保存

SYNC_HWCLOCK=yes

启动ntp服务

[root@ntpserver ~]# service ntpd start

查看与上层服务器连接状态

 [root@ntpserver ~]# ntpq -p

说明:*表示目前正在使用的上层NTP,+表示已连线,可提供时间更新的候补服务器

    remote           refid      st t when poll reach   delay   offset  jitter
============================================================
+news.neu.edu.cn 202.118.1.46     2 u   14  128  377   44.480    2.868  31.310
*202.118.1.130   202.118.1.46     2 u   63  128  377   41.726    1.631  21.462
 LOCAL(0)        .LOCL.          10 l    9   64  377    0.000    0.000   0.001

确认已进行同步

[root@ntpserver ~]# ntpstat

注意:等待连上上层NTP服务器后,执行上诉命令会得到下述结果。
通常显示下面信息,时间需要好几分钟

synchronised to NTP server (202.118.1.130) at stratum 3
   time correct to within 60 ms
   polling server every 128 s

加入开机启动

[root@ntpserver ~]# chkconfig ntpd on

配置内网NTP-Clients

内网其他设备作为NTP的客户端配置,相对就比较简单,而且所有设备的配置都相同。
首先需要安装NTPD服务,然后配置为自启动(与NTP-Server完全一样)。
然后找其中一台配置/etc/ntp.conf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。

driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1

# 配置时间服务器为本地的时间服务器
server 192.168.1.135

restrict 192.168.1.135 nomodify notrap noquery

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值