CentOS7设置时间、时区,系统时间,硬件时间,时间同步,NTP
.

.

.

.

.

.

.

时钟

CentOS 7 查看时间
[[email protected] ~]# date;hwclock;timedatectl
#date
Wed Jan 16 16:27:17 CST 2019
#hwclock
Wed 16 Jan 2019 04:27:01 PM CST -0.522806 seconds
#timedatectl
Local time: Wed 2019-01-16 16:27:18 CST #本地时间,CST
Universal time: Wed 2019-01-16 08:27:18 UTC #世界时间,UTC
RTC time: Wed 2019-01-16 08:27:01 #硬件时间,RTC
Time zone: Asia/Shanghai (CST, +0800) #时区
NTP enabled: no #NTP是否已开启
NTP synchronized: no #NTP是否已同步
RTC in local TZ: no #
DST active: n/a #

CST: Central Standard Time, 中国标准时间
UTC: Coordinated Universal Time,协调世界时间
RTC: Real-time Clock,硬件时钟(hardware clock)
NTP: Network Time Protocol,网络时钟协议

CST在这里指的是China Standard Time(中国标准时间,UTC+8),而不是Central Standard Time(中央标准时间,UTC-6)

查看系统时间

[[email protected] ~]# date
Wed Jan 16 16:32:52 CST 2019

查看硬件时间

[[email protected] ~]# hwclock
Wed 16 Jan 2019 04:32:57 PM CST -0.537587 seconds

修改时间

  
[[email protected] ~]# date -s "00:00:00"
[[email protected] ~]# date -s "2018-01-31 17:00:00"
[[email protected] ~]# timedatectl set-time "00:00:00"
[[email protected] ~]# timedatectl set-time "2018-01-31 17:00:00"

使用date -s 修改系统时间,UTC时间会自动根据系统时间减8小时,而硬件时间不会随之改变。

[[email protected] ~]# date -s "00:00:00"
Thu Jan 17 00:00:00 CST 2019

[[email protected] ~]# timedatectl
Local time: Thu 2019-01-17 00:00:05 CST
Universal time: Wed 2019-01-16 16:00:05 UTC
RTC time: Wed 2019-01-16 16:58:43
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

使用timedatectl set-time 修改本地系统时间,则UTC时间会自动根据本地时间减8小时,而硬件时间如果没有与本地时间同步,就取UTC时间的值。

查看当前时间
[[email protected] ~]# timedatectl
Local time: Mon 2018-01-01 00:58:17 CST
Universal time: Sun 2017-12-31 16:58:17 UTC
RTC time: Sun 2017-12-31 16:58:18
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

修改时间
[[email protected] ~]# timedatectl set-time "00:00:00"

再次查看时间
[[email protected] ~]# timedatectl
Local time: Mon 2018-01-01 00:00:02 CST
Universal time: Sun 2017-12-31 16:00:02 UTC
RTC time: Sun 2017-12-31 16:00:02
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

硬件时间的同步

修改/etc/sysconfig/ntpdate中的SYNC_HWCLOCK=yes,当系统时间从NTP进行同步时,硬件时间自动从系统时间进行同步(每11分钟同步1次?)

CentOS 7 最小化安装时,没有/etc/sysconfig/ntpdate文件

[[email protected] ~]# cat /etc/sysconfig/ntpdate 
Options for ntpdate
OPTIONS="-p 2"
Number of retries before giving up
RETRIES=2
Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=no

系统时间和硬件时间之间的同步

手动对系统时间和硬件时间进行同步

[email protected]:~# hwclock --help
Usage: hwclock [function] [option...]
Functions:
-s, --hctosys set the system time from the hardware clock
-w, --systohc set the hardware clock from the current system time
-s, 将系统时间设置成硬件的时间(改变的是系统时间,硬件时间不变)
-w, 将硬件时间设置成系统的时间(改变的是硬件时间,系统时间不变)
在CentOS7.4中,clock只是一个链接文件,链接到/usr/sbin/hwclock
可以说clcok和hwclock是同一个命令

[[email protected] ~]# type date
date is /usr/bin/date
[[email protected] ~]# type clock
clock is hashed (/usr/sbin/clock)
[[email protected] ~]# type hwclock
hwclock is hashed (/usr/sbin/hwclock)
[[email protected] ~]# ll /usr/bin/date
-rwxr-xr-x. 1 root root 62200 Nov 6 2016 /usr/bin/date
[[email protected] ~]# ll /usr/sbin/clock
lrwxrwxrwx. 1 root root 7 Dec 22 10:44 /usr/sbin/clock -> hwclock
[[email protected] ~]# ll /usr/sbin/hwclock
-rwxr-xr-x. 1 root root 49544 Dec 2 07:28 /usr/sbin/hwclock

例1:

查看系统时间
[email protected]:~# date
Wed May 23 09:59:20 CST 2018

查看硬件时间,硬件时间比系统时间慢了13分钟
[email protected]:~# hwclock
Wed 23 May 2018 09:46:17 AM CST -0.250495 seconds

将系统时间设置成硬件的时间(改变的是系统时间,硬件时间不变)
[email protected]:~# hwclock -s

再次查看系统时间,与硬件时间一致了
[email protected]:~# date
Wed May 23 09:46:28 CST 2018
[email protected]:~# hwclock
Wed 23 May 2018 09:46:34 AM CST -0.641335 seconds

例2:

查看当前时间
[[email protected] ~]# date;hwclock;timedatectl
Mon Jan 1 00:55:57 CST 2018
Mon 01 Jan 2018 12:55:58 AM CST -0.505783 seconds
Local time: Mon 2018-01-01 00:55:57 CST
Universal time: Sun 2017-12-31 16:55:57 UTC
RTC time: Sun 2017-12-31 16:55:58
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

修改系统时间
[[email protected] ~]# date -s "2018-01-01 00:00:00"
Mon Jan 1 00:00:00 CST 2018

查看当前时间
[[email protected] ~]# date;hwclock;timedatectl
Mon Jan 1 00:00:02 CST 2018
Mon 01 Jan 2018 12:58:42 AM CST -0.475867 seconds
Local time: Mon 2018-01-01 00:00:03 CST
Universal time: Sun 2017-12-31 16:00:03 UTC
RTC time: Sun 2017-12-31 16:58:42
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
修改系统时间后(date -s),hwclock查看到的时间没有改变,timedatectl查看到的RTC时间也没有改变

将硬件时间与系统时间同步
[[email protected] ~]# hwclock -w

查看当前时间
[[email protected] ~]# date;hwclock;timedatectl
Mon Jan 1 00:00:29 CST 2018
Mon 01 Jan 2018 12:00:29 AM CST -0.084489 seconds
Local time: Mon 2018-01-01 00:00:29 CST
Universal time: Sun 2017-12-31 16:00:29 UTC
RTC time: Sun 2017-12-31 16:00:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
将硬件时间与系统时间同步后(hwclock -w),时间时间一致了,但显示方式不一样。
date
Mon Jan 1 00:00:29 CST 2018
hwclock
Mon 01 Jan 2018 12:00:29 AM CST

查看时区

 [[email protected] ~]# date -R
Thu, 17 Jan 2019 02:51:08 +0800

[[email protected] ~]# timedatectl list-timezones
Asia/Shanghai

修改时区

[[email protected] ~]# 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) Beijing Time
2) Xinjiang Time
? 1
The following information has been given:
China Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Thu Jan 17 02:48:13 CST 2019.
Universal Time is now: Wed Jan 16 18:48:13 UTC 2019.
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

timedatectl

[email protected]:~# timedatectl --help
timedatectl [OPTIONS…] COMMAND …
Query or change system time and date settings.
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[[email protected]]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
Commands:
status Show current time settings
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization

如果开启了NTP同步,无法手动修改时间。需要关闭先关闭NTP同步,再手动修改。

[[email protected] ~]# timedatectl
Local time: Thu 2019-01-17 10:44:33 CST
Universal time: Thu 2019-01-17 02:44:33 UTC
RTC time: Thu 2019-01-17 02:44:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

[[email protected] ~]# timedatectl set-time "00:00:00"
Failed to set time: Automatic time synchronization is enabled

关闭NTP同步
[[email protected] ~]# timedatectl set-ntp no

再次修改时间
[[email protected] ~]# timedatectl set-time "00:00:00"
修改成功时没有提示

使用timedatectl修改日期时间后,查看时间
[[email protected] ~]# date;hwclock;timedatectl
#date
Thu Jan 17 00:42:13 CST 2019
#hwclock
Thu 17 Jan 2019 12:42:14 AM CST -0.053889 seconds
#timedatectl
Local time: Thu 2019-01-17 00:42:13 CST
Universal time: Wed 2019-01-16 16:42:13 UTC
RTC time: Wed 2019-01-16 16:42:14
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

从上面的显示来看
date显示的是系统时间,timedatectl set-time修改的也是系统时间
hwclock显示的是硬件时间

Network time on: no/yes
由 timedatectl set-ntp yes/no 命令进行设置,yes开启NTP,no关闭NTP
NTP synchronized: no/yes
时间是否已经与NTP服务器同步。这里的同步指的是,本系统作为NTP客户端,与NTP服务器进行的同步。

设置NTP服务器

如果没有安装NTP,需要先安装。程序名称是”ntp”,进程名称是”ntpd”

[[email protected] ~]# yum install ntp
[[email protected] ~]# systemctl start ntpd
[[email protected] ~]# systemctl enable ntpd
查看NTP配置
[[email protected] ~]# cat /etc/ntp.conf
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
查看NTP状态
[[email protected] ~]# ntpq -p
ntpq: read: Connection refused
因为NTP服务没有启动,提示connection refused
启动ntpd服务
[[email protected] ~]# systemctl start ntpd
查看ntpd状态
[[email protected] ~]# systemctl status ntpd
查看是否监听NTP的udp 123端口
[[email protected] ~]# netstat -ulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 192.168.122.1:123 0.0.0.0:* 2245/ntpd
udp 0 0 192.168.8.131:123 0.0.0.0:* 2245/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2245/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2245/ntpd
udp6 0 0 fe80::b118:315b:ed8:123 :::* 2245/ntpd
udp6 0 0 ::1:123 :::* 2245/ntpd
udp6 0 0 :::123 :::* 2245/ntpd

查看ntp状态,以下是未同步的状态
[[email protected] ~]# ntpq -pn
remote refid st t when poll reach delay offset jitter
51.75.17.219 .INIT. 16 u - 64 0 0.000 0.000 0.000
37.187.100.18 .INIT. 16 u - 64 0 0.000 0.000 0.000
5.103.139.163 .INIT. 16 u - 64 0 0.000 0.000 0.000
173.255.246.13.INIT. 16 u - 64 0 0.000 0.000 0.000
查看ntp状态,收下是已同步的状态
[[email protected] ~]# ntpq -pn
remote refid st t when poll reach delay offset jitter
-51.75.17.219 213.251.128.249 3 u 14 64 17 226.470 5.640 9.475
+37.187.100.18 131.188.3.221 2 u 17 64 15 214.443 16.205 5.103
*5.103.139.163 .GPS. 1 u 14 64 13 275.882 -12.305 50.543
+173.255.246.13204.123.2.72 2 u 84 64 16 187.217 9.355 3.514

2 Responses

  1. v2ray - NP  2019年3月1日

    […] 在服务器安装ntpd并开启NTP同步后,可以正常访问了NTP的设置可以参见这篇文章 […]

  2. VMware完全禁止虚拟机的时钟同步 - NP  2019年4月10日

    […] CentOS 7 设置时间、时区 […]

发表评论

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