用 NTP 设置 CentOS 和 Ubuntu 服务器的正确时区

Linux系统
438
0
0
2022-10-18

文章来源:www.liberiangeek.net/2013/02/setti...

Setting The Correct Timezone In CentOS And Ubuntu Servers With NTP

First, let view all the timezones your sytem has by running the commands below. Remember to run these commands as root. In Ubuntu always precede the commands with wudo.

# ls /usr/share/zoneinfo

Take notes of the available timezones on your system. Next, you’ll want to replace the current timezone file in /etc/ directory with the correct one. To do that, remove the current timezone file by running the commands below.

# mv /etc/localtime /etc/localtime.bak

Next, run the commans below to link the local timezone file with the correct one in the /usr/share/zoneinfo directory.

# ln -s /usr/share/zoneinfo/PRC /etc/localtime

Next, install these packages if you haven’t already done so in Ubuntu or CentOS.

$ sudo apt-get install ntp ntpdate

Here’s the same commands in CentOS.

# yum install ntp ntpdate

Once ntp is installed, run the commands below to synchronize your server’s internal clock. There are multiple ntp servers to choose from so if the one below isn’t what you want, then choose a different server.

# ntpdate 0.us.pool.ntp.org

Finally, run the commands below to apply the changes to the hardware so it sticks after reboot.

# hwclock --systohc

Enjoy!

以下为自己偿试的译文(可能会有些不准确的地方,勿喷^_^)
NTP 设置 CentOSUbuntu 服务器的正确时区

首先,让我们运行以下命令,来查看系统所有时区,记住需要使用 root 。在 Ubuntu 里,我们常常需要在该命令前使用 sudo

# ls /usr/share/zoneinfo

记录下来在您系统里找到的时区。然后,你应该想要用正确的时区文件来替换在 /etc 目录下的当前时区文件,这样,您需要运行以下命令来删除当前时区文件:

# mv /etc/localtime /etc/localtime.bak

然后,运行以下命令,用 /usr/share/zoneinfo 目录里正常的时区文件链接到当前时区文件

(译者注:中国用PRC即可,PRC指向的其实是 Asia/Shanghai )

# ln -s /usr/share/zoneinfo/PRC /etc/localtime

然后,如果 Ubuntu 里没有这些包,则需要安装

$ sudo apt-get install ntp ntpdate

CentOS 里可以用这个命令

# yum install ntp ntpdate

一旦 ntp 安装了,命令以下命令来同步您服务器上的内部时钟。有很多 ntp 服务器去选择,如果以下不是您想要的,可以选择不同的服务器。

# ntpdate 0.us.pool.ntp.org

Finally, run the commands below to apply the changes to the hardware so it sticks after reboot.

最后,运行以下命令,更改硬件时钟,这样下次重启以后也会生效了。

# hwclock --systohc

玩得开心!