11人参与 • 2025-10-23 • Linux
在开始配置之前,首先需要了解系统当前的时区状态:
# 查看当前时间和时区信息 timedatectl # 传统方法查看时区 date ls -l /etc/localtime
timedatectl命令会显示详细信息,包括本地时间、utc时间、当前时区、ntp同步状态等。
linux系统将时区文件存储在/usr/share/zoneinfo/目录下:
# 查看所有可用时区 timedatectl list-timezones # 查看特定地区的时区(如亚洲) timedatectl list-timezones | grep asia # 直接浏览时区文件目录 ls /usr/share/zoneinfo/ ls /usr/share/zoneinfo/asia/
方法一:使用timedatectl(推荐)
现代linux系统(systemd环境)推荐使用timedatectl命令:
# 设置为上海时区 sudo timedatectl set-timezone asia/shanghai # 设置为utc时区 sudo timedatectl set-timezone utc
优势:
方法二:修改软链接
传统方法通过修改/etc/localtime软链接:
# 备份原有时区文件 sudo mv /etc/localtime /etc/localtime.bak # 创建新的时区软链接 sudo ln -sf /usr/share/zoneinfo/asia/shanghai /etc/localtime
方法三:使用tzselect命令
交互式时区选择工具:
# 执行tzselect命令 tzselect # 按照提示选择:亚洲 -> 中国 -> 北京时间 # 最后会提示将tz环境变量添加到.profile文件 echo 'export tz="asia/shanghai"' >> ~/.profile source ~/.profile
方法四:修改/etc/timezone文件
某些发行版(如debian/ubuntu)需要修改此文件:
echo "asia/shanghai" | sudo tee /etc/timezone
# 验证时区设置 timedatectl date date +"%z %z" # 显示时区缩写和偏移量 # 验证时区文件 ls -l /etc/localtime readlink /etc/localtime
现代linux系统主要提供两种时间同步方案:
| 特性 | chrony | ntpd |
|---|---|---|
| 同步速度 | 快速同步,适合间歇性网络 | 启动较慢,需要稳定网络 |
| 精度 | 更高精度,减少时间跳跃 | 标准精度 |
| 资源占用 | 低资源占用 | 相对较高 |
| 虚拟化环境 | 优秀,适应时钟频率变化 | 一般 |
| 兼容性 | 现代发行版默认 | 老旧系统支持 |
推荐选择:现代linux发行版(rhel 8+、centos 8+、ubuntu 18.04+)默认使用chrony。
2.2.1 安装chrony
# rhel/centos sudo yum install -y chrony # ubuntu/debian sudo apt install -y chrony # 启动并设置开机自启 sudo systemctl start chronyd sudo systemctl enable chronyd
2.2.2 配置文件详解
编辑/etc/chrony.conf:
# 基础配置示例 server 0.cn.pool.ntp.org iburst server 1.cn.pool.ntp.org iburst server 2.cn.pool.ntp.org iburst server 3.cn.pool.ntp.org iburst # 使用阿里云ntp服务器 server time1.aliyun.com iburst server time2.aliyun.com iburst # 配置参数说明 driftfile /var/lib/chrony/drift makestep 1.0 3 rtcsync keyfile /etc/chrony.keys leapsectz right/utc logdir /var/log/chrony # 允许特定网段同步时间(可选) allow 192.168.1.0/24 # 本地可以作为时间服务器(可选) local stratum 10
关键参数解释:
iburst:加速初始同步makestep 1.0 3:允许最大1秒的时间跳跃,最多3次rtcsync:同步硬件时钟allow:允许客户端访问local stratum 10:本地作为时间服务器2.2.3 启用ntp同步
# 启用ntp同步 sudo timedatectl set-ntp yes # 重启chrony服务 sudo systemctl restart chronyd # 查看同步状态 timedatectl status
2.2.4 使用chronyc监控
# 查看时间源状态 chronyc sources -v # 查看同步状态 chronyc tracking # 查看活动状态 chronyc activity # 强制同步 chronyc makestep # 添加时间源 chronyc add server time.nist.gov
2.3.1 安装ntp
# rhel/centos sudo yum install -y ntp # ubuntu/debian sudo apt install -y ntp
2.3.2 配置ntp.conf
编辑/etc/ntp.conf:
# 指定时间服务器 server 0.cn.pool.ntp.org server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org # 阿里云ntp服务器 server time1.aliyun.com server time2.aliyun.com # 默认配置 driftfile /var/lib/ntp/drift pidfile /var/run/ntpd.pid logfile /var/log/ntp.log # 访问控制 restrict default kod nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1 # 允许局域网同步 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
2.3.3 启动ntp服务
# 启动ntp服务 sudo systemctl start ntpd sudo systemctl enable ntpd # 查看同步状态 ntpq -p
如果只需要快速同步时间,可以使用ntpdate:
# 安装ntpdate sudo yum install -y ntpdate # rhel/centos sudo apt install -y ntpdate # ubuntu/debian # 同步时间 sudo ntpdate time.nist.gov sudo ntpdate time1.aliyun.com # 同步后更新硬件时钟 sudo hwclock -w
# 默认使用chrony sudo systemctl status chronyd # 时区设置 sudo timedatectl set-timezone asia/shanghai
# 默认使用chrony sudo systemctl status chrony # 时区设置 sudo timedatectl set-timezone asia/shanghai
# 默认使用systemd-timesyncd sudo systemctl status systemd-timesyncd # 时区设置 sudo timedatectl set-timezone asia/shanghai # 或者使用传统方法 sudo dpkg-reconfigure tzdata
# 使用ntp sudo service ntpd start sudo chkconfig ntpd on # 时区设置 sudo cp /usr/share/zoneinfo/asia/shanghai /etc/localtime
# 综合状态查看 timedatectl status # chrony详细状态 chronyc tracking chronyc sources -v # ntp状态 ntpq -p # 查看系统日志 sudo journalctl -u chronyd -f sudo tail -f /var/log/chrony/log
问题1:时间同步失败
# 检查网络连接 ping time.nist.gov # 检查防火墙 sudo firewall-cmd --list-all sudo firewall-cmd --add-service=ntp --permanent # 检查服务状态 sudo systemctl status chronyd # 手动强制同步 sudo chronyc makestep
问题2:时区设置不生效
# 检查时区文件 ls -l /etc/localtime # 重新设置时区 sudo timedatectl set-timezone asia/shanghai # 重启相关服务 sudo systemctl restart systemd-timedated
问题3:硬件时间不同步
# 查看硬件时间 sudo hwclock --show # 同步硬件时间 sudo hwclock -w # 系统时间同步到硬件 sudo hwclock -s # 硬件时间同步到系统
# 调整同步间隔(chrony.conf) minpoll 4 maxpoll 9 # 限制日志大小 logdir /var/log/chrony log measurements statistics tracking
公共ntp服务器推荐:
选择原则:
iburst选项加速初始同步# 限制ntp访问 restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery # 启用认证(可选) keys /etc/chrony.keys controlkey 1 requestkey 1
# 定期检查同步状态 chronyc tracking # 监控时间偏移 watch -n 1 'chronyc tracking | grep "system time"' # 设置定时同步(如果需要) echo "0 * * * * /usr/sbin/chronyc makestep" | sudo crontab -
到此这篇关于linux中系统服务器时区设置与网络时间同步的完整指南的文章就介绍到这了,更多相关linux服务器时区设置与网络时间同步内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论