Linux 时间及时区设置

简介: Linux 时间及时区设置

时间以及时区设置


1.首先确认使用utc还是local time.

UTC(Universal Time Coordinated)=GMT(Greenwich Mean Time)Local time本地时间,

传统的POSIX计算机(Solaris,bsd,unix)使用UTC格式,linux可以处理UTC时间和蹩脚的Windows所使用的local time

 

到底是使用UTC还是local time可以这样来确定:

如果机器上同时安装有LinuxWindows,建议使用Local time

如果机器上只安装有Linux,建议使用utc

 

2.确定后编辑/etc/sysconfig/clock

设置UTC=0UTC=FALSE->硬件时钟为local time; UTC=1UTC=TRUE->硬件时钟UTC(GMT),ARC=0ARC=FALSE,->如果是在Alpha机器上,则设置为TRUE,否则FALSE

注:把硬件时钟设置为UTC的好处就是他能自动设置夏时制,缺点是在安装有多重操作系统启动的机器上,在另外的操作系统上时钟会错误。

 

Linux启动时,其中的一个脚本(/etc/rc.d/rc.sysinit)会运行/sbin/hwclock程序,把当前的硬件时钟复制为系统时间。以后修改时间通过修改系统时间实现。为了保持系统时间与CMOS时间的一致性,Linux每隔一段时间会将系统时间写入CMOShwclock假定硬件时钟已经设置为本地时钟,除非带有utc参数。脚本文件会读取/etc/sysconfig/clock文件的设置,因此我们不需要修改脚本,直接修改这个配置文件就可以了

修改/etc/sysconfig/clock文件,添加如下内容:

ZONE="Asia/Shanghai"

UTC=false

ARC=false

 

3.使用cat /etc/sysconfig/clock查看当前时区

[root@localhost laiyu]# 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

 

4.使用tzselect设置时区

[root@localhost laiyu]# 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[Enter]

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[Enter]

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[Enter]

The following information has been given:

 

   China

   east China - Beijing, Guangdong, Shanghai, etc.

 

Therefore TZ='Asia/Shanghai' will be used.

Local time is now:    20121125日星期日22:40:32 CST.

Universal Time is now:    20121125日星期日14:40:32 UTC.

Is the above information OK?

1) Yes

2) No

#? 1[Enter]

 

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

 

5.复制相应的时区文件,替换系统默认时区

[root@localhost laiyu]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp: "/usr/share/zoneinfo/Asia/Shanghai""/etc/localtime"为同一文件

 

注:cp /usr/share/zoneinfo/主时区/次时区/etc/localtime

时钟设置文件为:/etc/localtime,实际上这个文件是/usr/share/zoneinfo目录下对应文件的符号连接

 

6.设置时间与internet同步

[root@localhost laiyu]# ntpdate asia.pool.ntp.org

25 Nov 22:33:50 ntpdate[3210]: adjust time server 220.130.158.71 offset -0.060016 sec

 

注:设置系统时间也可以用datedate -s命令

附:the NTP socket is in use, exiting的解决办法,如下

[root@localhost laiyu]# ntpdate asia.pool.ntp.org

25 Nov 22:28:18 ntpdate[3176]: the NTP socket is in use, exiting

[root@localhost laiyu]# service ntpd stop

Shutting down ntpd:                                        [  OK  ]

[root@localhost laiyu]# ntpdate asia.pool.ntp.org

25 Nov 22:33:50 ntpdate[3210]: adjust time server 220.130.158.71 offset -0.060016 sec

[root@localhost laiyu]# date

20121125日星期日22:33:58 CST

 

7.将当前时间写入BIOS永久生效(避免重启后失效),让系统时间与硬件时钟同步

[root@localhost laiyu]# hwclock

20121125日星期日224422  -0.032355 seconds

或者

[root@localhost laiyu]# clock -w

 

注:clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

 

8.查看Boiscmos)时间(用#hwclock -r

[root@localhost laiyu]# hwclock -r

20121125日星期日224946  -0.813294 seconds

或者

[root@localhost laiyu]# hwclock --show

或者

[root@localhost laiyu]# clock --show

目录
相关文章
|
4月前
|
监控 Oracle 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第8天】在 Linux 平台设置 Oracle 开机自启动有多种方法,本文以 CentOS 为例,介绍了两种常见方法:使用 `rc.local` 文件(较简单但不推荐用于生产环境)和使用 `systemd` 服务(推荐)。具体步骤包括编写启动脚本、赋予执行权限、配置 `rc.local` 或创建 `systemd` 服务单元文件,并设置开机自启动。通过 `systemd` 方式可以更好地与系统启动过程集成,更规范和可靠。
297 2
|
4月前
|
Oracle Ubuntu 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第7天】本文介绍了 Linux 系统中服务管理机制,并详细说明了如何在使用 systemd 和 System V 的系统上设置 Oracle 数据库的开机自启动。包括创建服务单元文件、编辑启动脚本、设置开机自启动和启动服务的具体步骤。最后建议重启系统验证设置是否成功。
|
4月前
|
关系型数据库 MySQL Linux
Linux系统如何设置自启动服务在MySQL数据库启动后执行?
【10月更文挑战第25天】Linux系统如何设置自启动服务在MySQL数据库启动后执行?
263 3
|
5月前
|
Ubuntu Linux
Linux实践|设置静态 IP 地址
Linux实践|设置静态 IP 地址
107 0
Linux实践|设置静态 IP 地址
|
5月前
|
Linux 应用服务中间件 nginx
Linux下权限设置之suid、sgid、sticky
Linux下权限设置之suid、sgid、sticky
|
6月前
|
NoSQL Linux Redis
Linux Redis 服务设置开机自启动
【9月更文挑战第2天】在 Linux 系统中,可使用两种方法设置 Redis 开机自启动:一是通过创建 `redis.service` 文件并利用 systemd 进行管理,包括定义服务参数和启动脚本;二是编辑 `/etc/rc.local` 文件,在其中添加启动命令。推荐使用 systemd 方法,因为它更符合现代 Linux 系统的设计理念。设置完成后,可通过 `sudo systemctl status redis.service` 检查服务状态。
818 4
|
6月前
|
Linux Shell
10-8|linux date设置时间
10-8|linux date设置时间
|
6月前
|
Unix Linux Python
Cron定时设置在linux和mac中的使用
文章详细说明了如何在Linux和Mac操作系统中使用Cron进行定时任务的设置,并提供了多个Cron表达式的实例。
66 0
|
7月前
|
Linux 网络安全
在Linux中,如何设置防火墙规则?
在Linux中,如何设置防火墙规则?
|
7月前
|
网络协议 Linux 网络架构
在Linux中,如何设置网络路由?
在Linux中,如何设置网络路由?