NTP本地时钟源与时间同步


本文主要介绍 ntpd 服务,文末简单介绍 chronyd服务、systemd-timesyncd服务,当前系统正在使用哪个服务,可执行 timedatectlsystemctl status xxx查看

一、安装ntp

yum install -y ntp ntpdate

有些版本ntp已经包含ntpdate

二、配置 ntp.conf

国内可用时钟源可见文末

sudo vi /etc/ntp.conf

方式 “1”:
#指定ntp服务器地址
server [时钟源IP]
#允许上层时间服务器主动修改本机时间
restrict [时钟源IP] nomodify notrap noquery

方式 “2”:
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10

结论:
a、若想作为时钟源(服务端),那么必须配置“2”;
b、若想同步时间(客户端),必须设置“1”;
c、若“1”和“2”同时配置,只生效“2”;
d、若“1”和“2”均未配置,无任何效果。
e、魔法:若既想作为时钟源,又想同步时间,那么配置“2”,crontab -e 定时任务,添加:ntpdate -u [时钟源IP] , 比如 00 01 * * * root /usr/sbin/ntpdate -u 192.168.111.200

以上配置后大约默认6分钟生效同步
一个例子:
在这里插入图片描述

三、启动ntp服务
## 需要禁掉chronyd.service  不然无法开机自启
systemctl stop chronyd
systemctl disable chronyd 

systemctl start ntpd
systemctl enable ntpd.service
四、修改硬件时间

背景:服务器重启时候,会读取bios时钟写入系统时间

系统时间写入bios时钟:hwclock -w
bios时钟写入系统时间:hwclock -s

五、常用命令

1、作为客户端时验证配置的源:ntpq -p
2、查看与时钟源的差异:ntpdate -q 192.168.111.200
3、从时钟源同步时间:
ntpdate 192.168.111.200
ntpdate -u 192.168.111.200
推荐写上 -u ,命令能够绕过防火墙

六、国内常用的NTP时间源同步服务器汇总:

使用以前请先ping相应的域名查看网络是否可达,和相应的访问速度

ntp.ntsc.ac.cn   中国国家授时中心
cn.ntp.org.cn    中国授时

阿里云NTP时间源服务器
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com

国内一些大学NTP时间源服务器
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服务,以下7个域名
http://time1.apple.com
http://time2.apple.com
http://time3.apple.com
http://time4.apple.com
http://time5.apple.com
http://time6.apple.com
http://time7.apple.com

Google(谷歌)提供的NTP服务,以下4个域名
http://time1.google.com
http://time2.google.com
http://time3.google.com
http://time4.google.com

七、chronyd服务、systemd-timesyncd服务:

redhat系:(含openEuler)以前默认 ntpd,现在默认 chronyd,比如centos

debian系:默认 systemd-timesyncd ,比如ubuntu

一般来说,三者之能取其一运行,其他需要关闭systemctl stop xxx,不然冲突。

当前系统正在运行哪个服务,可执行 timedatectlsystemctl status xxx查看

1、 chrony:

此处顺便介绍下 chrony 的配置, 编辑配置文件: vi /etc/chrony.conf

添加/修改: pool [时钟源地址] iburst
重启服务:systemctl restart chronyd

2、systemd-timesync:

此处顺便介绍下 systemd-timesync 的配置, 编辑配置文件: vi /etc/systemd/timesyncd.conf

添加/修改: NTP=[时钟源地址]
重启服务:systemctl restart systemd-timesyncd

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值