CentOS7 修改服务器时区

查看时区设置

timedatectl

列出所有时区,通过键盘上下键进行浏览

timedatectl list-timezones

修改服务器时区为Africa/Lagos

# 拉各斯的时区,UTC+1
timedatectl set-timezone Africa/Lagos
# 北京时间是UTC+8
timedatectl set-timezone Asia/Shanghai

或者直接设置为UTC标准时间

cp /usr/share/zoneinfo/UTC /etc/localtime

复制之后,然后输入 yes,即可。

[root@cssbjqnffcsvic ccxt]# date
Mon Apr 27 10:00:01 UTC 2020
[root@cssbjqnffcsvic ccxt]# timedatectl
      Local time: Mon 2020-04-27 10:03:09 UTC
  Universal time: Mon 2020-04-27 10:03:09 UTC
        RTC time: Mon 2020-04-27 10:02:30
       Time zone: Asia/Shanghai (UTC, +0000)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

Centos7服务器时间不同步/修改时区

1. 修改时区/同步网络时间

   NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

   安装ntp server :           yum install ntp

   启动服务:                      service ntpd start

   设置开机启动:               systemctl enable ntpd

   修改时区:                      cp  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

修改为北京时间:

timedatectl set-timezone Asia/Shanghai    #执行完后时间还需要重启ntpd服务
systemctl restart ntpd
systemctl enable ntpd

到这里就配置好了,需要注意的是。执行完一下命令后并不是立刻就改好时间了,大概需要半分钟才能生效。

如果过了一大会,还没生效,我们可以查时区设置:

[root@cssbjqnffcsvic ccxt]# timedatectl
      Local time: Tue 2020-04-28 09:43:51 UTC
  Universal time: Tue 2020-04-28 09:43:51 UTC
        RTC time: Tue 2020-04-28 09:43:51
       Time zone: Asia/Shanghai (UTC, +0000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

修改成 Asia/Shanghai 但是 时区总是 Time zone: Asia/Shanghai (UTC, +0000) 却不是想要的+0800,那么只需设置配置文件即可:
/etc/profile 中简单设置一下
export TZ='CST-8',即时生效一下就全部搞定。

[root@css ccxt]# vim /etc/profile
# timezone,beijing(20200428)
export TZ='CST-8'

重新加载系统文件:

[root@css ccxt]# source /etc/profile

然后再试下,时间变为北京时间了

[root@cssc ccxt]# date
Tue Apr 28 17:47:37 CST 2020

2. 修改时间

    date mmddhhmmyyyy(月日时分年)
    例:date 010114302019        2019年1月1日14:30

1、查看Linux系统时间:
date

2、修改Linux系统时间:
date -s 06/10/96
date -s 13:12:00
date -s "06/10/1996 13:12:00" #这里的格式是 "MM/DD/YYYY hh:mm:ss"
date 061013121996.00 #这里的格式是 MMDDhhmmYYYY.ss (月日时分年.秒)

3、查看Linux硬件时间:
hwclock
or
hwclock –show

4、修改Linux硬件时间:
hwclock --set --date="09/17/2003 13:26:00"

5、让系统时间与硬件时钟同步:
hwclock –hctosys

6、让硬件时钟与系统时间同步:
hwclock –systohc(-w)

3、终极修改方法

(base) [root@css ~]# timedatectl
      Local time: Fri 2020-05-08 22:11:41 PDT
  Universal time: Sat 2020-05-09 05:11:41 UTC
        RTC time: Sat 2020-05-09 05:11:31
       Time zone: America/Los_Angeles (PDT, -0700)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2020-03-08 01:59:59 PST
                  Sun 2020-03-08 03:00:00 PDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2020-11-01 01:59:59 PDT
                  Sun 2020-11-01 01:00:00 PST
(base) [root@css ~]# yum install ntp

(base) [root@css ~]# service ntpd start
Redirecting to /bin/systemctl start ntpd.service
(base) [root@css ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
(base) [root@css ~]# cp  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
cp: overwrite ‘/etc/localtime’? y
(base) [root@css ~]# timedatectl set-timezone Asia/Shanghai
(base) [root@css ~]# systemctl restart ntpd
(base) [root@css ~]# timedatectl
      Local time: Sat 2020-05-09 13:15:03 CST
  Universal time: Sat 2020-05-09 05:15:03 UTC
        RTC time: Sat 2020-05-09 05:15:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

为者常成,行者常至