侧边栏壁纸
博主头像
v林羽博主等级

行动起来,活在当下

  • 累计撰写 171 篇文章
  • 累计创建 34 个标签
  • 累计收到 14 条评论

目 录CONTENT

文章目录

【Linux使用】之--系统时区设置、时间同步及修改

v林羽
2022-12-08 / 0 评论 / 0 点赞 / 553 阅读 / 12522 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

【Linux使用】之–系统时区设置、时间同步及修改

#Linux #教程 #手册 #系统 #Ubuntu

1. 设置时区

1.1. 使用Systemd更改Linux系统时区

systemd下有一个名为systemd-timedated 的系统服务负责调整系统时钟和时区,可以使用 timedatectl命令对此系统服务进行配置。

root@ubuntu:~# timedatectl set-timezone 'Asia/Shanghai'
root@ubuntu:~# date
Sun Dec  4 04:57:47 PM CST 2022
root@ubuntu:~# date -R
Sun, 04 Dec 2022 16:57:54 +0800

如果需要设置其他时区,可以通过timedatectl list-timezones获取

root@ubuntu:~# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam

可以执行timedatectl set-timezone UTC,重新切换为UTC

使用echo "Asia/Shanghai" > /etc/timezone,但不会立即生效。

root@ubuntu:~# echo "Europe/London" > /etc/timezone
root@ubuntu:~# date -R
Sun, 04 Dec 2022 17:00:53 +0800
root@ubuntu:~# timedatectl set-timezone 'Europe/London'
root@ubuntu:~# date -R
Sun, 04 Dec 2022 19:02:16 +0000
root@ubuntu:~# 

1.2. 修改用户配置文件

通过tzselect命令,获取对应时区的写法,在.profile.bash_profile/etc/profile中修改,这样可以为linux每个用户设置不同的时区。

root@ubuntu:~# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa                                                            7) Europe
2) Americas                                                          8) Indian Ocean
3) Antarctica                                                        9) Pacific Ocean
4) Asia                                                             10) coord - I want to use geographical coordinates.
5) Atlantic Ocean                                                   11) TZ - I want to specify the timezone using the Posix TZ format.
6) Australia
# ? 4
Please select a country whose clocks agree with yours.
 1) Afghanistan                        15) French Southern & Antarctic Lands  29) Kyrgyzstan                         43) Russia
 2) Antarctica                         16) Georgia                            30) Laos                               44) Saudi Arabia
 3) Armenia                            17) Hong Kong                          31) Lebanon                            45) Seychelles
 4) Azerbaijan                         18) India                              32) Macau                              46) Singapore
 5) Bahrain                            19) Indonesia                          33) Malaysia                           47) Sri Lanka
 6) Bangladesh                         20) Iran                               34) Mongolia                           48) Syria
 7) Bhutan                             21) Iraq                               35) Myanmar (Burma)                    49) Taiwan
 8) Brunei                             22) Israel                             36) Nepal                              50) Tajikistan
 9) Cambodia                           23) Japan                              37) Oman                               51) Thailand
10) China                              24) Jordan                             38) Pakistan                           52) Turkmenistan
11) Christmas Island                   25) Kazakhstan                         39) Palestine                          53) United Arab Emirates
12) Cocos (Keeling) Islands            26) Korea (North)                      40) Philippines                        54) Uzbekistan
13) Cyprus                             27) Korea (South)                      41) Qatar                              55) Vietnam
14) East Timor                         28) Kuwait                             42) Réunion                            56) Yemen
# ? 10
Please select one of the following timezones.
1) Beijing Time
2) Xinjiang Time, Vostok
# ? 1

The following information has been given:

        China
        Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Selected time is now:   Sun Dec  4 17:02:37 CST 2022.
Universal Time is now:  Sun Dec  4 09:02:37 UTC 2022.
Is the above information OK?
1) Yes
2) 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
root@ubuntu:~# date
Sun Dec  4 09:03:15 AM UTC 2022
root@ubuntu:~# vim .profile

根据提示,在.profile末尾添加。

TZ='Asia/Shanghai'; export TZ

重新执行刚修改的初始化文档.profile

root@ubuntu:~# source .profile 
root@ubuntu:~# date
Sun Dec  4 05:11:17 PM CST 2022
root@ubuntu:~# 

1.3. 更改系统的时区

修改/etc/localtime,设置Linux整个系统的时区。

1.3.1. 方法一:替换时区文件

root@ubuntu:~# ls /usr/share/zoneinfo
Africa      Australia  Cuba     Etc      GMT+0      Iceland      Japan              MET      NZ-CHAT     PRC        Turkey     WET
America     Brazil     EET      Europe   GMT-0      Indian       Kwajalein          Mexico   Pacific     PST8PDT    tzdata.zi  W-SU
Antarctica  Canada     Egypt    Factory  GMT0       Iran         leapseconds        MST      Poland      right      UCT        zone1970.tab
Arctic      CET        Eire     GB       Greenwich  iso3166.tab  leap-seconds.list  MST7MDT  Portugal    ROC        Universal  zone.tab
Asia        Chile      EST      GB-Eire  Hongkong   Israel       Libya              Navajo   posix       ROK        US         Zulu
Atlantic    CST6CDT    EST5EDT  GMT      HST        Jamaica      localtime          NZ       posixrules  Singapore  UTC
root@ubuntu:~# ls /usr/share/zoneinfo/Asia/
Aden       Atyrau   Brunei      Damascus   Hebron       Jerusalem  Kolkata       Makassar      Phnom_Penh  Saigon         Tashkent  Ujung_Pandang  Yangon
Almaty     Baghdad  Calcutta    Dhaka      Ho_Chi_Minh  Kabul      Krasnoyarsk   Manila        Pontianak   Sakhalin       Tbilisi   Ulaanbaatar    Yekaterinburg
Amman      Bahrain  Chita       Dili       Hong_Kong    Kamchatka  Kuala_Lumpur  Muscat        Pyongyang   Samarkand      Tehran    Ulan_Bator     Yerevan
Anadyr     Baku     Choibalsan  Dubai      Hovd         Karachi    Kuching       Nicosia       Qatar       Seoul          Tel_Aviv  Urumqi
Aqtau      Bangkok  Chongqing   Dushanbe   Irkutsk      Kashgar    Kuwait        Novokuznetsk  Qostanay    Shanghai       Thimbu    Ust-Nera
Aqtobe     Barnaul  Chungking   Famagusta  Istanbul     Kathmandu  Macao         Novosibirsk   Qyzylorda   Singapore      Thimphu   Vientiane
Ashgabat   Beirut   Colombo     Gaza       Jakarta      Katmandu   Macau         Omsk          Rangoon     Srednekolymsk  Tokyo     Vladivostok
Ashkhabad  Bishkek  Dacca       Harbin     Jayapura     Khandyga   Magadan       Oral          Riyadh      Taipei         Tomsk     Yakutsk
root@ubuntu:~# date
Sun Dec  4 09:30:06 AM UTC 2022
root@ubuntu:~# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
root@ubuntu:~# date
Sun Dec  4 05:32:16 PM CST 2022
root@ubuntu:~# 

1.3.2. 方法二:建立时区文件软连接

root@ubuntu:~# rm -rf /etc/localtime
root@ubuntu:~# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@ubuntu:~# date
Sun Dec  4 06:02:20 PM CST 2022
root@ubuntu:~# date -R
Sun, 04 Dec 2022 18:02:30 +0800
root@ubuntu:~# 

2. 时间同步

systemd下有一个名为systemd-timedated 的系统服务负责调整系统时钟和时区。

root@ubuntu:~# timedatectl
               Local time: Sun 2022-12-04 20:15:18 CST
           Universal time: Sun 2022-12-04 12:15:18 UTC
                 RTC time: Sun 2022-12-04 12:15:19
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

可以看到System clock synchronized: yes时间同步开启,systemd-timesyncd.service服务状态情况如下。

root@ubuntu:~# systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-12-04 17:56:37 CST; 2h 21min ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 725 (systemd-timesyn)
     Status: "Initial synchronization to time server 91.189.91.157:123 (ntp.ubuntu.com)."
      Tasks: 2 (limit: 4575)
     Memory: 1.4M
        CPU: 105ms
     CGroup: /system.slice/systemd-timesyncd.service
             └─725 /lib/systemd/systemd-timesyncd

Dec 04 18:44:46 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 185.125.190.58:123 (ntp.ubuntu.com).
Dec 04 18:44:46 ubuntu systemd-timesyncd[725]: Initial synchronization to time server 185.125.190.57:123 (ntp.ubuntu.com).
Dec 04 19:00:29 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 185.125.190.57:123 (ntp.ubuntu.com).
Dec 04 19:09:01 ubuntu systemd-timesyncd[725]: Initial synchronization to time server 185.125.190.58:123 (ntp.ubuntu.com).
Dec 04 19:26:51 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 185.125.190.58:123 (ntp.ubuntu.com).
Dec 04 19:27:02 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 185.125.190.56:123 (ntp.ubuntu.com).
Dec 04 19:27:12 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 91.189.91.157:123 (ntp.ubuntu.com).
Dec 04 19:27:22 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 91.189.94.4:123 (ntp.ubuntu.com).
Dec 04 19:27:32 ubuntu systemd-timesyncd[725]: Timed out waiting for reply from 185.125.190.57:123 (ntp.ubuntu.com).
Dec 04 19:44:37 ubuntu systemd-timesyncd[725]: Initial synchronization to time server 91.189.91.157:123 (ntp.ubuntu.com).

可以通过timedatectl set-ntp true/false打开或者关闭时间同步。

3. 设置时间

3.1. 查看/设置系统时间

## 查看当前时间
root@ubuntu:~# date

## 查看当前时间及时区
root@ubuntu:~# date -R

## 将日期设置为2022年12月3日
root@ubuntu:~# date -s 12/03/22

## 将时间设置为14点20分50秒
root@ubuntu:~# date -s 14:20:50

## 将时间设置为2022年12月3日14点16分30秒(MMDDhhmmYYYY.ss)
root@ubuntu:~# date 1203141622.30

3.2. 查看/设置硬件时间

## 查看系统硬件时钟
root@ubuntu:~# hwclock  --show

## 设置硬件时间,月/日/年时:分:秒 
root@ubuntu:~# hwclock --set --date="11/03/17 14:55"

3.3. 同步系统及硬件时钟

## hc代表硬件时间,sys代表系统时间,以硬件时间为基准同步系统时间
[root@linux-node ~]# hwclock --hctosys

## 以系统时间为基准同步硬件时间
[root@linux-node ~]# hwclock --systohc
0

评论区