Linux NTP服务安装配置详解

网络对时服务NTP(全称Network Time Protocol)

Linux 系统中,通常使用 NTP (Network Time Protocol) 来同步系统时间。阿里云提供了公共的 NTP 服务器供用户使用。下面是一个典型的 /etc/ntp.conf 配置文件示例,用于配置 NTP 以连接到 阿里云 提供的 NTP 服务器。

先给一个完整的配置文件示例

# /etc/ntp.conf

# 以下是阿里云提供的公共 NTP 服务器地址:
server ntp1.aliyun.com prefer
server ntp2.aliyun.com
server ntp3.aliyun.com
server ntp4.aliyun.com

# 下面是一些默认的 NTP 设置,您可以根据需要调整:
#
# 指定 NTP 服务器的最小和最大 poll 时间间隔(单位为秒)
restrict default nomodify notrap nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict -4 default kod notrap nomodify nopeer noquery

# 如果您的网络环境中有路由器或防火墙,可能需要打开 UDP 123 端口
# 并允许 NTP 流量通过。以下设置允许从本地网络进行同步:
restrict 192.168.122.0 mask 255.255.255.0 nomodify notrap

# 如果您希望从其他特定 IP 地址或子网同步时间,可以添加类似的 restrict 行
# 例如:restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap

# 允许广播更新(如果您的网络支持广播模式)
broadcast 224.0.1.1

# 允许 drift 文件被写入
filegen driftfile /var/lib/ntp/drift

# 如果您的网络环境需要,可以指定 drift 文件的权限和所有权
# filegen driftfile /var/lib/ntp/drift perm 0644 iflag 0 oflag 0

# 设置日志选项
logging {
        # 记录所有的调试信息(通常不需要这个级别的日志记录)
        # debugtrace;
        # 记录所有与包相关的错误
        # packettrace;
        # 记录所有警告信息
        # warningtrace;
        # 记录所有校正消息
        # corrtag;
}

# 以下是 NTP 的默认设置,通常不需要修改这些行
# driftfile /var/lib/ntp/drift
# statsdir /var/log/ntpstats
# pidfile /var/run/ntpd.pid
# logfile /var/log/ntp.log


#配置完成后,可以通过以下命令重启 NTP 服务来应用更改:
#systemctl restart ntpd

接下来我们展开讲讲。

准备工作

关闭防火墙、关闭selinux

系统版本:CentOS7.x,

NTP服务器IP:192.168.1.111,客户端IP:192.168.1.179

安装配置NTP服务器端

安装ntp

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

修改ntp的配置文件

[root@vxworks ~]# cp /etc/ntp.conf{,.bak}
[root@vxworks ~]# vim /etc/ntp.conf
server 127.127.1.0 #以本机作为时间服务器
fudge 127.127.1.0 startnum 10 #设置服务器层级
restrict 127.0.0.1 # 允许本机使用这个时间服务器
restrict 192.168.1.0 netmask 255.255.255.0 #允许允许192.168.1.0/24网段的所有主机使用该时间服务器进行时间同步
driftfile /var/lib/ntp/ #记录当前时间服务器,与上游服务器的时间差的文件
logfile /var/log/ntp/ntp.log #指定日志文件位置,需要手动创建

创建日志文件

root@vxworks ~]# mkdir /var/lib/ntp/
[root@vxworks ~]# touch /var/lib/ntp/ntp.log

启动服务

[root@vxworks ~]# systemctl start ntpd
[root@vxworks ~]# systemctl enable ntpd

查看状态

[root@vxworks ~]# ntp stat
synchronised to local net at stratum 6 time correct to within 11 ms polling server every 64 s
#synchronised:表示时间同步完成(ntp可以正常工作了)
#unsynchronised:表示时间同步尚未完成
# 或者用 ntpq -p查看状态
[root@vxworks ~]# ntpq -p 
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 13 64 377 0.000 0.000 0.000

安装配置NTP客户端

安装

[root@vxworks ~]# yum install ntp ntpdate -y

修改配置文件

[root@vxworks ~]# cp /etc/ntp.conf{,.bak}
[root@vxworks ~]# vim /etc/ntp.conf
server 192.168.1.111 #设置以192.168.1.111做为本机的时间服务器
restrict 127.0.0.1
logfile /var/log/ntp/ntp.log #指定日志文件位置,需要手动创建

创建日志文件

[root@vxworks ~]# mkdir /var/log/ntp
[root@vxworks ~]# touch /var/log/ntp/ntp.log

先执行一次ntpdate时间同步

[root@vxworks ~]# ntpdate 192.168.1.111

启动ntpd

[root@vxworks ~]# systemctl start ntpd

检查状态

[root@vxworks ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s

或者

[root@vxworks ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.1.111 LOCAL(0) 6 u 11 64 1 0.502 0.009 0.000