linux 时区 时间改更设置

转自:http://www.linux99.com

linux命令行下更改时区
tzselect

更改时间
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
date 月日时分年.秒

更新BOIS时间hwclock

hwclock - query and set the hardware clock (RTC)

Usage: hwclock [function] [options...]

Functions:
--help        show this help
--show        read hardware clock and print result
--set         set the rtc to the time given with --date
--hctosys     set the system time from the hardware clock
--systohc     set the hardware clock to the current system time
--adjust      adjust the rtc to account for systematic drift since
the clock was last set or adjusted
--getepoch    print out the kernel's hardware clock epoch value
--setepoch    set the kernel's hardware clock epoch value to the
value given with --epoch
--version     print out the version of hwclock to stdout

Options:
--utc         the hardware clock is kept in coordinated universal time
--localtime   the hardware clock is kept in local time
--directisa   access the ISA bus directly instead of /dev/rtc
--badyear     ignore rtc's year because the bios is broken
--date        specifies the time to which to set the hardware clock
--epoch=year  specifies the year which is the beginning of the
hardware clock's epoch value
--noadjfile   do not access /etc/adjtime. Requires the use of
either --utc or --localtime


Network Time Protocol (NTP) 也是RHCE新增的考试要求. 学习的时候也顺便复习了一下如何设置Linux的时间,现在拿出来和大家分享
设置NTP服务器不难但是NTP本身是一个很复杂的协议. 这里只是简要地介绍一下实践方法
和上次一样,下面的实验都在RHEL5上运行

1. 时间和时区

如果有人问你说现在几点? 你看了看表回答他说晚上8点了. 这样回答看上去没有什么问题,但是如果问你的这个人在欧洲的话那么你的回答就会让他很疑惑,因为他那里还太阳当空呢.

这里就有产生了一个如何定义时间的问题. 因为在地球环绕太阳旋转的24个小时中,世界各地日出日落的时间是不一样的.所以我们才有划分时区(timezone) 的必要,也就是把全球划分成24个不同的时区. 所以我们可以把时间的定义理解为一个时间的值加上所在地的时区(注意这个所在地可以精确到城市)

地理课上我们都学过格林威治时间(GMT), 它也就是0时区时间. 但是我们在计算机中经常看到的是UTC. 它是Coordinated Universal Time的简写. 虽然可以认为UTC和GMT的值相等(误差相当之小),但是UTC已经被认定为是国际标准,所以我们都应该遵守标准只使用UTC

那么假如现在中国当地的时间是晚上8点的话,我们可以有下面两种表示方式

20:00 CST
12:00 UTC

这里的CST是Chinese Standard Time,也就是我们通常所说的北京时间了. 因为中国处在UTC+8时区,依次类推那么也就是12:00 UTC了.

为什么要说这些呢(呵呵这里不是地理论坛吧...)?

第一,不管通过任何渠道我们想要同步系统的时间,通常提供方只会给出UTC+0的时间值而不会提供时区(因为它不知道你在哪里).所以当我们设置系统时间的时候,设置好时区是首先要做的工作
第二,很多国家都有夏令时(我记得小时候中国也实行过一次),那就是在一年当中的某一天时钟拨快一小时(比如从UTC+8一下变成UTC+9了),那么同理到时候还要再拨慢回来.如果我们设置了正确的时区,当需要改变时间的时候系统就会自动替我们调整

现在我们就来看一下如何在Linux下设置时区,也就是time zone


2. 如何设置Linux Time Zone

在Linux下glibc提供了我们事先编译好的许多timezone文件, 他们就放在/usr/share/zoneinfo这个目录下,这里基本涵盖了大部分的国家和城市


代码:
# ls -F /usr/share/zoneinfo/
Africa/      Chile/   Factory    Iceland      Mexico/   posix/      Universal
America/     CST6CDT  GB         Indian/      Mideast/  posixrules  US/
Antarctica/  Cuba     GB-Eire    Iran         MST       PRC         UTC
Arctic/      EET      GMT        iso3166.tab  MST7MDT   PST8PDT     WET
Asia/        Egypt    GMT0       Israel       Navajo    right/      W-SU
Atlantic/    Eire     GMT-0      Jamaica      NZ        ROC         zone.tab
Australia/   EST      GMT+0      Japan        NZ-CHAT   ROK         Zulu
Brazil/      EST5EDT  Greenwich  Kwajalein    Pacific/  Singapore
Canada/      Etc/     Hongkong   Libya        Poland    Turkey
CET          Europe/  HST        MET          Portugal  UCT
在这里面我们就可以找到自己所在城市的time zone文件. 那么如果我们想查看对于每个time zone当前的时间我们可以用zdump命令


代码:
# zdump Hongkong
Hongkong  Fri Jul  6 06:13:57 2007 HKT
那么我们又怎么来告诉系统我们所在time zone是哪个呢? 方法有很多,这里举出两种

第一个就是修改/etc/localtime这个文件,这个文件定义了我么所在的local time zone.
我们可以在/usr/share/zoneinfo下找到我们的time zone文件然后拷贝去到/etc/localtimezone(或者做个symbolic link)

假设我们现在的time zone是BST(也就是英国的夏令时间,UTC+1)


代码:
# date
Thu Jul  5 23:33:40 BST 2007
我们想把time zone换成上海所在的时区就可以这么做


代码:
# ln -sf /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime
# date
Fri Jul  6 06:35:52 CST 2007
这样时区就改过来了(注意时间也做了相应的调整)

第二种方法也就设置TZ环境变量的值. 许多程序和命令都会用到这个变量的值. TZ的值可以有多种格式,最简单的设置方法就是使用tzselect命令


代码:
# tzselect
...
TZ='America/Los_Angeles';export TZ
tzselect会让你选择所在的国家和城市(我省略了这些步骤),最后输出相应的TZ变量的值.那么如果你设置了TZ的值之后时区就又会发生变化

你可能感兴趣的:(linux,linux命令,职场,休闲)