linux 6 ntp,CentOS6 自己动手搭建开放NTP服务器(示例代码)

第一步:

1. 安装NTP服务程序:

[root@host ~]# rpm -qa | grep ntp #查询是否安装ntp服务程序

[root@host ~]# yum install -y ntp #安装ntp服务程序

2. 配置NTP服务程序:

以下红色是新增的配置,黑色是默认配置

[root@host ~]# cp /etc/ntp.conf{,.bak} #备份ntp配置文件

[root@host ~]# vim /etc/ntp.conf #使用vim编辑ntp配置文件 见vim使用方法

driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict -6 ::1

restrict 192.168.5.0 mask 255.255.255.0 nomodify notrap #允许该网段内的主机可以进行校时

restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap

restrict default #default指所有的IP

server 210.72.145.39 prefer #上层NTP服务器地址,prefer表示优先

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

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

3. 设置NTP服务器控制操作:

# chkconfig ntpd on #设置为开机启动

# /etc/init.d/ntpd start #启动NTP服务器

# /etc/init.d/ntpd stop #关闭NTP服务器

# /etc/init.d/ntpd restart #重启NTP服务器

查看NTP端口

[root@host ~]# netstat -tlunp | grep ntp

udp 0 0 192.168.5.180:123 0.0.0.0:* 28331/ntpd #这里的ip地址是本地IP,如果开放公共NTP的话是NTP服务器的IP

udp 0 0 127.0.0.1:123 0.0.0.0:* 28331/ntpd

udp 0 0 0.0.0.0:123 0.0.0.0:* 28331/ntpd

udp 0 0 fe80::20c:29ff:fe2b:9990:123 :::* 28331/ntpd

udp 0 0 ::1:123 :::* 28331/ntpd

udp 0 0 :::123 :::* 28331/ntpd

4.

现在ntp服务器已经启动了,不过与上层服务器连接则还需要一些时间,通常启动NTP后在15分钟内才会和上层NTP服务器顺利连接上。

如何确定NTP服务器顺利地更新了自己的时间呢?可以使用以下几个命令查看

[root@host ~]# ntpstat

synchronised to NTP server (72.5.72.15) at stratum 3

time correct to within 80 ms

polling server every 128 s

这个命令列出NTP服务器是否已经与上层NTP服务器连接,由上述的输出结果可以知道,时间已经校正约80ms,且每隔128秒会主动去更新============================================================================================================================================

ntpq -p命令可以列出当前我们的NTP服务器与上层NTP服务器的连接状态:

[root@host ~]# ntpd -p

remote refid st t when poll reach delay offset jitter==============================================================================

210.72.145.39 .INIT. 16 u - 1024 0 0.000 0.000 0.000

*mirror1.sjc02.s 162.213.2.253 2 u 1 128 377 10.196 0.945 2.653

+clocka.ntpjs.or 142.66.101.13 2 u 68 128 377 32.037 0.300 3.040

+96-83-113-171-s 172.16.23.153 2 u 132 128 377 77.364 3.457 3.268

-tesla.selinc.co 74.117.214.3 2 u 465 128 70 40.243 7.171 5.293

以上几个字段的意义如下:

remote: NTP主机的IP或主机名;

注意左边的符号: *:表示正在使用的NTP服务器

+:表示已经连接成功,但是作为备用提供时间更新的NTP服务器;

refid: 上层NTP服务器地址;

st: 就是stratum阶层;

when:几秒钟前做过时间同步更新操作;

pool:下次更新在几秒钟之后;

reach:已经向上层NTP服务器要求更新的次数;

delay: 网络传输过程中延迟的时间,单位为10-6秒;

offset:时间补偿结果,单位为10-3秒;

jitter:系统时间与BIOS硬件时间的差异时间,单位为10-6秒;

PS:

IP: 210.72.145.39

配置NTP时可以同步硬件时钟指令如下:

同步硬件时钟 SYNC_HWCLOCK=yes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值