Contents
ntp配置
ntp相关介绍:
参考windows文档: Windows 时间服务的工作原理: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/how-the-windows-time-service-works 参考: https://wiki.archlinux.org/title/Network_Time_Protocol_daemon_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 参考: https://man.archlinux.org/man/ntp.conf.5
Windows下ntp配置
脚本来源与参考: http://t.zoukankan.com/liangqihui-p-7230881.html 参考windows文档: Windows 时间服务工具和设置: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings 参考windows文档: Windows 时间服务的工作原理: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/how-the-windows-time-service-works 参考windows文档: Windows 时间服务 (W32Time): https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/windows-time-service-top 参考windows文档: 配置系统以实现高精度: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/configuring-systems-for-high-accuracy
需要修改NTPSERVERNAME变量为正确的时间服务器地址, 可以通过询问信息科或者网络管理员获得. 注意, 以下将以下内容复制为bat脚本时,注意windows下需要gb2312编码, 否则可能显示乱码(notepad++ -> 编码 -> 转为ANSI编码, 然后再粘贴)
::Windows 7,10 客户端时间更新脚本NTP(管理员身份运行)
:: 设置默认的ntp server地址
set NTPSERVERNAME=time.windows.com
:: 调起管理员授权
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
:: 跳转到此批处理所在目录
cd /d %~dp0
:start
echo 请输入ntp服务器ip或域名,然后按enter键:
set /p USER_INPUT=ntp服务器ip或域名:
if defined USER_INPUT (
set NTPSERVERNAME=%USER_INPUT%
)
echo %NTPSERVERNAME%
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /ve /t REG_SZ /d 3 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /v 1 /t REG_SZ /d time.windows.com /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /v 2 /t REG_SZ /d time.nist.gov /f
:: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /v 3 /t REG_SZ /d time1.aliyun.com /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers" /v 3 /t REG_SZ /d %NTPSERVERNAME% /f
:: reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" /v NtpServer /t REG_SZ /d time1.aliyun.com /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" /v NtpServer /t REG_SZ /d %NTPSERVERNAME% /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient" /v SpecialPollInterval /t REG_DWORD /d 3600 /f
sc config w32time start= auto
ping -n 3 127.0.0.1 >nul
net stop w32time
ping -n 3 127.0.0.1 >nul
net start w32time
ping -n 3 127.0.0.1 >nul
:: w32tm /config /manualpeerlist:"time1.aliyun.com" /syncfromflags:manual /reliable:yes /update
w32tm /config /manualpeerlist:"%NTPSERVERNAME%" /syncfromflags:manual /reliable:yes /update
ping -n 3 127.0.0.1 >nul
w32tm /resync
pause "按任意键退出"
:: ------------------------------ 以下是注释内容,可删除 -------------------------------------------------------------------
:: 脚本来源与参考: http://t.zoukankan.com/liangqihui-p-7230881.html
:: windows文档: Windows 时间服务工具和设置: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings
:: windows文档: Windows 时间服务的工作原理: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/how-the-windows-time-service-works
:: windows文档: Windows 时间服务 (W32Time): https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/windows-time-service-top
:: windows文档: 配置系统以实现高精度: https://docs.microsoft.com/zh-cn/windows-server/networking/windows-time-service/configuring-systems-for-high-accuracy
:: 调试日志输出:
:: w32tm /debug /enable /file:"C:\w32time_log.log" /entries:0-300 /size:30000000
:: 查看配置
:: w32tm /query /configuration
:: 立即同步时间
:: w32tm /resync
cmd下几个命令可以查看/立即同步/调试ntp
:: 调试日志输出:
:: w32tm /debug /enable /file:"C:\w32time_log.log" /entries:0-300 /size:30000000
:: 查看配置
:: w32tm /query /configuration
:: 立即同步时间
:: w32tm /resync
Linux下ntp配置
GNU/Linux常见的ntp服务端/客户端有三种: systemd-timesyncd, ntpd, chrony.
systemd-timesyncd是实现了简单ntp协议(SNTP)的客户端工具。 仅可用于客户端同步时间。 ntpd各发行版上常见的ntp服务,可同时作为ntp客户端向从上层服务器同步时间, 也可同时作为ntp服务端向下级提供ntp时间查询服务。 chrony是ntp协议的一个新实现,用来替代ntpd。据说chrony有更高的精度。
systemd-timesyncd的使用(仅作ntp客户端, 可使用这个)
因为它是systemd附带的,一般不需要安装,使用systemd启动的新Linux 直接使用一条命令即可启动(区别于使用sysV init 启动)
timedatectl set-ntp true
然后timedatectl
查看下ntp状态, 可以看到ntp服务已启动。 注意: systemd-timesyncd与ntpd都是ntp的实现, 它们是互斥的。如果之前启动了ntpd服务,上面命令启动systemd-timesyncd, 会自动停止ntpd. 类似的,如果启动ntpd,也会自动停止systemd-timesyncd服务。 systemd-timesyncd仅能作为客户端同步时间,而且不会自动将系统时间写入硬件时钟。(虽然crontab加定时任务hwclock -w
也是可以的)(好像同步系统时间也比较粗暴,直接跃变的) 由于以上原因, 这里不多做介绍。如需其他配置,如需要默认的同步服务器等, 参看文档man systemd-timesyncd
参考: https://systemd-book.junmajinlong.com/systemd_timesyncd.html
CentOS 8中已经移除了ntp和ntpdate,它们也没有集成在基础包中。
CentOS 8使用chronyd作为时间服务器,但如果只是简单做时间同步,可直接使用systemd.timesyncd组件。
timesyncd虽然没有chronyd更健壮,但胜在简单方便,只需配置一项配置文件并执行一个命令启动便可定时同步。
$ vim /etc/systemd/timesyncd.conf
[Time]
NTP=ntp1.aliyun.com ntp2.aliyun.com
# 以下四项均可省略
FallbackNTP=1.cn.pool.ntp.org 2.cn.pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
其它常用的网络时间服务器:
cn.pool.ntp.org
1.cn.pool.ntp.org
2.cn.pool.ntp.org
3.cn.pool.ntp.org
0.cn.pool.ntp.org
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
配置好timesyncd.conf后,启动systemd timesyncd时间同步服务:
$ timedatectl set-ntp true
查看同步状态:
$ timedatectl status
Local time: Sat 2020-07-04 20:01:41 CST
Universal time: Sat 2020-07-04 12:01:41 UTC
RTC time: Sat 2020-07-04 20:01:40
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: inactive
RTC in local TZ: no
# 或者
$ timedatectl show
Timezone=Asia/Shanghai
LocalRTC=no
CanNTP=yes
NTP=no
NTPSynchronized=yes
TimeUSec=Sat 2020-07-04 20:01:41 CST
RTCTimeUSec=Sun 2020-07-05 04:01:40 CST
ntpd的使用(使用这个)
参考: https://wiki.archlinux.org/title/Network_Time_Protocol_daemon_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 参考: https://man.archlinux.org/man/ntp.conf.5 参考: https://blog.csdn.net/weixin_54632015/article/details/114101914 参考: https://blog.csdn.net/u011436427/article/details/109803723
有些发行版默认是安装ntpd服务的,只是需要启动 对于archlinux 需要 pacman -S ntp
安装ntpd服务对应的应用包。 对于ubuntu1604, 暂未验证是否需要安装。
对于archlinux, 安装后的配置文件为/etc/ntp.conf
,下面是默认的配置文件内容
# Please consider joining the pool:
#
# http://www.pool.ntp.org/join.html
#
# For additional information see:
# - https://wiki.archlinux.org/index.php/Network_Time_Protocol_daemon
# - http://support.ntp.org/bin/view/Support/GettingStarted
# - the ntp.conf man page
# Associate to Arch's NTP pool
server 0.arch.pool.ntp.org
server 1.arch.pool.ntp.org
server 2.arch.pool.ntp.org
server 3.arch.pool.ntp.org
# By default, the server allows:
# - all queries from the local host
# - only time queries from remote hosts, protected by rate limiting and kod
restrict default kod limited nomodify nopeer noquery notrap
restrict 127.0.0.1
restrict ::1
# Location of drift file
driftfile /var/lib/ntp/ntp.drift
当ntpd作为客户端时, server关键字用来说明从哪里同步时间(后面跟主机名/ip), 后面还可以跟一些其他选项, 这里略。 当ntpd作为客户端时, 当网络同步中断时, 可以使用硬件时钟(GPS时钟/原子钟/主板时钟等)继续同步时间, ntpd服务使用一些无效ip指代硬件时钟。server 172.172.t.u 就表示时钟, t表示时钟类型, u表示同一类时钟的第几个。 若要使用硬件时钟, 还可以用fudge关键字附加一些其他单独的选项。
当ntpd作为服务端时, restrict关键字用来说明当客户端连接此服务时, 受到的限制。比如不允许修改服务端时间等, 没有出现的选项都是被允许的。 default表示来自默认网关(0.0.0.0/0.0.0.0)的任意客户端的连接。
driftfile关键字用来指定记录本地时钟与网络时钟偏差的文件。用于渐进补偿时间。
下面是修改后的配置文件:
- 注释掉了默认的网络server时钟源。添加了中国国家授时中心, 以及一个内网ntp时钟源192.168.1.8 。
- 添加了硬件时钟源 server 172.172.1.0 (第1类硬件设备的第1个硬件),并紧接下一行用fudge关键字指定其层级stratum 10。 (层级最低0,这里指定为10, 用于网络同步失败时使用)
# Please consider joining the pool:
#
# http://www.pool.ntp.org/join.html
#
# For additional information see:
# - https://wiki.archlinux.org/index.php/Network_Time_Protocol_daemon
# - http://support.ntp.org/bin/view/Support/GettingStarted
# - the ntp.conf man page
# Associate to Arch's NTP pool
#server 0.arch.pool.ntp.org
#server 1.arch.pool.ntp.org
#server 2.arch.pool.ntp.org
#server 3.arch.pool.ntp.org
server 192.168.1.8
server cn.ntp.org.cn
server 127.127.1.0
fudge 127.127.1.0 stratum 10
# By default, the server allows:
# - all queries from the local host
# - only time queries from remote hosts, protected by rate limiting and kod
restrict default kod limited nomodify nopeer noquery notrap
restrict 127.0.0.1
restrict ::1
# Location of drift file
driftfile /var/lib/ntp/ntp.drift
然后重启ntpd服务
systemctl restart ntpd.service
然后查看同步情况
ntpq -p
其中,显示的各列分别表示:
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差
过好多分钟后再次查询,offset和jitter的值会慢慢变小, 因为ntpd在慢慢地渐进地同步时间。
可以把以上过程修改为脚本
#!/bin/bash
#ntp_source时间源, 可以通过命令行参数指定, 或者直接修改配置脚本NTP_SOURCE变量.
#$1是命令行第一个参数, :-表示$1不存在时,把后面的内容赋为初值
NTP_SOURCE=${1:-"cn.ntp.org.cn"}
#注释原配置文件中时间服务器配置相关行
sed -i 's/^server/#server/' /etc/ntp.conf
sed -i 's/^fudge/#fudge/' /etc/ntp.conf
#配置新的时间服务器地址
echo -en '\n' >> /etc/ntp.conf
echo "server $NTP_SOURCE" >> /etc/ntp.conf
#配置本地硬件时钟为备用时间服务器
echo -en '\n' >> /etc/ntp.conf
echo "server 127.127.1.0" >> /etc/ntp.conf
echo "fudge 127.127.1.0 stratum 10" >> /etc/ntp.conf
#重启ntp服务
# arch linux 是ntpd, ubuntu是ntp.service
#systemctl restart ntpd.service
systemctl restart ntp.service
然后, 脚本加执行权限 chmod a+x ./set_ntp_source.sh
. 执行脚本 ./set_ntp_source.sh NTP_SERVER_IP_OR_HOST
, 这里的 NTP_SERVER_IP_OR_HOST时间服务器地址需要询问信息科或者从网络管理员获得. 然后,ntpq -p
查看一次ntp时间同步状态,过个几十分钟再看看有没有变小即可。 或者, 可以ntpstat
查看下有没有synchronised to NTP server
的字样.
chrony的配置
作为ntpd的替代, 较新版本的Linux发行版本好像都是这个。(ntp server)
以ubuntu为例
apt install chrony
默认chrony是作为client服务的。如果要提供ntp server的功能, 则要允许指定网络允许访问。
vim /etc/chrony/conf.d/allow.conf
# allow all
allow 10.0.0.0/8
allow 172.16.0.0/12
allow 192.168.0.0/16
启动并enable相应的服务
systemctl start chrony.service
systemctl enable chrony.service
如果之前有开systemd-timesyncd, 关闭
timedatectl set-ntp off
发表回复