首页 > 系统相关 >CentOS 修改时间

CentOS 修改时间

时间:2022-10-20 01:22:05浏览次数:52  
标签:TZ CentOS etc hwclock 修改 时间 localtime

Centos系统时间分为系统时间和硬件时间。两个必须都修改,重启系统才会永久生效。

# 查询时间常用命令
1 date 2 date -R 3 date +%z 4 hwclock -r

一、修改时区

# 修改时区(创建软连接)
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 将硬件写入主板
hwclock -w 

二、修改时间

两种修改时间方式:手动修改和自动修改

1.手动修改

在没网的时候,可以使用这种方式

 1 # 修改系统时间
 2 date -s  "2019-11-01 17:28:00"
 3 # 修改硬件时间
 4 hwclock --set  --date  "2019-11-01 17:28:00"
 5 # 同步系统时间和硬件时间
 6 hwclock --hctosys
 7 # 保存(永久生效,不保存重启后失效)
 8 hwclock -w 或者  clock -w
 9 # 重启系统
10 init  6

2.自动与时间服务器上的时间同步

相比于手动修改时间,时间更准确

 1 # 安装ntpdate工具
 2 yum  -y  install  ntp  ntpdate
 3 # 设置系统时间与网络时间同步
 4 ntpdate  cn.pool.ntp.org
 5 或者
 6 ntpdate  time.windows.com
 7 # 将系统时间写入硬件时间
 8 hwclock  --systohc
 9 # 强制系统时间写入CMOS中防止重启失效
10 hwclock  -w 或者  clock  -w
11 # 重启系统
12 init  6

三、方式三

(tzselect 手动选择时区)

正确的方式是到/etc/profile里(或用户的.profile或.bashrc文件),直接export TZ变量为要更改的时区(时区的名字可以用tzselect向导来确定)

/etc/localtime文件
默认情况下情况下,TZ属性是空,这时候是靠/etc/localtime文件来确定的时区。而此文件通常又是一个到/usr/share/zoneinfo/下各种时区文件的软连接。通过修改/etc/localtime指向的软连接,进而修改系统的时区。比如下面的方法,将localtime文件设置为了北京时间:
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

总结:
tzselect命令无法修改时区,仅给出时区的城市表示法
TZ变量和/etc/localtime文件会影响时区,并建议直接修改/etc/localtime文件。
如果在shell中临时需要变更时区信息,可以修改TZ变量实现。
在profile文件里设置变量TZ,达到和修改/etc/localtime类似的效果。

------------------------------------------------------------------------------------------------------------

版权声明:本文为CSDN博主「qq_38778137」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38778137/article/details/84896150

[root@fairy ~]# 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 Oct 20 00:41:19 CST 2022.
Universal Time is now:    Wed Oct 19 16:41:19 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@fairy ~]# TZ='Asia/Shanghai'; export TZ
[root@fairy ~]# rm /etc/localtime
rm:是否删除符号链接 "/etc/localtime"?y
[root@fairy ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime    //链接到上海时区文件 
[root@fairy ~]# 

vim /etc/profile

CST应该是指(China Shanghai Time,东八区时间)
UTC应该是指(Coordinated Universal Time,标准时间)
这2个时间实际上应该相差8个小时。

# 在/etc/profile文件中增加一行 
export TZ='CST-8'
# 使文件立即生效
source /etc/profile 或者 . /etc/profile

四、硬件时间修改方法

hwclock --show 查看硬件的时间
hwclock --set --date '2020-05-20 10:10:00' 设置硬件时间
hwclock --hctosys 设置系统时间和硬件时间同步
hwclock -s  让系统时间和硬件时间保持一致

 

 

 

 

 

 

 

 

 

 

标签:TZ,CentOS,etc,hwclock,修改,时间,localtime
From: https://www.cnblogs.com/elfin/p/16808356.html

相关文章