时间同步问题
查看系统当前时间,date命令
# date
2021年 07月 22日 星期四 16:31:07 CST
查看系统当前时区
# timedatectl
Local time: 四 2021-07-22 16:31:38 CST
Universal time: 四 2021-07-22 08:31:38 UTC
RTC time: 四 2021-07-22 08:31:37
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
只看时区
你可以在命令中提供一条管道,然后用 grep 命令来像下面一样,只过滤出时区信息:
$ timedatectl | grep “Time zone”
或者
date -R
修改时区
方法1:
使用tzselect设置时区
[root@db-server ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) east China - Beijing, Guangdong, Shanghai, etc.
2) Heilongjiang (except Mohe), Jilin
3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.
4) most of Tibet & Xinjiang
5) west Tibet & Xinjiang
#? 1
The following information has been given:
China
east China - Beijing, Guangdong, Shanghai, etc.
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Sun Jan 11 23:31:51 CST 2015.
Universal Time is now: Sun Jan 11 15:31:51 UTC 2015.
Is the above information OK?
1) Yes
2) No
#? yes
Please enter 1 for Yes, or 2 for No.
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
注意:tzselect命令只告诉你选择的时区的写法,并不会生效。所以现在它还不是东8区北京时间。你可以在.profile、.bash_profile或者/etc/profile中设置正确的TZ环境变量并导出。 例如在.bash_profile里面设置 TZ=‘Asia/Shanghai’; export TZ并使其生效。
[root@db-server ~]# source .bash_profile
[root@db-server ~]# date
Sun Jan 11 23:37:40 CST 2015
方法2:
复制相应的时区文件,替换系统时区文件;或者创建链接文件
在/usr/share/zoneinfo/下面有很多时区文件,如下所示,可以复制这些时区文件覆盖/etc/localtime文件,或修改符号链接/etc/locatime对应的文件
[root@db-server ~]# date -R
Mon, 12 Jan 2015 10:42:26 +0800
[root@db-server ~]# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
cp: overwrite `/etc/localtime'? y
[root@db-server ~]# date -R
Sun, 11 Jan 2015 18:42:49 -0800
[root@db-server ~]#
注意如果有时候,执行了上面命令后,使用date -R发现时区设置没有生效,有可能是因为你在profile或.bash_profile里面设置了TZ,如下案例所示:
[root@db-server ~]# date -R
Sun, 11 Jan 2015 18:42:49 -0800
[root@db-server ~]# vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
方式2:修改链接/etc/locatime的时区文件
[root@db-server ~]# ln /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ln: creating hard link /etc/localtime' to
/usr/share/zoneinfo/Asia/Shanghai’: File exists
[root@db-server ~]# rm /etc/localtime
rm: remove regular file `/etc/localtime’? y
[root@db-server ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@db-server ~]# date -R
Mon, 12 Jan 2015 10:56:10 +0800
[root@db-server ~]#
linux时间同步方法
Linux 时间同步 Chrony
chrony 是网络时间协议(NTP)的通用实现。
chrony 包含两个程序:chronyd 是一个可以在启动时启动的守护程序。chronyc 是一个命令行界面程序,用于监视 chronyd 的性能并在运行时更改各种操作参数。
与其它时间同步软件的对比:https://chrony.tuxfamily.org/comparison.html
一、安装与配置
yum -y install chrony
systemctl enable chronyd
systemctl start chronyd
vim /etc/chrony.conf
chrony.conf 默认配置
# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# 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
# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3
# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# 通过使用 hwtimestamp 指令启用硬件时间戳
# 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
# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# 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
# 指定包含 NTP 身份验证密钥的文件。
# 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
二、使用
ntp 服务器:https://www.cnblogs.com/jhxxb/p/10579816.html
1.服务端配置
chrony.conf 修改两处
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst
# Allow NTP client access from local network.
allow 192.168.8.0/24
开启同步
systemctl enable chronyd
systemctl restart chronyd
# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true
2.客户端配置
chrony.conf 修改两处
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst
# Allow NTP client access from local network.
allow 192.168.8.5
开启同步
systemctl enable chronyd
systemctl restart chronyd
# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true
三、命令
chronyc 用法
# 查看 ntp_servers
chronyc sources -v
# 查看 ntp_servers 状态
chronyc sourcestats -v
# 查看 ntp_servers 是否在线
chronyc activity -v
# 查看 ntp 详细信息
chronyc tracking -v
修改时区
# 查看日期时间、时区及 NTP 状态
timedatectl
# 查看时区列表
timedatectl list-timezones
timedatectl list-timezones | grep -E "Asia/S.*"
# 修改时区
timedatectl set-timezone Asia/Shanghai
# 修改日期时间(可以只修改其中一个)
timedatectl set-time "2019-09-19 15:50:20"
# 开启 NTP
timedatectl set-ntp true/flase