Windows 类: time.pool.aliyun.com
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
国家授时中心服务器 210.72.145.44
服务端 ntp
- ignore: #关闭所有的 NTP 联机服务 (基本不用);
- nomodify: #客户端不能更改服务端的时间参数, 但是客户端可以通过服务端进行网络校时;
- notrap: #拒绝特殊的 ntpdq 捕获消息
- notrust: #客户端除非通过认证, 否则该客户端来源将被视为不信任子网
- noquery: #不提供客户端的时间查询
本例中允许接收同步请求的地址范围是 192.168.30.0/24
例如 :
- restrict192.168.30.0 mask 255.255.255.0 nomodify notrap
- restrict 192.168.100.0 mask 255.255.255.0 nomodify #该网段可以进行校时
- restrict 59.124.196.84 #允许该 NTP 服务器进入
- # 注意: 如果参数没有设定, 那就表示该 IP(或子网) 没有任何限制
或者 restrict default kod nomodify
配置上游 NTP 服务器之前, 先检查上游服务器是否能正常连通. 若可以使用, 修改 NTP 配置文件如下:
- server 210.72.145.44 #China Time Center
- server cn.pool.ntp.org #Pulbic Time Server
- server 202.112.10.36
- server 127.127.1.0 # local clock
- fudge 127.127.1.0 stratum 10
- server time-nw.nist.gov prefer //prefer 该服务器优先
如果第二步配置的 server 210.72.145.44,cn.pool.ntp.org 都无效时,
则 NTP 服务器会根据这里的配置, 把自己的时间做为 NTP 服务器的时间, 即和自己同步.
如果要使用本地时间, 将 server 网络服务注释, 添加两行
- server 127.127.1.0 # local clock
- fudge 127.127.1.0 stratum 10
备注: fudge 这行是时间服务器的层次. 设为 0 则为顶级, 如果要向别的 NTP 服务器更新时间, 请不要把它设为 0
- systemctl start ntpd
- ntpstat
- ntpq -p
st: 即 stratum 阶层, 值越小表示 ntp serve 的精准度越高;
when: 几秒前曾做过时间同步更新的操作;
Poll 表示, 每隔多少毫秒与 ntp server 同步一次;
reach: 已经向上层 NTP 服务器要求更新的次数;
delay: 网络传输过程钟延迟的时间;
offset: 时间补偿的结果;
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间
- ntpdate ntp.aliyun.com
- [ ! $(rpm -q ntp) ] && yum -y install ntp
- sed -ri.bk 's/(restrict default nomodify.)/restrict default kod nomodify/;s/^(server.)/#&/' /etc/ntp.conf
- echo -e "server ntp.aliyun.com \nserver ntp1.aliyun.com">> /etc/ntp.conf
- systemctl start ntpd.service
- systemctl enable ntpd.service
客户端配置 ntpdate
30 * * * * root /usr/sbin/ntpdate 192.168.100.100; /sbin/hwclock -w
同步硬件时钟?
? NTP 一般只会同步 system clock. 如果我们也要同步 RTC(hwclock)
- vi /etc/sysconfig/ntpd SYNC_HWCLOCK=yes
- hostname -I
服务器端
yum -y install chrony
主配置文件:/etc/chrony.conf
客户端程序:/usr/bin/chronyc
服务端程序:/usr/sbin/chronyd
- VIM /etc/chrony.conf
- server cn.pool.ntp.org iburst
- server tw.pool.ntp.org iburst
允许指定网络的主机同步时间, 不指定就是允许所有, 默认不开启.
- allow 192.168.43.0/24
- # 还有一个默认不开启的选项, 意思是, 即使服务端没有同步到精确的网络时间, 也允许向客户端同步不精确的时间. 可以视情况而定.
- # Serve time even if not synchronized to any NTP server.
- #local stratum 10
- systemctl start chronyd.service
客户端
VIM chrony.conf
server 内网时间服务器主机 IP iburst
- systemctl start chronyd
- enable
使用 chronyc
查看现有的时间服务器
chronyc> sources
查看时间服务器状态
chronyc> sourcestats
chrony 兼容 ntpdate, 客户端可以使用 ntpdate 手动同步时间
ntpdate 内网时间服务器 IP
配置 /etc/ntp.conf 选项
系统时间与 BIOS 事件的偏差记录
driftfile /etc/ntp/drift
restrict 控制相关权限.
语法为: restrict IP 地址 mask 子网掩码 参数
其中 IP 地址也可以是 default ,default 就是指所有的 IP
参数有以下几个:
ignore? : 关闭所有的 NTP 联机服务
nomodify: 客户端不能更改服务端的时间参数, 但是客户端可以通过服务端进行网络校时.
notrust : 客户端除非通过认证, 否则该客户端来源将被视为不信任子网
noquery : 不提供客户端的时间查询: 用户端不能使用 ntpq,ntpc 等命令来查询 ntp 服务器
notrap : 不提供 trap 远端登陆: 拒绝为匹配的主机提供模式 6 控制消息陷阱服务. 陷阱服务是 ntpdq 控制消息协议的子系统, 用于远程事件日志记录程序.
nopeer : 用于阻止主机尝试与服务器对等, 并允许欺诈性服务器控制时钟
kod : 访问违规时发送 KoD 包.
restrict -6 表示 IPV6 地址的权限设置.
restrict default nomodifynotrapnoquery #? 关闭所有的? NTP? 要求封包
上层主机设定
server [IP|HOST Name] [prefer]
prefer 表示优先主机
与? Time Server? 沟通时所花费的时间, 记录在 driftfile? 后面接的文件内
restrict 用来设置访问权限, server 用来设置上层时间服务器, driftfile 用来设置保存漂移时间的文件
启动服务 systemctl start ntpd
查看是否同步 ntpstat 会有时间延迟, 可稍等会再试
ntpq -p ntp 与上层服务器的关系
- ntpq -p
- remote refid st t when poll reach delay offset jitter
- ==============================================================================
- *139.199.215.251 100.122.36.4 2 u 56 64 7 39.877 14.137 4.521
- +ntp04.rserver.e 85.199.214.102 2 u 57 64 5 250.114 43.419 3.252
- biisoni.miuku.n .INIT. 16 u - 64 0 0.000 0.000 0.000
- +ntp-fi01.rserve 129.242.4.241 2 u 53 64 7 336.771 27.274 7.773
含义:
remote: 即 NTP 主机的 IP 或主机名称. 注意最左边的符号, 如果由 "+" 则代表目前正在作用钟的上层 NTP, 如果是 "*" 则表示也有连上线, 不过是作为次要联机的 NTP 主机.
refid: 参考的上一层 NTP 主机的地址
st: 即 stratum 阶层 越小越精确
when: 几秒前曾做过时间同步更新的操作
poll: 下次更新在几秒之后
reach: 已经向上层 NTP 服务器要求更新的次数
delay: 网络传输过程钟延迟的时间
offset: 时间补偿的结果
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间
如需同时同步硬件时间, 需设置 / etc/sysconfig/ntpd 文件, 添加? SYNC_HWCLOCK=yes?
配置:
设置默认策略允许任何主机同步
restrict default ignore
保留设置 (对本机的)
- restrict 127.0.0.1
- restrict -6 ::1
修改 / etc/ntp/stpe-tickers 文件, 当 ntpd 服务启动时, 会自动与该文件中记录的上层 NTP 服务进行时间校对
iptables iptables -I INPUT -p udp --dport 123 -j ACCEPT
ntpdate 调整时间的方式 "跃变": 在获得一个时间之后, ntpdate 使用 settimeofday(2) 设置系统时间, 尽量使用 ntpd, 如果时间偏移太大, ntpd 同步不会成功, 可手动同步
则 NTP 服务器会根据这里的配置, 把自己的时间做为 NTP 服务器的时间, 即和自己同步.
如果要使用本地时间, 将 server 网络服务注释, 添加两行
- server 127.127.1.0 # local clock
- fudge 127.127.1.0 stratum 10
备注: fudge 这行是时间服务器的层次. 设为 0 则为顶级, 如果要向别的 NTP 服务器更新时间, 请不要把它设为 0
来源: http://www.bubuko.com/infodetail-3224399.html