NTP系统时间同步-操作记录

 

在初始化一台linux服务器后,发现这台服务器的时间不对
[root@dev ~]# date
2016年 10月 11日 星期二 07:04:34 CST

Linux时钟分为系统时钟 (System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。
Linux中的所有命令(包括函数)都是采用的系统时钟设置。在Linux中,用于时钟查看和设置的命令主要有date、hwclock和 clock。其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

------------------------------------------------------------------------------------------------------
linux系统时区由EDT改为CST

EDT:指美国东部夏令时间,波士顿、纽约市、华盛顿哥伦比亚特区,都在这个时区内,跟北京时间有12小时的时差,晚12小时。
CST:可以指下面两种:
1)美国中部标准时间(西六区,-6:00),中国是东八区(+8:00),北京时间比美国中部标准时间早14个小时。3:45 PM CST 是北京时间凌晨1:45。
2)中澳大利亚标准时间(+10:30),中国是东八区(+8:00),北京时间比中澳大利亚标准时间晚2个半小时。3:45 PM CST 是北京时间下午上午5:45。
[root@db1 ~]# date
Wed Oct 25 04:23:19 EDT 2017

修改操作:
[root@db1 ~]# mv /etc/localtime /etc/localtime.bak
[root@db1 ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@db1 ~]# date
Wed Oct 25 16:25:39 CST 2017
------------------------------------------------------------------------------------------------------

同步系统时间
(1)安装ntpdate
[root@dev ~]# yum install ntpdate

(2)接着进行在线同步,选择上海交大的NTP服务器进行同步;确保网络通畅,DNS正常解析;或者使用ntpdate cn.pool.ntp.org
[root@dev ~]# ntpdate ntp.sjtu.edu.cn
10 Oct 23:02:23 ntpdate[21945]: step time server 202.120.2.100 offset -28975.130708 sec

再次查看时间,发现时间是正确的了
[root@dev ~]# date   //正常显示时区应该是CST(即中国标准时间);EST是美国东部标准时间;UTC是协调世界时间/世界标准时间;GMT是格林尼治标准时间
2016年 10月 10日 星期一 23:02:35 CST
[root@dev ~]# date --set "10/10/16 23:45:52"          //--set参数等于-s.  (月/日/年 时:分:秒)
2016年 10月 10日 星期一 23:45:52 CST

(3)结合crontab制定定时同步系统时间(比如每一小时执行一次同步)
[root@dev ~]# crontab -e
10 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1

设置时区
方式1:删除本地时间,并设置时区为上海
[root@dev ~]# rm -rf /etc/localtime
[root@dev ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改/etc/sysconfig/clock文件,修改为:
[root@dev ~]# cat /etc/sysconfig/clock
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"
UTC=false              //这一行可加可不加
ARC=false             //这一行可加可不加

方式2:通过tzselect调整
[root@dev ~]# 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 Dec 18 21:34:19 CST 2016.
Universal Time is now:  Sun Dec 18 13:34:19 UTC 2016.
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

设置硬件时间
(1)查看硬件时间
[root@dev ~]# hwclock --show
Sun 18 Dec 2016 09:38:44 PM CST  -0.902813 seconds
[root@dev ~]# clock --show
Sun 18 Dec 2016 09:38:51 PM CST  -0.236699 seconds
(2)修改硬件时间 
[root@dev ~]# hwclock –set –date=”07/07/06 10:19″              (月/日/年 时:分:秒)
[root@dev ~]# clock –set –date=”07/07/06 10:19″                  (月/日/年 时:分:秒)

(3)硬件时间和系统时间的同步
按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步;
但是在不重新启动的时候,需要用hwclock或clock命令实现同步:
硬件时钟与系统时钟同步
[root@dev ~]# hwclock --hctosys           //hc代表硬件时间,sys代表系统时间
或者
[root@dev ~]# clock --hctosys
系统时钟和硬件时钟同步
[root@dev ~]# hwclock --systohc
或者
[root@dev ~]# clock --systohc

同步BIOS时钟,强制把系统时间写入CMOS,命令如下:
[root@dev ~]# clock -w

posted @ 2016-10-10 23:10  散尽浮华  阅读(4561)  评论(0编辑  收藏  举报