linux设置系统时间函数(linux命令修改系统时间方法)

前言:我们在搭建集群时必须保证集群中每一台主机的时间都是同步的,下面就具体讲解如何进行时间同步.

一 时间服务器的作用

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

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

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

二 时区管理

1 查看当前时区

timedatectl

[root@master ~]# timedatectlLocal time: Sat 2019-02-16 23:07:46 CSTUniversal time: Sat 2019-02-16 15:07:46 UTCRTC time: Sat 2019-02-16 15:07:45Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yesNTP synchronized: yesRTC in local TZ: noDST active: n/a

2 查看当前可用时区

timedatectl list-timezones

Africa/AbidjanAfrica/AccraAfrica/Addis_AbabaAfrica/AlgiersAfrica/AsmaraAfrica/Bamako…

3 设置时区

timedatectl set-timezone Asia/Shanghai

三 进行时间同步方法

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

例如:

[root@master ~]# ntpdate http://ntp1.aliyun.com

[root@slave1 ~]# ntpdate http://ntp1.aliyun.com

[root@slave2 ~]# ntpdate http://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 -groot 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]# ntpstatsynchronised 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.0fudge 127.127.1.0 stratum 8 
(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论