一、Linux时间介绍
Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置,当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统设置就会独立与硬件运作。
Linu中的所有命令(包括函数)都是采用的系统时钟设置,在Linux中,用于时钟查看和设置的命令主要有date、hwclock和clock,其中,clock和hwclock用法接近,只用一个 就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
二、Linux时间设置命令
1、date
语法格式:date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help][--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
==说明:可以用来设置系统日期与实践,只有管理员才有设置日期与时间的权限,一般用户只能用date命令显示时间,若不加任何参数,data会显示目前的日期与时间。
例1:显示当前系统时间:
例2:设置日期和时间为2022年9月12日12:00
例3:显示时区
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:查看硬件时间
例2:设置硬件时间
例3:硬件时间和系统时间同步
hwclock --hctosys
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 查看上面的设定。