ntp服务器的配置

配置 NTP(Network Time Protocol)服务器可以帮助你的网络设备保持时间同步。下面是配置 NTP 服务器的一般步骤,针对常见的 Linux 发行版:

在 CentOS/RHEL 中配置 NTP 服务器

# 安装 NTP 软件包

sudo yum install ntp

# 配置 NTP 服务器
编辑 /etc/ntp.conf 文件,添加或修改以下配置:

# 选择 NTP 上游服务器
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

或自身

server 127.127.1.0
fudge 127.127.1.0 stratum 10

 

# 允许的客户端网络
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# 允许所有本地地址
restrict 127.0.0.1

# 允许所有本地地址 (IPv6)
restrict ::1


# 启动并启用 NTP 服务

sudo systemctl start ntpd
sudo systemctl enable ntpd


# 验证 NTP 服务状态

sudo systemctl status ntpd


在 Ubuntu/Debian 中配置 NTP 服务器

 

# 安装 NTP 软件包

sudo apt update
sudo apt install ntp


# 配置 NTP 服务器
编辑 /etc/ntp.conf 文件,添加或修改以下配置:


# 选择 NTP 上游服务器
server 0.ubuntu.pool.ntp.org iburst
server 1.ubuntu.pool.ntp.org iburst
server 2.ubuntu.pool.ntp.org iburst
server 3.ubuntu.pool.ntp.org iburst
server 127.127.1.0
fudge 127.127.1.0 stratum 10

# 允许的客户端网络
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# 允许所有本地地址
restrict 127.0.0.1

# 允许所有本地地址 (IPv6)
restrict ::1

 

# 启动并启用 NTP 服务

sudo systemctl start ntp
sudo systemctl enable ntp


# 验证 NTP 服务状态

sudo systemctl status ntp


验证 NTP 同步状态

无论你使用的是哪种 Linux 发行版,都可以使用以下命令来验证 NTP 同步状态:

ntpq -p


这将显示 NTP 服务器的连接状态和同步情况。

防火墙配置

 

确保防火墙允许 NTP 流量。对于 firewalld,可以使用以下命令:


sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload


对于 ufw,可以使用以下命令:


sudo ufw allow 123/udp
sudo ufw reload


配置完成

完成上述步骤后,你的 NTP 服务器应该已经配置并开始为网络中的其他设备提供时间同步服务。

posted @ 2024-06-21 10:25  Augustone  阅读(173)  评论(0)    收藏  举报