1 时间的相关命令
1.1 hwclock
读取或设置系统硬件时钟(RTC)的时间
常用命令:
// 显示RTC时间
firefly@firefly:/etc$ sudo hwclock -r
2023-09-08 15:26:55.389199+06:30
//将系统时间设置为RTC硬件时间 (system to hardware clock )
sudo hwclock --systohc
//简写为
sudo hwclock -w
// 将RTC硬件时间设置为系统时间(hardware clock to system)
sudo hwclock --hctosys
//简写为
sudo hwclock -s
1.2 timedatectl
查询或更改系统时钟的时间和配置
常用命令:
// 设置系统时间,等同于data命令设置时间
sudo timedatectl set-time "2023-09-16 08:00:00"
//设置时区
sudo timedatectl set-timezone Africa/Ceuta
//.显示系统日期的属性
sudo timedatectl
1.3 date
显示或设置系统当前时间,相对简单所以常用于编写shell脚本时使用
常用命令:
//显示当前时间和日期
data
//设置时间与日期
sudo date -s "2023-09-16 18:00:00"
1.4 timedatectl 与 date 区别
timedatectl 是一个 systemd 工具,提供了更多的功能和选项,如管理时区、同步网络时间等。
date 命令相对简单,简单地设置系统时间为给定值,也可以显示当前时间和日期。
2 遇到的问题
2.1 设置时间界面提示权限不足(lxqt-admin-time)
//直接进入时间,时区设置交互界面
sudo lxqt-admin-time
报错log:
firefly@firefly:/$ systemctl status systemd-timedated
● systemd-timedated.service - Time & Date Service
Loaded: loaded (/lib/systemd/system/systemd-timedated.service; static; ven>
Active: inactive (dead)
Docs: man:systemd-timedated.service(8)
man:localtime(5)
https://www.freedesktop.org/wiki/Software/systemd/timedated
Sep 16 11:48:55 firefly systemd-timedated[2154]: Failed to set time zone: Permission denied
Sep 16 11:49:31 firefly systemd-timedated[2154]: Failed to set time zone: Permission denied
Sep 16 11:49:42 firefly systemd-timedated[2154]: Failed to set time zone: Permission denied
Sep 16 11:50:09 firefly systemd-timedated[2154]: Failed to set time zone: Permission denied
Sep 16 11:50:35 firefly systemd-timedated[2154]: Failed to set time zone: Permission denied
Sep 16 11:51:08 firefly systemd[1]: systemd-timedated.service: Succeeded.
Sep 16 11:51:48 firefly systemd[1]: Starting Time & Date Service...
Sep 16 11:51:48 firefly systemd[1]: Started Time & Date Service.
Sep 16 11:52:06 firefly systemd-timedated[2247]: Failed to set RTC to local:Permission denied
Sep 16 11:53:07 firefly systemd[1]: systemd-timedated.service: Succeeded.
2.2 处理办法
2.2.1 直接修改时区链接文件
firefly@firefly:/etc$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 31 Sep 16 2023 /etc/localtime -> /usr/share/zoneinfo/Asia/Yangon
如上所示 /etc/localtime文件就是个链接文件,所以更改链接对应的时区文件就可以实现
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
2.2.1 修改etc目录的所属用户,所属用户组为root
drwxrwxr-x 1 firefly firefly 4096 Sep 16 15:22 etc
如上所示 /etc目录所属用户,所属用户组都是firefly,导致权限不足
sudo chown root:root /etc
3 题外话
3.1 dpkg-reconfigure重新配置软件包
//重新配置tzdata这个时区数据软件包的配置
sudo dpkg-reconfigure --frontend noninteractive tzdata
//直接运行会进入交互式的界面,选择对应时区
sudo dpkg-reconfigure tzdata
4 参考连接
https://askubuntu.com/questions/1357274/timedatectl-on-ubuntu-server-20-04
https://www.cnblogs.com/coolYuan/p/16699972.html