开始输入,可搜索发票、服务、域名、工单,以及 更多...
新开通的服务器默认时区往往是 UTC,日志时间与本地对不上;时间不准还会导致 HTTPS 证书校验失败、定时任务错乱、二次验证码无效。本文介绍如何用 timedatectl 修改 Linux 时区、用 chrony 开启 NTP 时间同步、修改主机名与 /etc/hosts,以及在 systemd-resolved、NetworkManager 和传统 resolv.conf 三种情况下正确设置 DNS。适用于 Debian/Ubuntu 与 Rocky Linux/AlmaLinux。
先查看当前状态,再从列表中选择时区名称。香港服务器常用 Asia/Hong_Kong,日本为 Asia/Tokyo,俄罗斯为 Europe/Moscow;如果服务器给多地用户使用,保持 UTC 也是很好的选择。修改时区不会改变真实时间,只改变显示方式;已运行的程序(如 PHP-FPM、MySQL)建议重启以读取新时区。
timedatectl # current time, time zone, NTP status
timedatectl list-timezones | grep -E 'Asia|UTC'
timedatectl set-timezone Asia/Hong_Kong # e.g. Asia/Tokyo, Asia/Shanghai, Europe/Moscow, UTC
timedatectl set-local-rtc 0 # keep the hardware clock in UTC
date
chrony 在网络波动时也能快速、稳定地校时,是 Rocky/AlmaLinux 的默认方案,Debian/Ubuntu 上安装后会替代 systemd-timesyncd。输出中带 ^* 的行表示正在使用的时间源,chronyc tracking 可看到当前偏差。
# Debian / Ubuntu (replaces systemd-timesyncd)
apt install -y chrony
systemctl enable --now chrony
# Rocky Linux / AlmaLinux
dnf install -y chrony
systemctl enable --now chronyd
timedatectl set-ntp true
chronyc sources -v # "^*" marks the server currently in use
chronyc tracking # "System time" shows the current offset
默认的发行版时间池一般够用。如需指定服务器,编辑 chrony 配置文件中的 pool/server 行;makestep 允许启动初期时间偏差较大时直接跳变校正。注意防火墙需允许出站 UDP 123 端口。
# Config file: /etc/chrony/chrony.conf (Debian/Ubuntu) or /etc/chrony.conf (Rocky/AlmaLinux)
# Replace or add the server lines, for example:
pool pool.ntp.org iburst
server time.cloudflare.com iburst
makestep 1.0 3
# Apply and step the clock immediately if it is far off
systemctl restart chrony # chronyd on Rocky/AlmaLinux
chronyc makestep
hostnamectl 修改后立即生效且重启不丢失。随后在 /etc/hosts 中加入新主机名,否则 sudo 可能提示 “unable to resolve host”,部分邮件和数据库程序也会启动变慢。若系统使用 cloud-init,还需设置 preserve_hostname,避免重启后被改回。提示符中的主机名要重新登录 SSH 后才会更新。
hostnamectl set-hostname web01.example.com
hostnamectl
# /etc/hosts - make the new name resolve locally
127.0.0.1 localhost
127.0.1.1 web01.example.com web01 # Debian/Ubuntu convention
# or map it to the server's public IP:
# 203.0.113.10 web01.example.com web01
# Images with cloud-init: stop it from resetting the hostname on reboot
grep -n preserve_hostname /etc/cloud/cloud.cfg
sed -i 's/^preserve_hostname:.*/preserve_hostname: true/' /etc/cloud/cloud.cfg
不同发行版管理 DNS 的方式不同,直接编辑 /etc/resolv.conf 可能在重启后被覆盖。先看这个文件是软链接还是普通文件、开头有没有 “Generated by” 注释,再决定修改方式。
ls -l /etc/resolv.conf
# -> ../run/systemd/resolve/stub-resolv.conf : managed by systemd-resolved (Ubuntu)
# -> regular file "Generated by NetworkManager": managed by NetworkManager (Rocky/AlmaLinux)
# -> regular file without such comment : edited by hand (common on Debian)
resolvectl status 2>/dev/null | head -n 20
根据上一步结果三选一:Ubuntu 等使用 systemd-resolved 的系统写入 resolved 的配置片段;Rocky/AlmaLinux 通过 nmcli 修改连接(连接名以 nmcli con show 显示为准);传统 Debian 直接编辑 resolv.conf。最后用 getent 测试解析。
# A) systemd-resolved (Ubuntu and some Debian setups)
mkdir -p /etc/systemd/resolved.conf.d
cat > /etc/systemd/resolved.conf.d/dns.conf <<'EOF'
[Resolve]
DNS=1.1.1.1 8.8.8.8 2606:4700:4700::1111
FallbackDNS=9.9.9.9
EOF
systemctl restart systemd-resolved
resolvectl status
# B) NetworkManager (Rocky Linux / AlmaLinux)
nmcli -t -f NAME,DEVICE con show --active
nmcli con mod "System eth0" ipv4.dns "1.1.1.1 8.8.8.8" ipv4.ignore-auto-dns yes
nmcli con up "System eth0"
# C) Plain /etc/resolv.conf (Debian with ifupdown)
cat > /etc/resolv.conf <<'EOF'
nameserver 1.1.1.1
nameserver 8.8.8.8
options timeout:2 attempts:2
EOF
# Test
getent hosts www.imidc.com
nmcli con up 会短暂重载网卡,配置错误可能导致 SSH 断开。请确认连接名正确,必要时通过客户中心提供的控制台(如有)操作,找不到可提交工单协助。通常是 UDP 123 被防火墙拦截,或 DNS 无法解析时间服务器域名。先检查 DNS,再放行出站 UDP 123,或改用可访问的 NTP 服务器。
这是 systemd-resolved 的本地存根地址,属正常现象。真实的上游 DNS 请用 resolvectl status 查看,修改方法见步骤 6 的 A。
一般不会,但如果邮件服务、宝塔或数据库授权中写死了旧主机名,需要同步修改。邮件服务器的主机名还应与 rDNS(PTR)记录一致,可参考“rDNS/PTR”教程。
如果按以上步骤操作后问题仍未解决,请提交工单联系 IMIDC 7×24 技术支持,并附上服务器 IP、系统版本、执行过的命令和完整报错信息,方便工程师快速定位。