ntp
本例以局域网为环境进行配置,假设局域网ntp服务器与公网时钟源进行同步,局域网内部服务器与局域网内部ntp服务器进行同步。
服务端配置
以下为默认配置
[root@ntp-server ~]# egrep -v "^#|^$" /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery #把此行注释掉
restrict 127.0.0.1
restrict ::1
server 127.127.1.0 iburst #没有公网时钟源,添加此行与下面一行,把本机当做时钟源,有公网时钟源,只添加一行server即可
127.127.1.0 stratum 8
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
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
[root@ntp-server ~]# systemctl start ntpd
[root@ntp-server ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@ntp-server ~]# date
Tue Jul 11 14:04:41 CST 2023
[root@ntp-server ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 3 64 1 0.000 0.000 0.000
客户端配置
[root@ntp-client ~]# date -s "-1 year"
Mon Jul 11 13:49:25 CST 2022
[root@ntp-client ~]# ntpq -p
ntpq: read: Connection refused
[root@ntp-client ~]# date
Mon Jul 11 14:02:53 CST 2022
[root@ntp-client ~]# vim /etc/ntp.conf
[root@ntp-client ~]# egrep -v "^$|^#" /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 22.22.22.201 iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
[root@ntp-client ~]# systemctl start ntpd
[root@ntp-client ~]# ntpq -p
[root@ntp-client ntpstats]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*22.22.22.201 LOCAL(0) 6 u 12 64 1 0.213 -0.097 0.083
[root@ntp-client ~]#
chrony
Chrony服务端&客户端配置
[root@han ~]# vim /etc/chrony.conf
[root@han ~]# egrep -v "^$|^#" /etc/chrony.conf
server ntp.aliyun.com iburst #时钟服务器,当客户端时,仅配置此项即可
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.0.0/16 #允许客户端访问的IP
local stratum 10 #ntp服务器连接异常也可以为时钟客户端提供服务
logdir /var/log/chrony
[root@han ~]# systemctl start chronyd
[root@han ~]# chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.
chronyc> activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
chronyc> sources -v
210 Number of sources = 1
.-- 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
===============================================================================
^* 203.107.6.88
chronyc> clients
Hostname NTP Drop Int IntL Last Cmd Drop Int Last
===============================================================================
192.168.1.17 9 0 3 - 7 0 0 - -
chronyc> quit
[root@han ~]#
时间相关配置
hwclock命令
查询和设置硬件时间
timedatectl
设置(查看)系统时间与时区
timedatectl set-timezone Asia/Shanghai >>>服务器时区设置
查看时间与时区
[root@k8s-node01 ~]# timedatectl status
Local time: Tue 2023-10-10 13:24:04 CST
Universal time: Tue 2023-10-10 05:24:04 UTC
RTC time: Tue 2023-10-10 05:24:04
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@k8s-node01 ~]#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ##直接复制也可以设置时区,用命令设置是创建链接文件
ntpdate
手动与时钟服务器进行时钟同步
Linux时区相关设置
在Linux系统上,读取时区步骤
1、先找TZ变量,没有此变量到2
2、读取/etc/timezone,没有到3(java程序
默认读取该文件,可以在容器中创建此文件,echo “Asia/shanghai” >/etc/timezone ;java程序还可以通过jvm参数将时区信息传进jvm里:-Duser.timezone=GMT+08)
3、比较/etc/localtime文件与“/usr/share/zoneinfo”目录下所有时区文件,如果一致,就为该时区,如果没有到4
4、默认为标准GMT