第 16.4 节 时间服务

配置时区

  • 使用 bsdconfig 设置时区。

  • 使用命令行:

# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

每个用户可以设置自己的时区,在用户的 shell 配置文件中设置 TZ 变量即可。

  • sh、bash、zsh

export TZ=CST-8 
# 或
export TZ=Asia/Shanghai
  • csh

setenv TZ CST-8
# 或
setenv TZ "Asia/Shanghai"
  • 在 crontab 配置文件中,设置 CRON_TZ 变量即可:

CRON_TZ=CST-8
0 8 * * * date >> ~/date.log

配置同步时间

设置启动 ntp 服务

# service ntpd enable

设置 ntp 服务开机时候启动

# sysrc ntpd_sync_on_start="YES"

编辑 ntp.conf 文件

# ee /etc/ntp.conf

添加附加时钟服务器

server time.windows.com
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 服务

# service ntpd start
  • 重启 ntp 服务

# service ntpd restart

显示当前时间

$ date
2025年 3月22日 星期六 15时27分29秒 CST

手动同步时间(可选)

# ntpdate time.windows.com
  • 当时间相差较大时必须使用该命令,其他命令不会生效:

# ntpdate -u pool.ntp.org

最后更新于