CentOS 6 更改系统时区方法
为方便平时查看带时间戳日志/事件,系统常规设置项之一是修改默认时区。之前介绍过 CentOS 7+ 设置方法,本篇是 CentOS6。
查看当前时区
查询当前系统使用的时区。下面示例查询结果为北美东部夏令时间(EDT,UTC−4 时区)。
[test@localhost ~]$ date Thu Mar 21 22:30:53 EDT 2019
设置系统时区
用下面命令设置系统时区,红色部分是时区参数(完整时区名称列表见此介绍)。
# 设置 上海 UTC+8 时区 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 设置 香港 UTC+8 时区 ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime # 设置 台北 UTC+8 时区 ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime # 设置 北美东部标准 UTC-5 时区 ln -sf /usr/share/zoneinfo/EST /etc/localtime # 设置 协调世界时 UTC 时区 ln -sf /usr/share/zoneinfo/UTC /etc/localtime
设置硬件时区
如需修改硬件设备时区,编辑下面文件。
vi /etc/sysconfig/clock
修改其中的 ZONE 参数值,并额外添加两行,将 UTC(BIOS 时间) 和 ARC 参数设置为 false。
ZONE="Asia/Shanghai"
UTC=false
ARC=false
运行更新硬件时间。可用 hwclock
命令查询验证(date
查询的是系统时间)。
hwclock --systohc --localtime
定期同步时间
如果需要定期调准时间,可以设置 NTP 时间同步服务。
# 安装 NTP 软件包 yum -y install ntp # 添加可信任 NTP 时间同步服务器 ntpdate -d asia.pool.ntp.org # 重启刷新 NTP 服务 service ntpd restart
如果启用了系统防火墙,别忘了放行 NTP 使用的 123/UDP 端口。如果不清楚如何设置防火墙,可参考之前教程。