centos7时间同步教程

针对问题:在我们虚拟机配置好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

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

窦再兴

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值