LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 179|回复: 0

Linux ntpd 服务

[复制链接]
发表于 2024-1-12 17:46:44 | 显示全部楼层 |阅读模式
yum 安装
CentOS 安装 NTP 很简单,执行以下命令即可:

yum -y install ntp
ntpd 配置
ntp 的配置文件路径为: /etc/ntp.conf ,参考配置:

# 1. 先处理权限方面的问题,包括放行上层服务器以及开放区网用户来源:

# restrict default kod nomodify notrap nopeer noquery     # 拒绝 IPv4 的用户
# restrict -6 default kod nomodify notrap nopeer noquery  # 拒绝 IPv6 的用户
restrict default nomodify notrap nopeer noquery
#restrict 192.168.100.0 mask 255.255.255.0 nomodify # 放行同局域网来源(根据网关和子网掩码决定)
restrict 127.0.0.1   # 默认值,放行本机 IPv4 来源
restrict ::1         # 默认值,放行本机 IPv6 来源

# 2. 设定 NTP 主机来源

# 注释掉默认 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
# 设置国内 NTP 来源
server cn.pool.ntp.org prefer # 以这个主机为优先
server ntp1.aliyun.com
server ntp.sjtu.edu.cn

# 3. 预设时间差异分析档案与暂不用到的 keys 等,不需要更改它:

driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
includefile /etc/ntp/crypto/pw
注意:如果更改配置,必须重启 NTP 服务(systemctl restart ntpd)才能生效。

放开防火墙限制

NTP 服务的端口是 123,使用的是 udp 协议,所以 NTP 服务器的防火墙必须对外开放 udp 123 这个端口。

如果防火墙使用 iptables,执行以下命令:

iptables -A INPUT -p UDP -i eth0 -s 192.168.0.0/24 --dport 123 -j ACCEPT
如果防火墙使用 firewalld,执行以下命令:

firewall-cmd --zone=public --add-port=123/udp --permanent
ntpd 服务命令
systemctl enable ntpd.service  # 开启服务(开机自动启动服务)
systemctl disable ntpd.service # 关闭服务(开机不会自动启动服务)
systemctl start ntpd.service   # 启动服务
systemctl stop ntpd.service    # 停止服务
systemctl restart ntpd.service # 重启服务
systemctl reload ntpd.service  # 重新载入配置
systemctl status ntpd.service  # 查看服务状态
查看 ntp 服务状态
验证 NTP 服务正常工作
执行 ntpstat 可以查看 ntp 服务器有无和上层 ntp 连通,,如果成功,可以看到类似以下的内容:

$ ntpstat
synchronised to NTP server (5.79.108.34) at stratum 3
   time correct to within 1129 ms
   polling server every 64 s

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表