Debian 设置时区与同步网络时间

admin
2024-09-09 / 0 评论 / 10 阅读 / 正在检测是否收录...

首先设置 Debian 的时区,一般全自动安装好的 Debian 是 UTC 时间,与北京时间差 8 小时,所以我们最好将时区设置为常用的时区,方便使用与阅读。

timedatectl 是一个新工具,它作为 systemd 系统和服务管理器的一部分,代替旧的传统的用在基于 Linux 分布式系统的 sysvinit 守护进程的 date 命令。

timedatectl 命令可以查询和更改系统时钟和设置,你可以使用此命令来设置或更改当前的日期,时间和时区,或实现与远程 NTP 服务器的自动系统时钟同步。

timedatectl set-timezone "Asia/Shanghai"

要查看所有可用的时区,运行以下命令:

timedatectl list-timezones

一般重新设置时区以后,时间会与本地时间有差距,这时候建议使用同步来解决时间差异的问题。

注意:timedatectl 并不兼容 ntpd 等组件,请不要安装 ntpd 等组件,以免时间同步失效。

在一部分发行版里并没有内置 systemd-timesyncd 相关工具,这时候我们需要安装它:

apt install systemd-timesyncd

要增加 NTP 服务器,则需要修改 /etc/systemd/timesyncd.conf

vim  /etc/systemd/timesyncd.conf
[Time]#NTP=#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org#RootDistanceMaxSec=5#PollIntervalMinSec=32#PollIntervalMaxSec=2048

添加服务器需要取消 NTP= 这一行的注释,并填入 NTP 服务器地址。

例如:

NTP=pool.ntp.org 0.asia.pool.ntp.org 1.asia.pool.ntp.org 2.asia.pool.ntp.org

多服务器间用空格分割。

保存更改后,重启 systemd-timesyncd 服务

systemctl restart systemd-timesyncd

之后验证配置可以使用 timedatectl show-timesync --all

要启用并运行:

timedatectl set-ntp true

同步需要一点时间,可能会卡住。

查看状态使用:

timedatectl timesync-status

参考:

https://askubuntu.com/questions/1338764/cant-start-enable-timesyncd-on-20-04-lts

https://www.cnblogs.com/zhi-leaf/p/6282301.html

https://pan-xiao.gitbook.io/debian/config/ntp

0

评论

博主关闭了所有页面的评论