摘要:linux系统下配置网络时间源ntp服务相关命令及说明。

一、正确使用方式

系统开机自动ntpdate同步时间源一次,避免ntp服务当本机时间与时间源时间相差太大时(1000秒)不同步。

ntpd服务定期与时间源同步一次

二、ntp及ntpdate安装包下载

选择与硬件平台和操作系统版本相对应的安装包下载。

https://pkgs.org/download/ntp
https://pkgs.org/download/ntpdate

三、安装

安装命令:

# rpm –ixv ntpd*.rpm

查看软件安装情况

# rpm –qa ntp

四、服务端配置

配置文件:/etc/ntp.conf

配置网段权限

设置允许指定网段的服务器可以同步查询本服务器的ntp服务。

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict ${NETIP} mask 255.255.255.0 nomodify notrap

此处${NETIP}用实际网段IP代替。

参数说明:

Ignore           忽略NTP请求

nomodify       不允许更改服务端的时间

notrust          不信任子网

noquery        不提供时间查询服务

notrap           不提供陷阱服务

nopeer          禁用对等体连接

kod               访问违规时发送 KoD 包。

restrict -6      设置IPV6地址的权限。

五、客户端配置

配置文件:/etc/ntp.conf

设置NTP时间服务器

server ${NTPIP} prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org

此处${NTPIP}用实际时间源IP替换。

在封闭的局域网内,后三个默认server配置可以屏蔽。

允许时间源服务器修正本机时间

restrict ${NTPIP}

此处${NTPIP}用实际时间源IP替换。

避免本机作为时间源

#server 127.127.1.0  # local clock
#fudge 127.127.1.0  stratum 10

六、配置硬件时间同步

配置文件:/etc/sysconfig/ntpd

设置机器硬件BIOS时间与系统时间同步

#SYNC_HWCLOCK=no
SYNC_HWCLOCK=yes

相当于hwclock -w

七、防火墙配置

Ntp服务使用123端口,如果使用了系统防火墙,还需在防火墙中配置允许开放端口。

查看防火墙状态

# /etc/init.d/iptables status

配置允许123端口协议访问

# /sbin/iptables –I INPUT -p udp --dport 123 –j ACCEPT

八、启动ntp服务

启动ntp服务

# service ntpd start

查看ntp服务状态

# service ntpd status

停止ntp服务

# service ntod stop

九、检查ntp服务

检测ntp服务网络端口

# netstat –tlunp | grep ntp

123端口在用说明ntp服务启动成功

查看网络中的ntp服务器

# ntpq –p

查看时间同步状态

# ntpstat

十、设置开机自动启动ntp

查看ntpd开机启动配置情况

# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

查看当前系统启动等级

# runlevel
N 5

配置允许开机启动ntpd服务

# chkconfig ntpd on

配置在指定运行级别上允许开机启动ntpd服务

# chkconfig --level 345 ntpd on

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐