linux 如何修改时区

2016年11月8日
date -R

2012年11月18日 星期日 19时33分23秒 CSTLinux系统下使用网络对时的工具是ntpdate,如果没有安装ntpdate的话,需要安装ntpdate:在Centos下:
代码如下:

#yum -y install ntpdate ntp
在Debian或者Ubuntu下:
#sudo apt-get install -y ntpdate ntpntpdate


安装后就可以使用ntpdate来更新Linux系统的时间了(我们使用windows的time服务器对时):
代码如下:

#ntpdate time.windows.com


更改VPS的时区
如果VPS默认是非中国时区的话,如下命令可以用来更改为中国时区:
代码如下:

#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


注意:在Openvz VPS下,只可以修改时区,但是无法修改VPS的时间。因为Openvz的时间是和VPS服务器同步的。如果你发现您的Openvz VPS时间不对的话,可以通知VPS的销售商来对时。
**************************************************************************************************
常见用法
使用timedatectl可以进行如下常见操作

  1. 查看当前时间/日期/时区:timedatectl或者timedatectl status
  2. 查看所有可用时区:timedatectl list-timezones
  3. 设置时区:timedatectl set-timezone “时区信息”
  4. 设置UTC:timedatectl set-timezone UTC
  5. 设置时间:timedatectl set-time HH:MM:SS
  6. 设置日期:timedatectl set-time YYYY-MM-DD
  7. 设置日期时间:timedatectl set-time “YYYY-MM-DD HH:MM:SS”
  8. 设置硬件时钟为本地时间:timedatectl set-local-rtc 1
  9. 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
  10. 启动NTP时间同步(启用NTP服务或者Chrony服务):timedatectl set-ntp true
  11. 禁用NTP时间同步:timedatectl set-ntp false

查看当前时间/日期/时区 ; 命令:timedatectl或者timedatectl status


[root@platform ~]# timedatectl status
Local time: Fri 2019-03-08 06:07:19 EST
Universal time: Fri 2019-03-08 11:07:19 UTC
RTC time: Fri 2019-03-08 09:41:48
Time zone: America/New_York (EST, -0500)
NTP enabled: yesNTP synchronized: no
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2018-11-04 01:59:59 EDT
Sun 2018-11-04 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2019-03-10 01:59:59 EST
Sun 2019-03-10 03:00:00 EDT[root@platform ~]#

查看所有可用时区 ; 命令:timedatectl list-timezones


[root@platform ~]# timedatectl list-timezonesAfrica/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
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
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Juba
Africa/Kampala
Africa/Khartoum
Africa/Kigali
lines 1-29


设置时区 ; 命令:timedatectl set-timezone “时区信息”
 

[root@platform ~]# timedatectl set-timezone "Asia/Shanghai"
[root@platform ~]# timedatectlLocal time: Fri 2019-03-08 19:13:38 CST
Universal time: Fri 2019-03-08 11:13:38 UTC
RTC time: Fri 2019-03-08 09:47:38
Time zone: Asia/Shanghai(CST, +0800)NTP enabled:yesNTP synchronized: no
RTCinlocal TZ: no
DST active: n/a

设置UTC
命令:timedatectl set-timezone UTC
执行示例 

[root@platform ~]# timedatectl
Local time: Fri 2019-03-08 19:14:48 CST
Universal time: Fri 2019-03-08 11:14:48 UTC
RTC time: Fri 2019-03-08 09:48:42
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: no
RTC in local TZ: no
DST active: n/a[root@platform ~]# timedatectl set-timezone UTC[root@platform ~]# timedatectl status
Local time: Fri 2019-03-08 11:15:05 UTC
Universal time: Fri 2019-03-08 11:15:05 UTC
RTC time: Fri 2019-03-08 09:48:59
Time zone: UTC (UTC, +0000)
NTP enabled: yesNTP synchronized: no
RTC in local TZ: no
DST active: n/a[root@platform ~]#1234567891011121314151617181920

设置时间
命令:timedatectl set-time HH:MM:SS
执行示例

[root@platform ~]# timedatectl set-timezone "Asia/Shanghai"
[root@platform ~]# dateFri Mar  8 19:15:36 CST 2019
[root@platform ~]# 
[root@platform ~]# timedatectl set-time "20:45:00"Failed to set time: Automatic time synchronization is enabled
[root@platform ~]# dateFri Mar  8 19:16:20 CST 2019
[root@platform ~]# 
[root@platform ~]# timedatectl set-ntp false
[root@platform ~]# timedatectl set-time "20:45:00"
[root@platform ~]# dateFri Mar  8 20:45:01 CST 2019
[root@platform ~]#1234567891011121314

注意: 如果ntp时间同步为true时无法修改时间设定
设置日期
命令:timedatectl set-time YYYY-MM-DD
执行示例

[root@platform ~]# timedatectl set-ntp false
[root@platform ~]# timedatectl set-time 2019-03-10
[root@platform ~]# dateSun Mar 10 00:00:01 CST 2019
[root@platform ~]#12345


注意: 如果ntp时间同步为true时无法修改时间设定
设置日期时间
命令:timedatectl set-time “YYYY-MM-DD HH:MM:SS”
执行示例


[root@platform ~]# timedatectl set-ntp false
[root@platform ~]# timedatectl set-time "2019-03-11 20:45:00"
[root@platform ~]# dateMon Mar 11 20:45:01 CST 2019
[root@platform ~]# 12345


注意: 如果ntp时间同步为true时无法修改时间设定
设置硬件时钟为本地时间
命令:timedatectl set-local-rtc 1
执行示例
[root@platform ~]# timedatectl
Local time: Mon 2019-03-11 20:58:12 CST
Universal time: Mon 2019-03-11 12:58:12 UTC
RTC time: Mon 2019-03-11 12:46:14
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: yes
RTC in local TZ: no
DST active: n/a[root@platform ~]# timedatectl set-local-rtc 1[root@platform ~]# timedatectl status
Local time: Mon 2019-03-11 20:58:16 CST
Universal time: Mon 2019-03-11 12:58:16 UTC
RTC time: Mon 2019-03-11 20:58:16
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC         time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling         'timedatectl set-local-rtc 0'.
[root@platform ~]#123456789101112131415161718192021222324252627


设置硬件时钟为UTC时间
命令:timedatectl set-local-rtc 0
执行示例


[root@platform ~]# timedatectl
Local time: Mon 2019-03-11 20:59:18 CST
Universal time: Mon 2019-03-11 12:59:18 UTC
RTC time: Mon 2019-03-11 20:59:17
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC         time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling         'timedatectl set-local-rtc 0'.
[root@platform ~]# [root@platform ~]# timedatectl set-local-rtc 0[root@platform ~]# timedatectl status

Local time: Mon 2019-03-11 20:59:27 CST
Universal time: Mon 2019-03-11 12:59:27 UTC
RTC time: Mon 2019-03-11 12:59:27
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: yes
RTC in local TZ: no
DST active: n/a[root@platform ~]#12345678910111213141516171819202122232425262728

启动NTP时间同步(启用NTP服务或者Chrony服务)
命令:timedatectl set-ntp true
执行示例


[root@platform ~]# timedatectl status
Local time: Mon 2019-03-11 21:00:19 CST
Universal time: Mon 2019-03-11 13:00:19 UTC
RTC time: Mon 2019-03-11 13:00:19
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a[root@platform ~]# timedatectl set-ntp true[root@platform ~]# timedatectl status
Local time: Mon 2019-03-11 21:00:24 CST
Universal time: Mon 2019-03-11 13:00:24 UTC
RTC time: Mon 2019-03-11 13:00:24
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: no
RTC in local TZ: no
DST active: n/a[root@platform ~]#1234567891011121314151617181920

禁用NTP时间同步
命令:timedatectl set-ntp false
执行示例


[root@platform ~]# timedatectl status
Local time: Mon 2019-03-11 21:00:45 CST
Universal time: Mon 2019-03-11 13:00:45 UTC
RTC time: Mon 2019-03-11 13:00:44
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yesNTP synchronized: yes
RTC in local TZ: no
DST active: n/a[root@platform ~]# timedatectl set-ntp false
[root@platform ~]# timedatectl status
Local time: Mon 2019-03-11 21:00:50 CST
Universal time: Mon 2019-03-11 13:00:50 UTC
RTC time: Mon 2019-03-11 13:00:50
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@platform ~]#


------------------------------
PS:另一个方法


[root@slave1 logs]#date
Tue Aug 28 21:37:04 EDT 2018
[root@slave1 logs]#mv /etc/localtime /etc/localtime.bak
[root@slave1 logs]#ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@slave1 logs]#date
Wed Aug 29 09:41:07 CST 2018

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注