Ubuntu 服务器 20.04 上的 timedatectl

Ubuntu 服务器 20.04 上的 timedatectl

我有一台 ubuntu 服务器 (20.04),其中 timedatectl 无法设置时区。第一次调用时,它总是失败,第二次调用时,它认为它已经成功,但实际上并没有:

事先时区是欧洲/巴黎:

root@oxpad:~# timedatectl 
               Local time: Wed 2021-08-11 02:02:44 CEST
           Universal time: Wed 2021-08-11 00:02:44 UTC 
                 RTC time: Wed 2021-08-11 00:02:44     
                Time zone: Europe/Paris (CEST, +0200)  
System clock synchronized: yes                         
              NTP service: active                      
          RTC in local TZ: no                          

** 时间为 CEST == 欧洲中部夏令时 **

root@oxpad:~# date
Wed Aug 11 02:02:46 CEST 2021

更改时区:

root@oxpad:~# timedatectl set-timezone Europe/London
Failed to set time zone: Access denied
root@oxpad:~# timedatectl set-timezone Europe/London
root@oxpad:~#

明显的成功

root@oxpad:~# timedatectl 
               Local time: Wed 2021-08-11 01:02:58 BST
           Universal time: Wed 2021-08-11 00:02:58 UTC
                 RTC time: Wed 2021-08-11 00:02:58    
                Time zone: Europe/London (BST, +0100) 
System clock synchronized: yes                        
              NTP service: active                     
          RTC in local TZ: no                         

但实际失败了

root@oxpad:~# date
Wed Aug 11 02:02:59 CEST 2021
root@oxpad:~# 

并且确实没有其他服务看到更新后的本地时间。似乎只有 timedatectl 认为它已经成功了。

这是完全可重复的。这是一个问题,因为我们将部署相当多的服务器,如果无法设置时区,我们就会遇到问题。我真的很想了解timedatectl和其他进程所看到的“实际”系统时间之间的关系date,因为这可能会导致对问题的理解。欢迎任何指点。

有趣的是,使用 timedatectl 在 Ubuntu 20.04 桌面系统上设置时区可以正常工作。这几乎就像 timedatectl 通过 dbus 设置时区,但 dbus 在服务器版本的操作系统上设置不正确。

所有这些测试都是在较新的安装上进行的,这些安装不超过几天,并且除了添加了一些软件包之外都是干净的。

有人知道这是怎么回事吗?如果能帮助我将不胜感激。

更多日志: 在 Ubuntu 20.04 服务器上,更改时间时日志中出现错误,而在 20.04 桌面上,相同的服务日志显示成功。错误:

root@oxpad:~# systemctl status systemd-timedated
● systemd-timedated.service - Time & Date Service
     Loaded: loaded (/lib/systemd/system/systemd-timedated.service; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd-timedated.service(8)
             man:localtime(5)
             https://www.freedesktop.org/wiki/Software/systemd/timedated

Aug 11 02:02:33 oxpad systemd[1]: systemd-timedated.service: Succeeded.
Aug 11 02:02:38 oxpad systemd[1]: Starting Time & Date Service...
Aug 11 02:02:38 oxpad systemd[1]: Started Time & Date Service.
Aug 11 02:02:38 oxpad systemd-timedated[35339]: Failed to set time zone: Permission denied
Aug 11 02:02:53 oxpad systemd-timedated[35339]: Failed to set time zone: Permission denied

答案1

我无法在普通的 Ubuntu 20.04 映像上复制您的错误。您应该检查命令是否更改/etc/timezone以及/etc/localtime符号链接。还要检查这些文件的权限。如果您正在运行selinux或其他安全软件,那么这可能是导致错误的另一个原因。

root@ubuntu:~# date
Wed Aug 11 16:05:25 UTC 2021
root@ubuntu:~# timedatectl
               Local time: Wed 2021-08-11 16:05:28 UTC
           Universal time: Wed 2021-08-11 16:05:28 UTC
                 RTC time: Wed 2021-08-11 16:05:29
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
root@ubuntu:~# cat /etc/timezone
Etc/UTC
root@ubuntu:~# ls -l /etc/localtime
lrwxrwxrwx 1 root root 29 Aug 11 15:39 /etc/localtime -> ../usr/share/zoneinfo/Etc/UTC


root@ubuntu:~# timedatectl set-timezone Europe/London
root@ubuntu:~# date
Wed Aug 11 17:05:51 BST 2021
root@ubuntu:~# timedatectl
               Local time: Wed 2021-08-11 17:05:59 BST
           Universal time: Wed 2021-08-11 16:05:59 UTC
                 RTC time: Wed 2021-08-11 16:06:00
                Time zone: Europe/London (BST, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
root@ubuntu:~# cat /etc/timezone
Europe/London
root@ubuntu:~# ls -l /etc/localtime
lrwxrwxrwx 1 root root 35 Aug 11 17:05 /etc/localtime -> ../usr/share/zoneinfo/Europe/London

这些步骤是自动更改时区的另一种方法。

  1. 更新/etc/localtime符号链接
  2. 跑步dpkg-reconfigure --frontend noninteractive tzdata

您可以看到这些步骤/etc/timezone也将更新文件。

root@ubuntu:~# date
Wed Aug 11 16:09:30 UTC 2021
root@ubuntu:~# ln -f -s /usr/share/zoneinfo/Europe/London /etc/localtime
root@ubuntu:~# dpkg-reconfigure --frontend noninteractive tzdata

Current default time zone: 'Europe/London'
Local time is now:      Wed Aug 11 17:10:30 BST 2021.
Universal Time is now:  Wed Aug 11 16:10:30 UTC 2021.

root@ubuntu:~# date
Wed Aug 11 17:10:34 BST 2021
root@ubuntu:~# cat /etc/timezone
Europe/London
root@ubuntu:~# ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Aug 11 17:10 /etc/localtime -> /usr/share/zoneinfo/Europe/London

相关内容