centos7配置时间同步

一 时间服务器的作用

设备集群需要有一个统一的时间,来确保所执行的操作的时序正确。

NTP 是网络时间协议(Network Time Protocol)的简称,就是通过网络协议使计算机之间的时间同步化。

例如服务器集群进行文件创建、数据库处理等操作的时间。

二 时区管理

1 查看当前时区

timedatectl

[root@master ~]# timedatectl
Local time: Sat 2019-02-16 23:07:46 CST
Universal time: Sat 2019-02-16 15:07:46 UTC
RTC time: Sat 2019-02-16 15:07:45
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

2 查看当前可用时区

timedatectl list-timezones

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako

3 设置时区

timedatectl set-timezone Asia/Shanghai

三 进行时间同步方法

3.1 最简单的方法,让所有集群(这里默认集群中有三台服务器: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

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

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

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

四 配置ntp服务

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

4.1 服务端

4.1.1 工具安装

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

4.1.2 进行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
4.1.3 设置开机自启动服务(初始化)

使服务端服务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

4.1.4 启用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

进程存在,说明服务已经正常启动

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

提前将防火墙关闭即可

4.1.6 设置硬件时间
4.1.6.1 对时间的解释
linux的时间分为系统时间和硬件时间。

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

硬件时间:
(RTC、Real-Time Clock),CMOS时间,在主板上靠电池供电,仅保存时期时间数值。通过hwclock命令设置,在这里,我们用系统时间同步硬件时间:hwclock -w

同步前需要测试ntp上层服务器的连通性
4.1.6.2 查看ntp的情况

[root@master ~]# ntpq -p

remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================
+electrode.felix 89.231.96.83     2 u   56   64    1  286.235    9.133   0.971
*undefined.hostn 127.67.113.92    2 u   56   64    1  202.865   -7.224  13.201
-de-user.deepini 195.13.23.5      3 u   58   64    1  273.953   16.689   2.172
+ntp5.flashdance 192.36.143.153   2 u   55   64    1  321.711   10.639   4.733
 LOCAL(0)        .LOCL.          10 l   64   64    2    0.000    0.000   0.000

解释:

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硬件时间的差异时间
4.1.6.3 执行同步

[root@master ~]# hwclock -w

4.1.6.4 测试

[root@master conf]# ntpstat
synchronised to NTP server (78.46.102.180) at stratum 3
time correct to within 189 ms
polling server every 64 s

说明本地已经与时间服务器实现了同步

4.2 客户端

4.2.1 安装ntp服务和ntpdate工具

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

4.2.2 方法一

1 配置用于同步的服务器

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

[root@slave1 ~]# hwclock -w

再执行下面的命令

[root@slave1 ~]# echo “server 192.168.10.200” >/etc/ntp.conf

2 重启服务以使配置生效,之后大概要等10分钟左右,才会同步成功

[root@slave1 ~]# systemctl enable ntpd

[root@slave1 ~]# systemctl restart ntpd

3 这样做的好处:

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

4 注意点:

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

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

4.2.3 方法二

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

[root@slave1 ~]# systemctl enable ntpdate

[root@slave1 ~]# /usr/sbin/ntpdate -u 192.168.10.200

28 Mar 04:54:43 ntpdate[1727]: adjust time server 192.168.10.200 offset 0.000001 sec

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

[root@slave1 ~]# hwclock -w

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

[root@slave1 ~]# crontab -e

每天和主机同步一次

10 23 * * * (/usr/sbin/ntpdate -u 192.168.10.200 && /sbin/hwclock -w) &> /var/log/ntpdate.log

4 常见错误分析

常见错误分析: 客户端 执行  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 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: CentOS 7 可以使用ntpd服务来配置NTP时间同步。首先,要安装ntpd服务:sudo yum install ntp。然后配置/etc/ntp.conf,添加NTP服务器列表。最后,启动ntpd服务:sudo systemctl start ntpd。 ### 回答2: 在CentOS 7上配置NTP时间同步,需要以下步骤: 1. 安装NTP软件包:打开终端,并使用root权限执行以下命令安装NTP软件包: `yum install ntp -y` 2. 配置NTP服务器:编辑NTP配置文件,通过以下命令打开它: `vi /etc/ntp.conf` 在文件的顶部添加以下行来配置NTP服务器: ``` server pool.ntp.org iburst server time.google.com iburst ``` 3. 更新防火墙规则:如果系统启用了防火墙,需要更新防火墙规则以允许NTP流量通过。执行以下命令更新防火墙规则: ``` firewall-cmd --zone=public --add-service=ntp --permanent firewall-cmd --reload ``` 4. 启用和启动NTP服务:使用以下命令启用和启动NTP服务: ``` systemctl enable ntpd systemctl start ntpd ``` 5. 检查NTP服务状态:执行以下命令来检查NTP服务的状态: `systemctl status ntpd` 6. 验证时间同步:使用以下命令验证时间同步是否成功: `ntpq -p` 如果出现一列以"*"开头的服务器,表示时间同步成功。 注意:以上步骤假设您具有root权限。如果没有root权限,请使用sudo命令来执行相应的命令。另外,根据自己的需求可以选择不同的NTP服务器进行配置。 ### 回答3: Centos 7配置NTP时间同步非常简单。您可以按照以下步骤进行设置: 1. 打开终端并以root用户身份登录到Centos 7系统中。 2. 使用以下命令安装ntp软件包: ``` yum install ntp ``` 3. 安装完成后,修改ntp配置文件。使用以下命令打开该文件: ``` vi /etc/ntp.conf ``` 4. 在打开的文件中,找到并注释掉以"server"开头的行,例如: ``` # server <服务器地址> ``` 然后,在下面添加以下行来指定要同步时间服务器: ``` server ntp.ntsc.ac.cn iburst ``` 您可以根据需要选择其他可靠的NTP服务器。 5. 保存并关闭文件。 6. 启动ntp服务并设置其自动启动: ``` systemctl start ntpd systemctl enable ntpd ``` 7. 使用以下命令验证时间同步是否成功: ``` ntpq -p ``` 该命令将显示已同步到的时间服务器列表。 现在,您的Centos 7系统已配置为使用NTP时间同步。它将自动与指定的NTP服务器同步时间

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小大数据

你的打赏是我活下去的动力哟~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值