Linux标准环境配置手册--NTP服务配置
在内部局域网不能联网进行时间同步的时候我们就需要搭建NTP时间服务器使局域网的机器时间同步。此时局域网的机器只有一个或多个内网网卡而没有外部网卡,而搭建NTP服务的机器至少有一个外部网卡和一个与局域网同网段的内网网卡,外网网卡用于本机时间同步,内网网卡用于连通局域网。
服务端配置1.安装NTP服务组件
yum install ntp -yrpm -qa ntp
2.配置文件详解
# 默认配置文件
[root@m01 ~]# egrep -v "^$|#" /etc/ntp.conf # 过滤掉注释行driftfile /var/lib/ntp/driftrestrict default kod nomodify notrap nopeer noquery # 对默认的客户端拒绝所有操作restrict -6 default kod nomodify notrap nopeer noquery # 针对ipv6地址的权限restrict 127.0.0.1 # 允许本地地址的一切操作restrict -6 ::1 # 允许ipv6地址的一切操作server 0.centos.pool.ntp.org iburst # 默认的上层时间服务器server 1.centos.pool.ntp.org iburstserver 2.centos.pool.ntp.org iburstserver 3.centos.pool.ntp.org iburstincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
3.restrict参数介绍
权限的设定主要是由restrict的参数决定的。 restrict语法
restrict IP地址 mask 子网掩码 参数
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
参数有以下几个:
ignore:关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询
注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!
设定局域网内部网段所有客户端可以连接到本地进行时间同步,但是拒绝他们修改服务器上的时间
restrict 172.16.1.0 mask 255.255.255.0 nomodify
4.修改配置文件如下
查询ntp.conf文件中内容
[root@web1 ~]# egrep -v "^$|#" /etc/ntp.confdriftfile /var/lib/ntp/drift#修改为局域网网段restrict 10.154.0.0 mask 255.255.255.0 nomodify notraprestrict 127.0.0.1 restrict -6 ::1server 127.127.1.0fudge 127.127.1.0 stratum 8includefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
5.开机自启动
[root@m01 ~]# chkconfig ntpd on[root@m01 ~]# chkconfig --list ntpdntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
客户端配置
1.安装NTP服务组件
yum install ntp -yrpm -qa ntp
2.只需要在客户端的配置文件/etc/ntp.conf中添加我们刚才配置的时间服务器ip地址
[root@localhost ~]#vi /etc/ntp.confdriftfile /var/lib/ntp/driftrestrict 10.154.0.3 nomodify notrap noqueryrestrict 127.0.0.1 restrict -6 ::1server 10.154.0.3 #修改为局域网内服务器IP地址fudge 10.154.0.3 stratum 10 #修改为局域网内服务器IP地址includefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
3.测试NTP客户端
# Server Start Service[root@client ~]# /etc/init.d/ntpd start # Client Test[root@client ~]# ntpdate 10.154.0.311 Apr 10:49:37 ntpdate[3523]: step time server 172.16.1.61 offset 40976.883166 sec[root@client ~]# dateWed Apr 11 10:49:40 CST 2018
4.配置定时任务
# Client 每五分钟同步一下cat >>/var/spool/cron/root<<EOF#time sync by rsq*/5 * * * * /usr/sbin/ntpdate 10.154.0.3 > /dev/null 2>&1 #IP地址修改为NTP服务器IP地址EOF
5.开机自启动
[root@m01 ~]# chkconfig ntpd on[root@m01 ~]# chkconfig --list ntpdntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
常见问题
1.修改本地时区
[root@m01 ~]# date # 有可能我们看到的时区不是CST(中国的默认时区)Wed Apr 11 11:54:18 EDT 2018 # 修改本地时区,把/usr/share/zoneinfo/目录下我们想要的timezone拷贝到/etc/localtime文件即可(覆盖)[root@m01 ~]# \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime[root@m01 ~]# dateWed Apr 11 11:55:21 CST 2018
2.同步硬件时钟
# 在/etc/sysconfig/ntpd配置文件中添加SYNC_HWCLOCK=yes即可cat >>/etc/sysconfig/ntpd<<EOFSYNC_HWCLOCK=yesEOF# 或者使用hwclock -w命令
3.ntpdate报错the NTP socket is in use, exiting
关闭ntp服务,重新使用ntpdate命令
4.ntp的错误 no server suitable for synchronization found
• 检查服务端与客户端防火墙123端口启用情况,如果启用加入123端口访问
-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
• 检查端口占用情况
netstat -alntp|grep ntp
• 使用ntpdate -d [ntp服务器IP地址],尝试手动连接NTP服务器
• 错误分两种:
– 错误1.server dropped:strata too high
– 错误2:server dropped :no data
- 2022-10-10
Linux标准环境配置手册--NTP服务配置 - 2022-10-10
计算机行业的就业前景与从业方向 - 2022-10-10
如何看待计算机专业的就业前景,以及计 - 2022-10-10
目前计算机专业就业前景(计算机专业的就 - 2022-10-09
工作在物理层的网络设备是(计算机网络基 - 2022-10-09
openwrt 学习常用命令及备注 - 2022-10-09
关于工业以太网交换机,你应该知道这些 - 2022-10-09
怎么改家里的wifi密码oppo手机(OPPO的WiFi功 - 2022-10-09
简单的设置,便可以让OPPO手机成为家里的 - 2022-10-09
OPPO手机WiFi问题汇总解决方法,建议收藏