前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Linux部署NTP服务器进行时间同步

Linux部署NTP服务器进行时间同步

作者头像
星哥玩云
发布2022-07-26 20:31:58
发布2022-07-26 20:31:58
4.5K0
举报
文章被收录于专栏:开源部署开源部署

NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步

一、安装

# 既可做服务端也可做客户端 yum install -y ntp

# 开启服务,让其他客户端与本机同步,注意防火墙状态 systemctl start ntpd

# 开机自启 systemctl enable ntpd

二、ntp 常用配置 /etc/ntp.conf

# 记录和上级时间服务器的时间差异 driftfile /var/lib/ntp/drift

# ntp 日志 logfile /var/log/ntp.log # 日志级别 all event info logconfig all

# 设置默认策略,允许同步时间,不允许修改 restrict default nomodify notrap nopeer noquery

# 允许本机地址的一切操作,-6 为 IPV6 restrict 127.0.0.1 restrict -6 ::1

# 允许网段内客户端连接此服务器同步时间,但是拒绝让他们修改服务器上的时间 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# https://www.ntppool.org/zone/cn # 利用 server 设定上层 NTP 服务器,可设置多个。prefer 表示优先 server s1b.time.edu.cn prefer

# 在 /ntp.conf 中定义的 server 都不可用时,将使用 local 时间作为 ntp 服务提供给 ntp 客户端。建议配置,否则 ntp 服务器无法与公网 ntp 服务器同步时,其客户端也会无法同步 server  127.127.1.0 fudge  127.127.1.0 stratum 10

restrict 控制相关权限

# 格式:restrict [IP地址] mask [netmask_IP] [parameter] # IP地址:可以是 default,指所有的 IP # netmask_IP:子网掩码 # parameter 有以下几个: ## ignore  :拒绝所有类型的 NTP 联机 ## nomodify:客户端不能使用 ntpc 与 ntpq 修改服务器的时间参数,但客户端仍可通过这部主机来进行网络校时 ## noquery :客户端不能够使用 ntpq 与 ntpc 等来查询时间服务器,等于不提供 NTP 的网络校时 ## notrap :不提供 trap 这个远程事件登录(remote event logging)的功能,用于远程事件日志记录 ## notrust :Client 除非通过认证,否则该 Client 来源将被视为不信任网域,会拒绝没有认证的客户端 ## nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟 ## kod : 向不安全的访问者发送 Kiss-Of-Death 报文

三、使用

# 国家授时中心 210.72.145.44

s1a.time.edu.cn 北京邮电大学 s1b.time.edu.cn 清华大学 s1c.time.edu.cn 北京大学 s1d.time.edu.cn 东南大学 s1e.time.edu.cn 清华大学 s2a.time.edu.cn 清华大学 s2b.time.edu.cn 清华大学 s2c.time.edu.cn 北京邮电大学 s2d.time.edu.cn 西南地区网络中心 s2e.time.edu.cn 西北地区网络中心 s2f.time.edu.cn 东北地区网络中心 s2g.time.edu.cn 华东南地区网络中心 s2h.time.edu.cn 四川大学网络管理中心 s2j.time.edu.cn 大连理工大学网络中心 s2k.time.edu.cn CERNET桂林主节点 s2m.time.edu.cn 北京大学 ntp.sjtu.edu.cn 202.120.2.101 上海交通大学

ntp 常用命令

ntpdate 命令参数

# 查看ntp版本 ntpq -c version

# 上层 ntp 的状态 ntpq -p

# ntp 同步状态 ntpstat

# 向 NTP 服务器同步自己的时间,需关闭 ntpd 服务,-u 指定使用无特权的端口发送数据包 -d 调试 ntpdate -u s1a.time.edu.cn

系统时钟与硬件时钟之间同步

# 设置硬件时钟 # -w,--systohc hwclock -w

# 设置系统时钟 # -s, --hctosys hwclock -s

# 修改配置文件方式 vim /etc/sysconfig/ntpd # 将系统时间写入BIOS,与 hwclock -w 效果相同 SYNC_HWCLOCK=yes

ntpd 与 ntpdate

ntpd 不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。

四、关于定时同步

开机时自动同步可编辑 /etc/ntp/step-tickers 文件

vim /etc/ntp/step-tickers

# List of NTP servers used by the ntpdate service.

定时同步可使用 crontab,添加定时任务

systemctl start crond

systemctl enable crond

# 添加 crontab –e

# 每两个小时同步一次 0 */2 * * * ntpdate s2m.time.edu.cn && hwclock -w

# 查看 crontab –l

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档