Linux下的timedatectl命令和ntp服务

在做项目时遇到需要对服务器时间进行设置的需求,很是头疼,涉及时区转换,很容易被时间弄糊涂

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

猜你喜欢

转载自www.cnblogs.com/luozeyang/p/12161857.html
今日推荐