zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Centos5, 6, 以及Ubuntu18.04下更改系统时间和时区

2023-03-07 09:42:49 时间

http://www.namhuy.net/2435/how-to-change-date-time-timezone-on-centos-6.html

查看日期(使用 -R 参数会以数字显示时区)

$date

选择时区, 用当地的时区文件覆盖默认的, 这个在Ubuntu18.04下同样有效

cd /usr/share/zoneinfo/Asia/
sudo cp Shanghai /etc/localtime

修改时间

sudo date -s "20150514 18:07:00"

硬件时间操作

#查看硬件时间
sudo /sbin/clock --show
#将系统时间保存至硬件
sudo /sbin/clock --systohc
#使用硬件时间, 将硬件时间更新到系统
sudo /sbin/clock --hctosys

安装ntp, 之后同步系统时间

sudo yum install ntp
ntpdate pool.ntp.org

如果出现  sudo: timestamp too far in the future 错误, 使用 sudo -k 清理下当前会话时间

配置NTP时间同步

Centos6

检查ntpd服务是否已经运行 service ntpd status

检查ntpd服务配置文件 /etc/ntp.conf, 修改其中的 Use public servers 这部分, 将其修改为自己的ntp服务器, 或公用的ntp服务器

#restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 192.168.16.1 prefer
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org

查看ntp服务器列表, 如果上面的配置正确, 可以通过ntpq -p命令查看ntp服务器状态

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 WNDR3800.lan    192.36.143.153   3 u   12   64    1    0.488  2175.86   0.000
 ntp1.flashdance .INIT.          16 u    -   64    0    0.000    0.000   0.000
 ntp8.flashdance 192.36.143.150   2 u    9   64    1  306.020  2159.29   0.000
 tick.ntp.infoma .INIT.          16 u    -   64    0    0.000    0.000   0.000
 ntp6.flashdance 192.36.143.150   2 u    7   64    1  289.320  2167.42   0.000