关于linux运维:linux时间管理

5次阅读

共计 6520 个字符,预计需要花费 17 分钟才能阅读完成。

Linux 工夫设置 date、hwclock、timedatectl

date 查看及设置零碎工夫:

[root@localhost ~]# date
Thu Jan 13 16:30:27 CST 2022
[root@localhost ~]# date -s "20220113 16:33:00"
Thu Jan 13 16:33:00 CST 2022

hwclock 设置零碎工夫:

[root@localhost ~]# hwclock
2022-01-13 16:37:35.195393+08:00
// 同步硬件工夫到零碎工夫
[root@localhost ~]# hwclock -s
[root@localhost ~]# hwclock --hctosys
// 同步零碎工夫到硬件工夫
[root@localhost ~]# hwclock -w
[root@localhost ~]# hwclock --systohc

timedatectl 工夫治理:

// 查问工夫及时区
[root@localhost ~]# timedatectl
               Local time: Thu 2022-01-13 16:53:10 CST
           Universal time: Thu 2022-01-13 08:53:10 UTC
                 RTC time: Thu 2022-01-13 16:53:09
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: yes
// 查问并设置时区
[root@localhost ~]# timedatectl list-timezones
[root@localhost ~]# timedatectl set-timezone "Asia/Shanghai"
// 设置日期和工夫
[root@localhost ~]# timedatectl set-time '16:10:40 2015-11-20'
[root@localhost ~]# timedatectl set-time UTC
// 设置硬件工夫为本地工夫
[root@localhost ~]# timedatectl set-local-rtc 1
[root@localhost ~]# timedatectl set-local-rtc 0
//

Linux 工夫同步 ntpdate、ntpd、chronyd

ntpdate 工夫同步:

[root@localhost ~]# ntpdate 192.168.0.1
[root@localhost ~]# ntpdate time.ntp.org

ntpd 工夫同步(端口:123):

// 配置文件:/etc/ntp.conf
1. 对于权限设定局部
权限的设定次要以 restrict 这个参数来设定,次要的语法为:restrict IP mask netmask_IP parameter
其中 IP 能够是软件地址,也能够是 default,default 就相似 0.0.0.0。至于 paramter 则有:ignore:敞开所有的 NTP 联机服务
  nomodify:示意 Client 端不能更改 Server 端的工夫参数
  notrust:该 Client 除非通过认证,否则该 Client 起源将被视为不信赖网域
  noquery:不提供 Client 端的工夫查问
  notrap:不提供 trap 这个近程事件登入
如果 paramter 齐全没有设定,那就示意该 IP (或网域)"没有任何限度"

restrict default nomodify notrap noquery   # 敞开所有的 NTP 要求封包
restrict 127.0.0.1   # 这是容许本机查问
restrict 192.168.0.1 mask 255.255.255.0 nomodify   # 在 192.168.0.1/24 网段内的服务器就能够通过这台 NTP Server 进行工夫同步了

2. 下层主机的设定
要设定下层主机次要以 server 这个参数来设定,语法为:server [IP|HOST Name] [prefer]
Server 前面接的就是下层 Time Server,前面加上 perfer 的话,那示意咱们的 NTP 主机次要以该部主机来作为工夫校对的对应。另外,为了解决更新工夫封包的传送提早动作,能够应用 driftfile 来规定咱们的主机,在与 Time Server 沟通时所破费的工夫,能够记录在 driftfile 前面接的文件内,例如上面的范例中,咱们的 NTP server 与 cn.pool.ntp.org 联机时所破费的工夫会记录在 /var/lib/ntp/drift 文件内。server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server cn.pool.ntp.org prefer

3. 其余设置值,以零碎默认值即可
server 127.127.1.0    # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
broadcastdelay 0.008
keys /etc/ntp/keys
// 确认 ntp 曾经同步了工夫
[root@localhost ~]# ntpstat
synchronised to NTP server (202.120.2.101) at stratum 4
    time correct to within 557 ms
    polling server every 64 s
# 该指令可列出 NTP 服务器是否与下层联机。由上述输入后果可知,工夫校对约为 557*10(-6) 秒,且每隔 64 秒会被动更新工夫。
//ntpq 命令
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*stratum2-1.ntp. 195.91.239.8     2 u   24   64    1  258.761  -16.146  16.491
 ntp6.flashdance 194.58.202.148   2 u   23   64    1  330.604   47.243  13.014
 a.chl.la        131.188.3.222    2 u   22   64    1  187.224   41.501   1.249
 139.199.215.251 100.122.36.196   2 u   21   64    1   44.815   29.043   7.562

remote:即 NTP 主机的 IP 或主机名称。留神最右边的符号,如果由 "+" 则代表目前正在作用钟的下层 NTP,如果是 "*" 则示意也有连上线,不过是作为主要联机的 NTP 主机。refid:参考的上一层 NTP 主机的地址
st:即 stratum 阶层
when:几秒前曾做过工夫同步更新的操作
poll:下次更新在几秒之后
reach:曾经向下层 NTP 服务器要求更新的次数
delay:网络传输过程钟提早的工夫
offset:工夫弥补的后果
jitter:Linux 零碎工夫与 BIOS 硬件工夫的差别工夫

# ntp 服务默认只会同步零碎工夫。如果想要让 ntp 同时同步硬件工夫,能够设置 /etc/sysconfig/ntpd 文件。在 /etc/sysconfig/ntpd 文件中,增加 SYNC_HWCLOCK=yes 这样,就能够让硬件工夫与零碎工夫一起同步。

chronyd 工夫同步(端口:323):

// 配置文件:# 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

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

配置参数:

参数 参数阐明
server 该参数能够屡次用于增加时钟服务器,必须以 ”server “ 格局应用。
stratumweight stratumweight 指令设置当 chronyd 从可用源中抉择同步源时,每个层应该增加多少间隔到同步间隔。默认状况下,CentOS 中设置为 0,让 chronyd 在抉择源时疏忽源的层级。
driftfile chronyd 程序的次要行为之一,就是依据理论工夫计算出计算机增减工夫的比率,将它记录到一个文件中是最正当的,它会在重启后为零碎时钟作出弥补,甚至可能的话,会从时钟服务器取得较好的估值。
rtcsync rtcsync 指令将启用一个内核模式,在该模式中,零碎工夫每 11 分钟会拷贝到实时时钟(RTC)
allow/deny 这里你能够指定一台主机、子网,或者网络以容许或回绝 NTP 连贯到表演时钟服务器的机器。
cmdallow/cmddeny 指定哪个 IP 地址或哪台主机能够通过 chronyd 应用管制命令。
bindcmdaddress 该指令容许你限度 chronyd 监听哪个网络接口的命令包(由 chronyc 执行)。该指令通过 cmddeny 机制提供了一个除上述限度以外可用的额定的访问控制等级。
makestep 通常,chronyd 将依据需要通过减慢或减速时钟,使得零碎逐渐纠正所有工夫偏差。在某些特定状况下,零碎时钟可能会漂移过快,导致该调整过程耗费很长的工夫来纠正零碎时钟。该指令强制 chronyd 在调整期大于某个阀值时步进调整零碎时钟,但只有在因为 chronyd 启动工夫超过指定限度(可应用负值来禁用限度),没有更多时钟更新时才失效。
// 查看工夫同步源:[root@localhost ~]# chronyc sources -v
210 Number of sources = 4
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [yyyy] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 202.118.1.130                 1   7   377   118  -1213us[-1553us] +/-   20ms
^- time.cloudflare.com           3   7   337    47    +48ms[+48ms] +/-  166ms
^- ntp7.flashdance.cx            2   8   357    46    -13ms[-13ms] +/-  229ms
^+ 139.199.215.251               2   8   377    57  -7428us[-7428us] +/-   32ms

// 立刻手工同步
[root@localhost ~]# chronyc -a makestep
200 OK

// 查看工夫同步源状态:[root@localhost ~]# chronyc sourcestats -v
210 Number of sources = 4
                             .- Number of sample points in measurement set.
                            /    .- Number of residual runs with same sign.
                           |    /    .- Length of measurement set (time).
                           |   |    /      .- Est. clock freq error (ppm).
                           |   |   |      /           .- Est. error in freq.
                           |   |   |     |           /         .- Est. offset.
                           |   |   |     |          |          |   On the -.
                           |   |   |     |          |          |   samples. \
                           |   |   |     |          |          |             |
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
202.118.1.130               6   5   646    -10.813     97.702    -40us  5068us
time.cloudflare.com        11   5  1037     +0.211     13.118    +52ms  2892us
ntp7.flashdance.cx         22  12   32m     +0.089     13.917    -26ms  8756us
139.199.215.251            25  11   32m     -0.098      3.164   -514us  2062us

// 校准工夫服务器:[root@localhost ~]# chronyc tracking
Reference ID    : CA760182 (202.118.1.130)
Stratum         : 2
Ref time (UTC)  : Thu Jan 13 15:54:22 2022
System time     : 0.000000001 seconds fast of NTP time
Last offset     : -0.003181520 seconds
RMS offset      : 0.002209083 seconds
Frequency       : 7.816 ppm fast
Residual freq   : -0.157 ppm
Skew            : 3.514 ppm
Root delay      : 0.044469427 seconds
Root dispersion : 0.005420812 seconds
Update interval : 129.1 seconds
Leap status     : Normal

chronyc 命令参数阐明:

参数 参数阐明
accheck 查看 NTP 拜访是否对特定主机可用
activity 该命令会显示有多少 NTP 源在线 / 离线
add server 手动增加一台新的 NTP 服务器
clients 在客户端报告已拜访到服务器
delete 手动移除 NTP 服务器或对等服务器
settime 手动设置守护过程工夫
tracking 显示零碎工夫信息
正文完
 0