Linux时间设置

一、关于时间

有时候我们会发现我们调整了Linux的时间,但是重启之后,发现时间又对不上了,瞬间有种很蛋疼的感觉有没有?因为在Linux系统中有两个时间,一个是硬件时间,还有一个是系统时间。我们通常所表达的都是指系统时间,一般情况下我们修改的也是系统时间。而Linux系统在启动时会去读取硬件时间,所以你会发现你重启后,你系统的时间又不对的“感觉”。

二、设置时间

查看系统时间:

[root@localhost ~]# date
2018年 10月 21日 星期日 17:18:26 CST 

格式化系统时间

[root@localhost etc]# date "+%Y-%m-%d %H:%M:%S"
2018-10-21 18:30:50

查看硬件时间

[root@localhost ~]# hwclock 
2018年10月21日 星期日 17时21分42秒  -0.405552

设置当前系统时间

[root@localhost ~]# date -s "2018-10-21 17:25"
2018年 10月 21日 星期日 17:25:00 CST
[root@localhost ~]# date
2018年 10月 21日 星期日 17:25:02 CST

将系统时间同步到硬件时钟

[root@localhost ~]# hwclock --systohc
[root@localhost ~]# date && hwclock 
2018年 10月 21日 星期日 17:30:50 CST
2018年10月21日 星期日 17时30分50秒  -0.037117

三、使用网络时间

使用网络时间同步系统时间

[root@localhost ~]# rpm -qa  | grep ntp
# 查看是否安装ntp服务

[root@localhost ~]# yum install ntp
# 安装ntp服务

[root@localhost ~]# ntpdate time.windows.com
21 Oct 17:47:07 ntpdate[3120]: step time server 51.140.65.84 offset -25.155433 sec
# 将系统时间设置为当前时间服务器的时间

[root@localhost ~]# hwclock --systohc
# 将当前系统时间同步硬件时间

使用ntpd服务自动同步系统时间

[root@localhost etc]# systemctl start ntpd
[root@localhost etc]# ps -ef | grep ntpd
ntp        3274      1  0 18:13 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root       3276   2613  0 18:13 pts/0    00:00:00 grep --color=auto ntpd

这/etc/ntp.conf这个配置文件中21行左右设置了3个默认的服务器,当我们启动这个这个服务的时候,它就会帮我么自动同步系统时间了。 

四、时间问题排查

主要的排查思路就是查看系统的时区,如果时区和当前地区吻合。那么就是硬件时间和系统时间不一致导致的使用上面的方式设置就好了。

查看当前系统时区

[root@localhost etc]# date -R
Sun, 21 Oct 2018 18:15:53 +0800

设置当前系统时区

[root@localhost etc]# 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:    Sun Oct 21 18:23:59 CST 2018.
Universal Time is now:    Sun Oct 21 10:23:59 UTC 2018.
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

猜你喜欢

转载自www.cnblogs.com/xsblog/p/9824544.html
今日推荐