centos7时间同步

时间服务器

设备集群需要有一个统一的时间,来确保所执行的操作的时序正确。 NTP 是网络时间协议(Network Time Protocol)的简称,就是通过网络协议使计算机之间的时间同步化。

时区管理

查看当前时区

timedatectl

查看当前可用时区

timedatectl list-timezones

设置时区

timedatectl set-timezone Asia/Shanghai

进行时间同步方法

最简单的方法,让所有集群(这里默认集群中有三台服务器:master,slave1,slave2)中的主机跟某个时间服务器的 时间同步,执行 ntpdate 时间服务器,

[root@master ~]# ntpdate ntp1.aliyun.com
[root@slave1 ~]# ntpdate ntp1.aliyun.com
[root@slave2 ~]# ntpdate ntp1.aliyun.com

不过,这种方法不好.ntpdate同步时间是跳跃的,这将导致文件时间标记,监控数据的紊乱. 而且ntpdate只运行一次就结束,即只同步一次.所以即使我们要写这样写 vi /etc/crontab

* * * * root /usr/sbin/ntpdate ntp1.aliyun.com && /sbin/hwclock -w

配置远程的时间服务器,再让所有的主机跟他同步

配置本地的时间服务器,再让所有的主机跟他同步

下面的配置ntp服务中就是同时配置了远程和本地的时间服务器,配置目的是先使用远程时间服务器同步,如果外 网发生了问题,再使用本地时间服务器.

配置ntp服务

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

服务端

工具安装

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

进行ntp服务文件配置

[root@master ~]# vim /etc/ntp.conf

注意: 1.大家自己在配置时,只需要把我的内容替换你的内容即可 2.#授权下述网段上所有的机器允许从ntp服务器上查询和同步时间 restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap 保证这里的网段是你集群的网段即可

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
restrict default nomodify notrap nopeer noquery
# 给与本机所有权限
restrict 127.0.0.1
restrict ::1
#授权下述网段上所有的机器允许从ntp服务器上查询和同步时间
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
#增加下述几个时间服务器列表,除了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
#这两行内容表示当外部时间不可用时,使用本地时间
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
keys /etc/ntp/keys
disable monitor

设置开机自启动服务(初始化)

使服务端服务ntp的守护进程ntpd生效
[root@master ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to
/usr/lib/systemd/system/ntpd.service.
使客户端工具ntpdate工具生效(选做)
[root@master ~]# systemctl enable ntpdate
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpdate.service to
/usr/lib/systemd/system/ntpdate.service.
检查
[root@master ~]# systemctl is-enabled ntpd
显示: enabled
​

启用ntp服务

[root@master ~]# systemctl start ntpd
查看ntpd进程
[root@master ~]# ps -ef | grep ntpd 
ntp 1185 1 0 03:50 ? 00:00:00 /usr/sbin/ntpd -u ntp:ntp -g root 1663 1136 0 04:35 pts/2 00:00:00 grep --
color=auto ntp
进程存在,说明服务已经正常启动

设置防火墙,放行ntp协议请求

提前将防火墙关闭即可

设置硬件时间

对时间的解释

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

系统时间:通常在开机时复制硬件时间,之后独立运行并保存了时间、时区和夏令时设置。通过date命令设置。硬件时间:(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。通过hwclock命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w同步前需要测试ntp上层服务器的连通性

2 查看ntp的情况

[root@master ~]# 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硬件时间的差异时间

执行同步

[root@master ~]# hwclock -w

测试

[root@master conf]# ntpstat

客户端

安装ntp服务和ntpdate工具

[root@slave1 ~]# yum -y install ntp ntpdate

方法一

配置用于同步的服务器

先执行hwclock -w 让系统时间和bios时间同步.[root@slave1 ~]# hwclock -w再执行下面的命令[root@slave1 ~]# echo "server 192.168.10.200" >/etc/ntp.conf重启服务以使配置生效,之后大概要等10分钟左右,才会同步成功[root@slave1 ~]# systemctl enable ntpd[root@slave1 ~]# systemctl restart ntpd这样做的好处:

a 客户端的ntpd服务始终运行着,定期同步时间,不用我们每次都手动同步或者写定时器
b 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
原因:错误1.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值