赞
踩
最终目的:将我们的一台主机配置成ntp服务器,同网段的其他主机可以通过ntpdate -u host-addr命令以ntp服务 器的时间来进行客户端的时间同步。
https://blog.csdn.net/leezsj/article/details/118417954
我们需要先安装
ntp
服务和ntpdate
工具:yum -y install ntp ntpdate
即使是作为服务端的主机,在必要时刻也是 需要向公用的ntp
服务器进行时间同步的(一般不用)。
yum -y install ntp ntpdate
vim /etc/ntp.conf # For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # 默认情况下,NTP服务器的日志保存在 /var/log/messages.当然我们也可以自己指定 # 自己指定日志目录 # 我们要确保他的属性和SELinux环境(这两项一般不用改) # chown ntp:ntp /var/log/ntpd.log # chcon -t ntpd_log_t /var/log/ntpd.log logfile /var/log/ntpd.log # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. # 授权下述网段上所有的机器允许从ntp服务器上查询和同步时间 restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # 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 # 增加下述几个时间服务器列表,除了0.asia.pool.ntp.org还会有很多时间服务器.比如0.cn.pool.ntp.org或者 time.nist.gov或者 server 0.asia.pool.ntp.org iburst server 1.asia.pool.ntp.org iburst server 2.asia.pool.ntp.org iburst server 3.asia.pool.ntp.org iburst #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto #这两行内容表示当外部时间不可用时,使用本地时间 server 127.127.1.0 iburst fudge 127.127.1.0 stratum 10 ##下述四行表示允许上层服务器修改本机时间 restrict 0.asia.pool.ntp.org nomodify notrap noquery restrict 1.asia.pool.ntp.org nomodify notrap noquery restrict 2.asia.pool.ntp.org nomodify notrap noquery restrict 3.asia.pool.ntp.org nomodify notrap noquery includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
# 使服务端服务ntp的守护进程ntpd生效
systemctl enable ntpd
# 使客户端工具ntpdate工具生效(选做)
systemctl enable ntpdate
# systemctl 检查
systemctl is-enabled ntpd
# 启动 ntp 服务
systemctl start ntpd
# 查看 ntp 进程
ps -ef | grep ntpd
注意查看是否开启防火墙配置
对时间的解释
linux 的时间分为系统时间和硬件时间。
系统时间:通常在开机时复制硬件时间,之后独立运行并保存了时间、时区和夏令时设置。
通过
date
命令设置。硬件时间:(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。通过
hwclock
命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w
同步前需要测试ntp
上层服务器的连通性
ntpq -p
参数 | 解释说明 |
---|---|
remote | 即NTP主机的IP或主机名称.注意最左边是+表示目前正在起作用的上层NTP,如果是*表示这个也连接上了,不过是作为次要联机的NTP主机 |
refid | 参考的上一层NTP主机的地址 |
st | 即stratum阶层 |
t | 是连接类型. u代表单播(unicast) l代表本地(local) ,其他的还有多播(multicast),广播(broadcast) |
when | 这个时间之前刚刚做过时间同步 |
poll | 下次更新在几秒之后 |
reach | 已经向上层NTP服务器要求更新的次数 |
delay | 网络传输过程中的延迟时间 |
offset | 时间补偿的结果 |
jitter | Linux系统时间和Bios硬件时间的差异时间 |
hwclock -w
ntpstat
yum -y install ntp ntpdate
配置用于同步的服务器
# 1. 先执行 hwclock -w 让系统时间和 bios 时间同步
hwclock -w
# 2. 在执行如下命令
echo "server 10.0.0.210" >/etc/ntp.conf
# 3. 重启服务以使配置生效,之后大概要等几分钟左右,才会同步成功
systemctl enable ntpd
systemctl restart ntpd
这样做的好处:
- 客户端的ntpd服务始终运行着,定期同步时间,不用我们每次都手动同步或者写定时器
- ntpd服务是慢慢改变时间直至标准时间
注意点:
最好先执行
hwclock -w
,否则如果bios时间和系统时间差异超过了30分钟,就会报错FAQ:系统报错-Sep 25 12:23:33 localhost kerne: set_rtc_mmss: can’t update from 3 to 22
进行客户端与服务器端的时间同步
[root@slave1 ~]# systemctl enable ntpdate
[root@slave1 ~]# /usr/sbin/ntpdate -u 192.168.10.20028 Mar 04:54:43 ntpdate[1727]: adjust time server 192.168.10.200 offset 0.000001 sec
让系统时间和硬件时间同步
[root@slave1 ~]# hwclock -w
可以设置定时器,定时执行,因为 ntpdate 每次执行完就失效了
[root@slave1 ~]# crontab -e
每天和主机同步一次
10 23 * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log
常见错误分析:
客户端 执行 ntpdate master
显示: no server suitable for synchronization found
原因:
错误 Server dropped: Strata too high
(在客户端执行 ntpdate -d master
可以看到,且显示 stratum 16
。而正常情况下 stratum
这个值得范围是“0~15”)
解决:
这是因为 NTP server
还没有和其自身或者它的 server
同步上。
以下的定义是让 NTP Server
和其自身保持同步,如果在 /ntp.conf
中定义的 server
都不可用时,将使用 local
时间作为 ntp
服务提供给 ntp 客户端。
server 127.127.1.0
fudge 127.127.1.0
stratum 8
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。