linux下的时间问题

来源:互联网 发布:淘宝节点处罚 编辑:程序博客网 时间:2024/05/17 19:18
1、date:
语法格式:date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
说明:可用来设置系统日期与时间。只有管理员才有设置日期与时间的权限,一般用户只能用date 命令显示时间。若不加任何参数,data 会显示目前的日期与时间。
例1:显示当前系统时间
[root@Test2 ~]# date
2010 年06 月17 日星期四00:00:04 CST
例2:设置日期和时间为2010 年6 月18 号12:00
[root@Test2 ~]# date -s "20100618 12:00:00"
2010 年06 月18 日星期五12:00:00 CST
例3:设置日期为2010年年6 月18 号
[root@Test2 ~]# date -s 20100618
2010 年06 月18 日星期五00:00:00 CST
例4:设置时间为12:00:00
[root@Test2 ~]# date 12:00:00
date: invalid date “12:00:00”
例5:显示时区
[root@Test2 ~]# date -R
Thu, 17 Jun 2010 00:01:36 +0800
或者:
[root@Test2 ~]# cat /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="Asia/Shanghai"
UTC=true
ARC=false

2、hwclock/clock
语法格式:hwclock [--adjust][--debug][--directisa][--hctosys][--show][--systohc][--test] [--utc][--version][--set --date=<日期与时间>]
参数:
--adjust hwclock 每次更改硬件时钟时,都会记录在/etc/adjtime 文件中。使用--adjust 参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟。
--debug 显示hwclock 执行时详细的信息。
--directisa hwclock 预设从/dev/rtc 设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O 指令来存取硬件时钟。
--hctosys 将系统时钟调整为与目前的硬件时钟一致。
--set --date=<日期与时间> 设定硬件时钟。
--show 显示硬件时钟的时间与日期。
--systohc 将硬件时钟调整为与目前的系统时钟一致。
--test 仅测试程序,而不会实际更改硬件时钟。
--utc 若要使用格林威治时间,请加入此参数,hwclock 会执行转换的工作。
--version 显示版本信息。
例1:查看硬件时间
# hwclock --show
或者
# clock --show
例2:设置硬件时间
# hwclock --set --date="07/07/06 10:19" (月/日/年时:分:秒)
或者
# clock --set --date="07/07/06 10:19" (月/日/年时:分:秒)
例3:硬件时间和系统时间的同步
按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步,但是在不重新启动的时候,需要用hwclock 或clock 命令实现同步。
硬件时钟与系统时钟同步:
# hwclock --hctosys(hc 代表硬件时间,sys 代表系统时间)
或者
# clock –hctosys
例4:系统时钟和硬件时钟同步:
# hwclock --systohc
或者
# clock –systohc
例5:强制将系统时间写入CMOS,使之永久生效,避免系统重启后恢复成原时间
# clock –w
或者
# hwclock -w

3、时区的设置
# 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
3) central China - Gansu, Guizhou, Sichuan, Yunnan, etc.
4) Tibet & most of Xinjiang Uyghur
5) southwest Xinjiang Uyghur
#? 输入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: Fri Jul 7 10:32:18 CST 2006.
Universal Time is now: Fri Jul 7 02:32:18 UTC 2006.
Is the above information OK?
1) Yes
2) No
#? 输入1,确认
如果不用tzselect 命令,可以修改文件变更时区。
# vi /etc/sysconfig/clock
Z/Shanghai(查/usr/share/zoneinfo 下面的文件)
UTC=false
ARC=false
# rm /etc/localtime
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
重新启动即可。

4、图形界面设置时区命令timeconfig

5、时间同步
例1:同步时间
# ntpdate 210.72.145.44 (210.72.145.44 是中国国家授时中心的官方服务器)
例2:定时同步时间
# crontab –e 添加脚本例子如下:
*/20 * * * * /usr/sbin/ntpdate 210.72.145.44 //每20 分钟执行一次
30 5 * * * /usr/sbin/ntpdate 210.72.145.44 //每天早晨5 点半执行
※ 前面五个*号代表五个数字,数字的取值范围和含义如下:分钟(0-59) 小時(0-23) 日期(1-31) 月份(1-12) 星期(0-6)//0 代表星期天设定完毕后,可使用# crontab –l 查看上面的设定。