当前位置:   article > 正文

centos 7 配置ntp时间服务器_centos7 ntpdate

centos7 ntpdate

centos 7 配置ntp时间服务器

最终目的:将我们的一台主机配置成ntp服务器,同网段的其他主机可以通过ntpdate -u host-addr命令以ntp服务 器的时间来进行客户端的时间同步。

参考地址:

https://blog.csdn.net/leezsj/article/details/118417954

1. 服务端

1.1 安装ntp服务和ntpdate工具

我们需要先安装 ntp 服务和 ntpdate 工具:yum -y install ntp ntpdate 即使是作为服务端的主机,在必要时刻也是 需要向公用的 ntp 服务器进行时间同步的(一般不用)。

yum -y install ntp ntpdate
  • 1

1.2 修改配置文件

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

1.3 设置开机自启(初始化)

# 使服务端服务ntp的守护进程ntpd生效
systemctl enable ntpd

# 使客户端工具ntpdate工具生效(选做)
systemctl enable ntpdate

# systemctl 检查
systemctl is-enabled ntpd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.4 启用ntp服务

# 启动 ntp 服务
systemctl start ntpd

# 查看 ntp 进程
ps -ef | grep ntpd
  • 1
  • 2
  • 3
  • 4
  • 5

注意查看是否开启防火墙配置

1.5 设置硬件时间

对时间的解释

linux 的时间分为系统时间和硬件时间。

系统时间:通常在开机时复制硬件时间,之后独立运行并保存了时间、时区和夏令时设置。

通过 date 命令设置。硬件时间:(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。

通过 hwclock 命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w 同步前需要测试 ntp 上层服务器的连通性

1.6 查看 ntp 情况

ntpq -p
  • 1
参数解释说明
remote即NTP主机的IP或主机名称.注意最左边是+表示目前正在起作用的上层NTP,如果是*表示这个也连接上了,不过是作为次要联机的NTP主机
refid参考的上一层NTP主机的地址
st即stratum阶层
t是连接类型. u代表单播(unicast) l代表本地(local) ,其他的还有多播(multicast),广播(broadcast)
when这个时间之前刚刚做过时间同步
poll下次更新在几秒之后
reach已经向上层NTP服务器要求更新的次数
delay网络传输过程中的延迟时间
offset时间补偿的结果
jitterLinux系统时间和Bios硬件时间的差异时间

1.7 执行同步

hwclock -w

ntpstat
  • 1
  • 2
  • 3

2. 客户端

2.1 安装ntp服务和ntpdate工具

yum -y install ntp ntpdate
  • 1

2.2 配置时间同步

2.2.1 方法一

配置用于同步的服务器

# 1. 先执行 hwclock -w 让系统时间和 bios 时间同步
hwclock -w

# 2. 在执行如下命令
echo "server 10.0.0.210" >/etc/ntp.conf

# 3. 重启服务以使配置生效,之后大概要等几分钟左右,才会同步成功
systemctl enable ntpd
systemctl restart ntpd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样做的好处:

  1. 客户端的ntpd服务始终运行着,定期同步时间,不用我们每次都手动同步或者写定时器
  2. ntpd服务是慢慢改变时间直至标准时间

注意点:

最好先执行 hwclock -w ,否则如果bios时间和系统时间差异超过了30分钟,就会报错

FAQ:系统报错-Sep 25 12:23:33 localhost kerne: set_rtc_mmss: can’t update from 3 to 22

2.2.2 方法二

进行客户端与服务器端的时间同步

[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 
  • 1
  • 2

让系统时间和硬件时间同步

[root@slave1 ~]# hwclock -w 
  • 1

可以设置定时器,定时执行,因为 ntpdate 每次执行完就失效了

[root@slave1 ~]# crontab -e
每天和主机同步一次
10 23 * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log
  • 1
  • 2
  • 3

常见错误分析:

客户端 执行 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
  • 1
  • 2
  • 3

 
 
 
 
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/305258
推荐阅读
相关标签
  

闽ICP备14008679号