Linux系统时区错误问题-已解决

165 阅读3分钟

解决Linux时间不对的问题

1.输入date查看时间

[root@pg-xc2 ~]# date
20240519日 星期日 17:14:33 PDT

2.安装ntpdate

yum install -y ntpdate

3.删除 etc/localtime 文件,并复制shanghai时区文件替换到这里。

rm -rf /etc/localtime
[root@pg-xc2 ~]# cd /usr/share/zoneinfo/Asia/
[root@pg-xc2 Asia]# ls
Aden       Bishkek     Gaza         Kathmandu     Novokuznetsk  Seoul          Ulan_Bator
Almaty     Brunei      Harbin       Katmandu      Novosibirsk   Shanghai       Urumqi
Amman      Calcutta    Hebron       Khandyga      Omsk          Singapore      Ust-Nera
Anadyr     Chita       Ho_Chi_Minh  Kolkata       Oral          Srednekolymsk  Vientiane
Aqtau      Choibalsan  Hong_Kong    Krasnoyarsk   Phnom_Penh    Taipei         Vladivostok
Aqtobe     Chongqing   Hovd         Kuala_Lumpur  Pontianak     Tashkent       Yakutsk
Ashgabat   Chungking   Irkutsk      Kuching       Pyongyang     Tbilisi        Yangon
Ashkhabad  Colombo     Istanbul     Kuwait        Qatar         Tehran         Yekaterinburg
Atyrau     Dacca       Jakarta      Macao         Qostanay      Tel_Aviv       Yerevan
Baghdad    Damascus    Jayapura     Macau         Qyzylorda     Thimbu
Bahrain    Dhaka       Jerusalem    Magadan       Rangoon       Thimphu
Baku       Dili        Kabul        Makassar      Riyadh        Tokyo
Bangkok    Dubai       Kamchatka    Manila        Saigon        Tomsk
Barnaul    Dushanbe    Karachi      Muscat        Sakhalin      Ujung_Pandang
Beirut     Famagusta   Kashgar      Nicosia       Samarkand     Ulaanbaatar
[root@pg-xc2 Asia]# cp Shanghai /etc/localtime
[root@pg-xc2 Asia]# ll /etc/localtime 
-rw-r-xr--. 1 root root 556 5月  20 08:19 /etc/localtime

4.使用ntp更新时间

rpm -qa | grep ntp #查询已安装的ntp版本信息等

service ntpd status #查询ntp服务状态

service ntpd start #启动

service ntpd stop #停止

service ntpd restart #重启

ntpq -p #查看ntp服务器与上层ntp的状态

ntpstat #命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

ntpdate命令:将客户端时间同步为时间服务器的时间

ntpdate -u 11.10.10.111 #客户端时间同步111时间服务器

ntpdate -u cn.pool.ntp.org #指定名字同步时间

hwclock --systohc(或-w) #将系统时间同步到硬件时钟

4.1 启动ntpd服务

[root@pg-xc2 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@pg-xc2 ~]# systemctl start ntpd
[root@pg-xc2 ~]# systemctl status ntpd\
> 
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2024-05-19 17:11:25 PDT; 4s ago
  Process: 36123 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 36126 (ntpd)
    Tasks: 1
   CGroup: /system.slice/ntpd.service
           └─36126 /usr/sbin/ntpd -u ntp:ntp -g
​
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen and drop on 1 v6wildcard :: UDP 123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen normally on 2 lo 127.0.0.1 UDP 123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen normally on 3 ens33 192.168.126.129 UDP 123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen normally on 4 virbr0 192.168.122.1 UDP 123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen normally on 5 lo ::1 UDP 123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listen normally on 6 ens33 fe80::20c:29ff:fe07:667f ...123
5月 19 17:11:25 pg-xc2 ntpd[36126]: Listening on routing socket on fd #23 for interface ...tes
5月 19 17:11:25 pg-xc2 ntpd[36126]: 0.0.0.0 c016 06 restart
5月 19 17:11:25 pg-xc2 ntpd[36126]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
5月 19 17:11:25 pg-xc2 ntpd[36126]: 0.0.0.0 c011 01 freq_not_set
Hint: Some lines were ellipsized, use -l to show in full.

4.2 查看ntpd的上层服务

[root@pg-xc2 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp7.flashdance 194.58.202.20    2 u   29   64    1  258.564   -0.640   0.704
*139.199.214.202 100.122.36.196   2 u   28   64    1   46.766  -20.500   0.337
+stratum2-1.ntp. 130.173.91.58    2 u   28   64    1   94.784  -24.570   1.110
 time.cloudflare .STEP.

4.3 将客户端与时间服务器同步、或者与公共 NTP 服务器池(pool.ntp.org)同步

[root@pg-xc2 Asia]# ntpdate -u 194.58.202.20
20 May 08:38:33 ntpdate[77487]: adjust time server 194.58.202.20 offset 0.003320 sec
[root@pg-xc2 Asia]# date
2024年 05月 20日 星期一 08:38:43 CST
[root@pg-xc2 Asia]# ntpdate -u pool.ntp.org
20 May 08:39:58 ntpdate[79613]: adjust time server 202.112.29.82 offset -0.045242 sec

4.4 时间同步完成,写入硬件时间避免丢失

hwclock -w
[root@pg-xc2 Asia]# hwclock -w
[root@pg-xc2 Asia]# date
20240520日 星期一 08:41:42 CST