针对问题:在我们使用虚拟机配置好centos7后,发现服务器时间和当前时间对不上
第一种方法,命令有点多
- 通过命令查看时间不同步
date 或者 date -R - 修改/etc/sysconfig/clock文件如下内容,保存
vi /etc/sysconfig/clock
ZONE=“Asia/Shanghai”
UTC=true
ARC=false
- 重写/etc/localtime,执行如下,回车
rm /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- 重启服务器使时区生效,需要再次登录服务器
reboot - 安装ntp命令
yum install ntp 或者 yum install ntpdate
- 更新系统时间和硬件时间
ntpdate cn.pool.ntp.org
- crontab定时较对时间,设置crontab定时较对时间,编辑如下内容后保存
crontab -e
30 1 * * * ntpdate cn.pool.ntp.org ;hwclock --systohc
- 每次启动虚拟机启动同步时间
hwclock -w
- 再次查看时间,同步OK
date
第二种方法(推荐)
date # 查看时间
yum install ntp # 安装ntp
systemctl enable ntpd # 开机启动
systemctl start ntpd # 启动服务
timedatectl set-timezone Asia/Shanghai # 更改时区
timedatectl set-ntp yes # 启用ntp同步
ntpq -p # 同步时间 # 如果报错执行下面命令
service ntpd start # 启动ntpd
ntpdate -u cn.pool.ntp.org # 同步时间
临时:
查看当前时间:date
和阿里云同步:ntpdate ntp.aliyun.com
再次查看:date