CentOS搭建NTP服务器

来源:互联网 发布:可以查数据的网站 编辑:程序博客网 时间:2024/05/22 17:31

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

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

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

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

[root@ntpserver ~]#vim /etc/sysconfig/clockZONE="Asia/Shanghai"UTC=trueARC=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.1restrict -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.orgserver 1.cn.pool.ntp.orgserver 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/driftrestrict 127.0.0.1restrict -6 ::1# 配置时间服务器为本地的时间服务器server 192.168.1.135restrict 192.168.1.135 nomodify notrap noqueryserver  127.127.1.0     # local clockfudge   127.127.1.0 stratum 10includefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
0 0
原创粉丝点击