在做項目時遇到需要對服務器時間進行設置的需求,很是頭疼,涉及時區轉換,很容易被時間弄糊塗
linux(至少包含centos,ubuntu)下的timedatectl命令用於設置時間,將同時修改系統時間和硬件時間
# 查看系統時間
timedatectl status
# 或
date
# 查看硬件時間
hwclock
# 查看支持的所有時區
timedatectl list-timezones
# 設置系統時間
timedatectl set-time "xxxx-xx-xx xx:xx:xx"
# 設置時區
timedatectl set-timezone Asia/Shanghai
# 更多的可以查看命令的幫助文檔
timedatectl --help
# 或
man timedatectl
ntp服務可以用於在聯網的情況下,根據指定時間服務器自動獲取互聯網時間
# 安裝ntp服務(centos下)
yum install ntp ntpdate -y
# 開啟ntp服務
systemctl start ntpdate
# 同步時間服務器時間
ntpdate time.windows.com
# ntp只會同步系統時間而不會同步硬件時間,若服務器重啟則系統時間會失效,依舊從硬件時間開始計時,所以使用ntp同步系統時間后需要將系統時間同步到硬件時間
hwclock -w