查看系统时间
date
查看硬件时间
hwclock
将系统时间写入硬件时间
hwclock -w
修改时间
date -s
(将系统日期设定为2010年05月16日)
date -s 05/16/2010
时间服务器上时间同步的方法
CentOS7之前采用ntp服务自动同步时间,CentOS7推荐使用chrony同步时间,当然ntp仍然可以使用,chrony官网列举了诸多chrony优于ntp的功能(ntp与chrony的对比:https://chrony.tuxfamily.org/comparison.html)。
若要立刻将系统时间同步为NTP服务时间,使用ntpdate命令,也可以配置计划任务定期使用ntpdate命令同步时间,从而就不用使用ntp或chrony服务,减少监听的端口,增加系统安全性。
1.ntp的安装配置
安装ntp
yum -y install ntp
配置ntp同步阿里云时间服务器
vim /etc/ntp.conf
...
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
#server 0.centos.pool.ntp.org iburst
...
启动ntp服务器
systemctl start ntpd
2chrony的安装配置
安装chrony
yum -y install chrony
配置chrony
vim /etc/chrony.conf
server ntp1.alyun.com
server ntp2.alyun.com
server ntp3.alyun.com
#server 0.centos.pool.ntp.org iburst...
启动chrony
systemctl start chronyd
3配置计划任务,使用ntpdate同步时间
启动并开机启动计划任务cron
systemctl start crond
systemctl enable crond
配置计划任务,每30分钟同步一次
crontab -e
*/30 * * * * /usr/sbin/ntpdate ntp1.aliyun.com