摘要:本文主要向大家介绍了Linux运维知识之Linux时间设置,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
本文主要向大家介绍了Linux运维知识之Linux时间设置,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
场景:Linux时间设置对于系统日志的查看,文件操作很有必要,有时候不可避免的调整系统时间。
1 手动设置
1.1 时区查看
使用date命令查看时区
[root@db-server ~]# date -R
Sun, 11 Jan 2015 07:10:28 -0800
[root@db-server ~]#
上面命令输出了-0800表示西八区,是美国旧金山所在的时区,下面表示我们国家的东八区(+0800)
lfcp10:~ # date -R
Wed, 28 Dec 2016 10:38:51 +0800
查看clock系统配置文件
[root@db-server ~]# more /etc/sysconfig/clock
ZONE="America/Los_Angeles"
UTC=true
ARC=false
如上所示,表示该系统设置的时区为“America/Los_Angeles”,也就是西八区。
1.2 时区设置
方法1:使用tzselect设置时区
例子,将系统时区设置为东八区(北京时间)
[root@db-server ~]# 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) east China - Beijing, Guangdong, Shanghai, etc.
2) Heilongjiang (except Mohe), Jilin
3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.
4) most of Tibet & Xinjiang
5) west Tibet & Xinjiang
#? 1
The following information has been given:
China
east China - Beijing, Guangdong, Shanghai, etc.
Therefore TZ=‘Asia/Shanghai‘ will be used.
Local time is now: Sun Jan 11 23:31:51 CST 2015.
Universal Time is now: Sun Jan 11 15:31:51 UTC 2015.
Is the above information OK?
1) Yes
2) No
#? yes
Please enter 1 for Yes, or 2 for 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
注意:tzselect命令只告诉你选择的时区的写法,并不会生效。所以现在它还不是东8区北京时间。你可以在.profile、.bash_profile或者/etc/profile中设置正确的TZ环境变量并导出。
例如在.bash_profile里面设置 TZ=‘Asia/Shanghai‘; export TZ并使其生效。
需要将上述的时区设置写入配置文件中,
[root@db-server ~]# source .bash_profile
[root@db-server ~]# date
Sun Jan 11 23:37:40 CST 2015
上述方法只是一个过程,我还是倾向使用第二种方法:
方法2:复制相应的时区文件,替换系统时区文件;或者创建链接文件——即时生效
首先查看相应的时区
lfcp10:~ # cd /usr/share/zoneinfo
lfcp10:/usr/share/zoneinfo # ls
Africa Atlantic Canada EST5EDT Factory GMT-0 Iceland Japan MST7MDT PRC ROC US Zulu zone.tab
America Australia Chile Egypt GB GMT0 Indian Kwajalein Mexico PST8PDT ROK UTC iso3166.tab zone1970.tab
Antarctica Brazil Cuba Eire GB-Eire Greenwich Iran Libya NZ Pacific Singapore Universal posix
Arctic CET EET Etc GMT HST Israel MET NZ-CHAT Poland Turkey W-SU posixrules
Asia CST6CDT EST Europe GMT+0 Hongkong Jamaica MST Navajo Portugal UCT WET right
lfcp10:/usr/share/zoneinfo # cd Asia
lfcp10:/usr/share/zoneinfo/Asia # ls
Aden Baghdad Calcutta Dhaka Hong_Kong Kamchatka Kuala_Lumpur Muscat Pyongyang Seoul Tel_Aviv Ust-Nera
Almaty Bahrain Chita Dili Hovd Karachi Kuching Nicosia Qatar Shanghai Thimbu Vientiane
Amman Baku Choibalsan Dubai Irkutsk Kashgar Kuwait Novokuznetsk Qyzylorda Singapore Thimphu Vladivostok
Anadyr Bangkok Chongqing Dushanbe Istanbul Kathmandu Macao Novosibirsk Rangoon Srednekolymsk Tokyo Yakutsk
Aqtau Beijing Chungking Gaza Jakarta Katmandu Macau Omsk Riyadh Taipei Ujung_Pandang Yekaterinburg
Aqtobe Beirut Colombo Harbin Jayapura Khandyga Magadan Oral Saigon Tashkent Ulaanbaatar Yerevan
Ashgabat Bishkek Dacca Hebron Jerusalem Kolkata Makassar Phnom_Penh Sakhalin Tbilisi Ulan_Bator
Ashkhabad Brunei Damascus Ho_Chi_Minh Kabul Krasnoyarsk Manila Pontianak Samarkand Tehran Urumqi
lfcp10:/usr/share/zoneinfo/Asia #
复制相应的时区文件,替换系统时区文件;或者创建链接文件
cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime
在中国可以使用:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock
方法3:修改/etc/sysconfig/clock文件 ——重启生效 (没试过)
修改/etc/sysconfig/clock文件,把ZONE的值改为Asia/Shanghai,UTC值改为false,改完后的文件如下:
# 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
1.3 手动设置时间
date命令:
lfcp10:~ # date
Wed Dec 28 10:26:07 CST 2016
设置时间:
lfcp10:~ # date -s "2016-01-01 10:10:10"
Fri Jan 1 10:10:10 CST 2016
lfcp10:~ # date -s "2016-12-28"
Wed Dec 28 00:00:00 CST 2016
lfcp10:~ # date -s "10:34:20"
Wed Dec 28 10:34:20 CST 2016
lfcp10:~ # date
Wed Dec 28 10:34:24 CST 2016
lfcp10:~ #
将时间写入bios避免重启失效:
hwclock -w
2 同步网络时间
在Linux下,我们可以使用ntpdate进行网络时间的同步,而不是我们自己去设置时间。这个命令的使用很简单,
ntpdate 0.cn.pool.ntp.org
另外网络时间同步和时区是不一样的。你可以选择任何一台网络时间同步服务器来同步你的时间,只要你的时区设置是对的,那么你的Linux上的时间就是对的。因为全球都是基于一个标准时间来约定的,美国人民与我们不同的是时区不同,经过换算,我们和他们的标准时间是一样的。
2.1 定时的同步时间
我们可以使用crontab来定时的同步时间
vim /etc/crontab
10 5 * * * root (/usr/sbin/ntpdate 0.cn.pool.ntp.org && /sbin/hwclock -w) &> /var/log/ntpdate.log
每天的5点10分crontab运行一次命令,自动同步时间。
如果你具有多台的服务器,不要使用这个方法来定时的同步的你服务器,请使用ntpd来进行,这个还可以校准始终的问题。详情查看鸟哥。
使用ntpdata造成的时间的越变还可能引发因某些依赖连续时间的程序的问题。一般第一次使用ntpdate,接下来使用ntpd服务来不断的调整时间。参见//blog.sina.com.cn/s/blog_3f3422fd0100f06c.html。
全球那么多的网络时间同步器,那么使用哪个好呢。我们知道数据在网络上流动是由延迟的,因此我们选择离我们服务器越近的服务器进行同步,时间越准。
时间服务器分为两种,一种是一级时间服务器,另外一种是二级时间服务器。我们如果是同步自己的服务器的时间,那么选择二级时间服务器,因为一级时间服务器是为二级时间服务器提供时间校对服务器,我们尽量不要增加一级服务器的压力。这种层级的概念和DNS的层级概念是一致的。
一级时间服务器列表://support.ntp.org/bin/view/Servers/StratumOneTimeServers
二级时间服务器列表://support.ntp.org/bin/view/Servers/StratumTwoTimeServers
附二级服务器列表:
0.pool.ntp.org 有域名负载均衡
0.cn.pool.ntp.org 有域名负载均衡
ntp.tuna.tsinghua.edu.cn 清华大学
time.windows.com 微软
ntpdate命令:
网络时间同步命令
ntpdate -u 210.72.145.44
注意:若不加上-u参数, 会出现以下提示:no server suitable for synchronization found -u:从man ntpdate中可以看出-u参数可以越过防火墙与主机同步; 210.72.145.44:中国国家授时中心的官方服务器。
ntp常用服务器: 中国国家授时中心:210.72.145.44 NTP服务器(上海) :ntp.api.bz 美国:time.nist.gov 复旦:ntp.fudan.edu.cn 微软公司授时主机(美国) :time.windows.com 台警大授时中心(台湾):asia.pool.ntp.org
经测试中国国家授时中心与NTP上海服务器可以正常同步时间,注意需要加上-u参数!
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号