分享交流
合作共赢!

Linux修改时间方法总结

一、快速更改方法

1.查询时间

用date命令查看系统时间,colck查看硬件时间,显示时区:/etc/localtime,显示日历:cal -y

2.用date命令修改系统时间

格式:date  月日时分年.秒,date [MMDDhhmm[[CC]YY[.ss]]——MM:月份,DD:几号,hh:小时,mm:分钟,YY:两位年份,CCYY:四位年份,.ss:秒钟

如:更改日期为2019年一月10日  12点30分30秒

[root@centos7 ~]#date 011012302018.30

3、将系统时间同步到硬件时间

[root@centos7 ~]#clock -w或clock –hctosys   (以硬件时间为准,校正系统时间)

4、将硬件时间同步到系统时间

[root@centos7 ~]#clock -s或clock –systohc  (以系统时间为准,校正硬件时间)

附:

将当前时间到1970年1月1日之间的秒数,转化为天

[root@centos7 ~]#date +%s   (当前时间到1970年1月1日之间的秒数)

[root@centos7 ~]#date -d @156108732 或  date -d @156108732 “+%Y-%m-%d %H:%M:%S”

二、Linux更改时间/时区详解

说明:
如果要修改时区并且马上生效,可以通过替换/etc/localtime文件来实现。
首先通过tzselect命令选择时区。注意tzselect只是帮我们把选择的时区显示出来,并不会实际生效,也就是说它仅仅告诉我们怎么样去设置环境变量TZ。如我想用使用太平洋时区,他会告诉我们使用America/Los_Angeles
然后就可以通过cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime来修改服务器时区。

实现:
1. 查看当前时区

# date -R
Fri, 09 Jan 2015 13:56:05 +0700

2. 通过tzselect获取时间文件,如这里我要使用太平洋时区

# 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.
#? 2   #因为太平洋时区,是美国那边的,因为选择2 Americas,美洲
Please select a country.
 1) Anguilla			   28) Haiti
 2) Antigua & Barbuda		   29) Honduras
 3) Argentina			   30) Jamaica
 4) Aruba			   31) Martinique
 5) Bahamas			   32) Mexico
 6) Barbados			   33) Montserrat
 7) Belize			   34) Nicaragua
 8) Bolivia			   35) Panama
 9) Bonaire Sint Eustatius & Saba  36) Paraguay
10) Brazil			   37) Peru
11) Canada			   38) Puerto Rico
12) Cayman Islands		   39) Sint Maarten
13) Chile			   40) St Barthelemy
14) Colombia			   41) St Kitts & Nevis
15) Costa Rica			   42) St Lucia
16) Cuba			   43) St Martin (French part)
17) Curacao			   44) St Pierre & Miquelon
18) Dominica			   45) St Vincent
19) Dominican Republic		   46) Suriname
20) Ecuador			   47) Trinidad & Tobago
21) El Salvador			   48) Turks & Caicos Is
22) French Guiana		   49) United States
23) Greenland			   50) Uruguay
24) Grenada			   51) Venezuela
25) Guadeloupe			   52) Virgin Islands (UK)
26) Guatemala			   53) Virgin Islands (US)
27) Guyana
#? 49   #选择49 United States,美国
Please select one of the following time zone regions.
 1) Eastern Time
 2) Eastern Time - Michigan - most locations
 3) Eastern Time - Kentucky - Louisville area
 4) Eastern Time - Kentucky - Wayne County
 5) Eastern Time - Indiana - most locations
 6) Eastern Time - Indiana - Daviess, Dubois, Knox & Martin Counties
 7) Eastern Time - Indiana - Pulaski County
 8) Eastern Time - Indiana - Crawford County
 9) Eastern Time - Indiana - Pike County
10) Eastern Time - Indiana - Switzerland County
11) Central Time
12) Central Time - Indiana - Perry County
13) Central Time - Indiana - Starke County
14) Central Time - Michigan - Dickinson, Gogebic, Iron & Menominee Counties
15) Central Time - North Dakota - Oliver County
16) Central Time - North Dakota - Morton County (except Mandan area)
17) Central Time - North Dakota - Mercer County
18) Mountain Time
19) Mountain Time - south Idaho & east Oregon
20) Mountain Time - Navajo
21) Mountain Standard Time - Arizona
22) Pacific Time
23) Alaska Time
24) Alaska Time - Alaska panhandle
25) Alaska Time - southeast Alaska panhandle
26) Alaska Time - Alaska panhandle neck
27) Alaska Time - west Alaska
28) Aleutian Islands
29) Metlakatla Time - Annette Island
30) Hawaii
#? 22   #选择22 Pacific Time,太平洋时区

The following information has been given:

	United States
	Pacific Time

Therefore TZ='America/Los_Angeles' will be used.
Local time is now:	Mon Sep  7 02:07:21 PDT 2015.
Universal Time is now:	Mon Sep  7 09:07:21 UTC 2015.
Is the above information OK?
1) Yes
2) No
#? 1  #这边要输入,表示正确,然后就可以看到下面这个,使用America/Los_Angeles

You can make this change permanent for yourself by appending the line
	TZ='America/Los_Angeles'; 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:
America/Los_Angeles

3. 修改系统时区

# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime  //如果是上海时区的话,就是Asia/Shanghai,下同
# vim /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE="America/Los_Angeles"
# hwclock   //用当前系统时间更新硬件时间,更具体可以查看下面
# date -R   //这时再通过date -R就可以看到时区已经更改成东8区了(+8)
Mon, 07 Sep 2015 02:12:43 -0700
# date   //date可以看到显示data
Mon Sep  7 02:12:52 PDT 2015

对于Centos7以上系统可以用一下命令列出时区信息:

[root@centos7 ~]#timedatectl list-timezones

设置时区为上海:

[root@centos7 ~]#timedatectl set-timezone Asia/Shanghai

附录:
1、查看系统时间

[root@test3 ~]# date
Thu Dec 20 23:25:33 CST 2012
[root@test3 ~]# date +”%Y%m%d %H:%m:%S” //以指定格式显示时间
20121220 23:12:54

2、查看硬件时间

系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。当Linux启动时,系统时钟会去读取硬件时钟的设置,然后系统时钟就会独立于硬件运作。

[root@test3 ~]# hwclock //或者clock也一样,ll /sbin/clock可以看到clock是hwclock的软链接
Thu 20 Dec 2012 02:57:09 PM CST -0.176702 seconds

3、修改系统时间

[root@test3 ~]# date
Thu Dec 20 23:27:43 CST 2012
[root@test3 ~]# date -s “20121220 15:28:00”
Thu Dec 20 15:28:00 CST 2012
[root@test3 ~]# date
Thu Dec 20 15:28:03 CST 2012

4、修改硬件时间

[root@test3 ~]# hwclock
Thu 20 Dec 2012 03:30:58 PM CST -0.119534 seconds
[root@test3 ~]# hwclock –set –date=”20121220 23:31:00″
[root@test3 ~]# hwclock
Thu 20 Dec 2012 11:31:08 PM CST -0.451844 seconds

5、系统时间与硬件时间之间的同步

A、系统时间同步到硬件时间:

hwclock -w OR hwclock — systohc

B、硬件时间同步到系统时间:

hwclock -s OR hwclock — hctosys
[root@test3 ~]# date
Thu Dec 20 15:39:08 CST 2012
[root@test3 ~]# hwclock
Thu 20 Dec 2012 11:36:25 PM CST -0.661838 seconds
[root@test3 ~]# hwclock -w
[root@test3 ~]# hwclock
Thu 20 Dec 2012 03:39:25 PM CST -0.090384 seconds

6、查看当前时区

[root@test3 ~]# date -R
Thu, 20 Dec 2012 15:41:24 +0800
[root@test3 ~]# cat /etc/sysconfig/clock |grep -i zone
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE=”Asia/Shanghai”

7、修改当前时区

方法一、[root@test3 ~]# timeconfig //界面化修改,仅适用于RHEL和CentOS,这个命令CentOS 6.5没找到- –

方法二、[root@test3 ~]# dpkg-reconfigure tzdata” //适用于Debian,没试过。

方法三、修改/etc/localtime文件,该文件定义了Local Time Zone

[root@test3 ~]# date -R
Thu, 20 Dec 2012 15:46:51 +0800
[root@test3 ~]# mv /etc/localtime /etc/localtime.bak
[root@test3 ~]# cp /usr/share/zoneinfo/America/New_York /etc/localtime
[root@test3 ~]# date -R
Thu, 20 Dec 2012 02:47:40 -0500
[root@test3 ~]# cat /etc/sysconfig/clock |grep -i zone
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE=”Asia/Shanghai”
修改成功,但/etc/sysconfig/clock文件中的timezone未变,所以我们要修改/etc/sysconfig/clock文件
[root@test3 ~]# vim /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE=”America/New_York”

方法四、设置TZ环境变量,通过先通过tzselect获取选择时区的写法,然后再按如下设置。tzselect命令只是告诉你选择时区的写法,并不会修改系统时区。如上海时区是Asia/Shanghai这样写,越南时区是Asia/Ho_Chi_Minh是这样的。只是指出时区的写法。要更新时区,还需要 TZ=’Pacific/Honolulu’; export TZ

[root@test3 ~]# date -R
Thu, 20 Dec 2012 02:53:15 -0500
[root@test3 ~]# echo “export TZ=’Asia/Shanghai'” >> /etc/profile
[root@test3 ~]# cat /etc/profile |grep TZ
export TZ=’Asia/Shanghai’
[root@test3 ~]# source /etc/profile
[root@test3 ~]# date -R
Thu, 20 Dec 2012 15:54:47 +0800
$TZ环境变量的优先级要比/etc/localtime要高,并且在设置该全局环境变量后,即使使用timeconfig来修改也一样无法改变时区。

方法五、suse时间的修改与redhat linux相同,具体方法参照以上操作;
suse时区的交互式界面修改方法yast—system—Date and Time找到合适的时区,确定进行修改。

8、查看指定时区当前时间

查看-5区当前时间

[root@test3 ~]# zdump GMT-5
GMT-5 Thu Dec 20 12:59:13 2012 GMT

9、附相关缩写含义

GMT
GMT 是 Greenwich Mean Time 的缩写,译为中文为“格林威治标准时间”或“格林尼治标准时间”,直译的话,可译为“格林威治平时”或“格林尼治平时”。这里的格林威治位于英国伦敦东南方向的泰晤士河畔,美国也有个格林威治镇,不过与时间没关系。

UTC
UTC 是 Coordinated Universal Time 的缩写,译为中文为“世界标准时间”,直译的话,可译为“协调通用时间”或“协调世界时间”。目前来说也就是指 GMT 时间。为什么说目前就是指 GMT 时间呢?因为本初子午线(子午线即经线,本初子午线即 0 度经线)其实穿过的是沙特阿拉伯西边的麦加,而不是英国的格林威治。当时英国皇家学会暂时确定格林威治为本初子午线的穿过点﹐加之英国正是兴旺发达时期,全世界就将错就错,用到现在。说不定哪天改为麦加时间为标准时间也不是没有可能。所以我们一般使用 UTC,而不是 GMT。

PST
PST是太平洋标准时间(西八区),与北京时间(东八区)时差-16个小时,也就是北京时间减去16就是PST时间。而PDT比PST早1个小时,就是说PDT与北京时间时差为-15小时
更具体的,可以看这篇:GMT UTC PDT说明

赞(0) 打赏
未经允许不得转载:琼杰笔记 » Linux修改时间方法总结

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册