在 Linux 系统(包括高斯Linux)中校准服务器时间通常有两种方法:使用 ntp/chrony 同步网络时间协议(NTP)或手动设置。以下是详细步骤:方法一:使用 ntp 或 chrony 同步网络时间(推荐)
1. 检查当前时间设置
bash
timedatectl输出示例:
text
Local time: Tue 2023-10-10 09:45:28 UTC
Universal time: Tue 2023-10-10 09:45:28 UTC
Time zone: UTC (UTC, +0000)
NTP enabled: yes
NTP synchronized: no2. 安装时间同步服务
使用ntp(传统方式):
yum install ntp # CentOS/RHEL/高斯Linux apt install ntp # Debian/Ubuntu使用chrony(现代推荐):
yum install chrony # CentOS/RHEL/高斯Linux apt install chrony # Debian/Ubuntu
3. 配置时间服务器
编辑配置文件
对于ntp
vim /etc/ntp.conf添加公共 NTP 服务器(例如):
server ntp.aliyun.com iburst server cn.pool.ntp.org iburst对于chrony
vim /etc/chrony.conf添加服务器:
server ntp.aliyun.com iburst server cn.pool.ntp.org iburst
4. 启动服务并启用开机自启
ntpsystemctl start ntpd systemctl enable ntpdchronysystemctl start chronyd systemctl enable chronyd
5. 强制立即同步时间
对于chrony
chronyc makestep对于ntp
ntpq -p # 查看同步状态 ntpd -gq # 强制同步(如果服务已运行)
6. 验证时间状态
timedatectl # 检查 "NTP synchronized" 是否为 yes
chronyc tracking # 如果使用 chrony
方法二:手动设置时间(临时调整)
1. 手动设置时间
bash
timedatectl set-time "2023-10-10 12:30:00"2. 更新硬件时钟(BIOS时间)
hwclock --systohc # 将系统时间写入硬件时钟方法三:使用 date 命令(不推荐生产环境)
date -s "2023-10-10 12:30:00" # 手动修改时间(重启可能失效)解决常见问题
时间无法同步:
检查防火墙是否放行 UDP 123 端口:
firewall-cmd --add-service=ntp --permanent firewall-cmd --reload确保时区设置正确:
timedatectl set-timezone Asia/Shanghai # 设置为上海时区
chrony同步状态检查:chronyc sources -v # 查看时间源状态 chronyc tracking # 查看同步详细信息
总结
- 生产环境必用:通过
ntp或chrony自动同步网络时间。 - 操作顺序:安装服务 → 配置 NTP 服务器 → 启动服务 → 验证状态。
- 检查命令:
timedatectl、ntpq -p、chronyc tracking。
评论0
暂时没有评论