在linux上设置编码、地区、时区和NTP服务器以同步时间的例子

133 阅读1分钟

在这个例子中,我们将设置服务器编码、地域、时区和NTP服务器,以便与我们所在的国家保持一致。我是在英国!

设置

$ apt-get update -y

语言和编码

$ sed -i "s/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/g" /etc/locale.gen
$ locale-gen en_GB.UTF-8
$ update-locale LANG=en_GB.UTF-8
$ update-locale LC_ALL=en_GB.UTF-8
$ update-locale LANGUAGE=en_GB:en
$ update-locale

时区

$ timedatectl set-timezone Europe/London

ntp

# Instal
$ apt-get install -y ntp

# Configure
cat > /etc/ntp.conf <driftfile /var/lib/ntp/ntp.drift

server 0.uk.pool.ntp.org iburst
server 1.uk.pool.ntp.org iburst
server 2.uk.pool.ntp.org iburst
server 3.uk.pool.ntp.org iburst

restrict 127.0.0.1
restrict ::1
EOF

# Correct the time (Not needed for Ubuntu)
service ntp stop
ntpd -gq
service ntp start

# Enable startup
systemctl enable ntp

# Synchronize the system clock (Not needed for Ubuntu)
$ timedatectl set-ntp true

如果你需要重新启动ntp,请运行$ systemctl restart ntp 命令。

状态

蝶变

$ timedatectl
      Local time: Sun 2018-05-27 10:35:00 BST
  Universal time: Sun 2018-05-27 09:35:00 UTC
        RTC time: Sun 2018-05-27 09:34:59
       Time zone: Europe/London (BST, +0100)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2018-03-25 00:59:59 GMT
                  Sun 2018-03-25 02:00:00 BST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2018-10-28 01:59:59 BST
                  Sun 2018-10-28 01:00:00 GMT
$ locale
LANG=en_GB.UTF-8
LANGUAGE=en_GB:en
...
LC_ALL=en_GB.UTF-8

乌班图

$ timedatectl
      Local time: Sun 2018-05-27 10:35:00 GMT
  Universal time: Sun 2018-05-27 10:35:00 UTC
        RTC time: Sun 2018-05-27 10:35:00
       Time zone: Europe/London (GMT, +0000)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
$ locale
LANG=en_GB.UTF-8
LANGUAGE=en_GB:en
...
LC_ALL=en_GB.UTF-8