设置CentOS7的时间与网络同步

  • Post author:
  • Post category:其他

1.设置时区为北京时间
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai

2.查看系统时间
[root@localhost ~]# timedatectl
      Local time: 四 2023-03-02 17:40:41 CST                    #系统时间
      Universal time: 四 2023-03-02 09:40:41 UTC             #国际协调时间
      RTC time: 六 2023-02-25 05:35:45                             #硬件时间
      Time zone: Asia/Shanghai (CST, +0800)                    #时区
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

3.设置时间同步网络
本节所有命令均在root用户下运行。

        3.1 安装ntp服务

            如果已经安装,此命令会自动无效。

[root@localhost ~]# yum install -y ntp

     3.2 修改ntp相关参数

[root@localhost ~]# vi /etc/sysconfig/ntpd
 

下图是初始参数

 

将其修改为:

 

用于修改的文本如下:

SYNC_HWCLOCK=yes
OPTIONS="-g -x"

3.3 启动ntp服务
在这里实际调用的是重启命令,这样可以忽略ntp服务的当前状态。

systemctl restart ntpd

3.4 设置ntp服务开机启动
systemctl enable ntpd

3.5 设置Linux系统时钟与远程NTP服务器同步
timedatectl set-ntp true

3.6 再用 hwclock -w 将系统时间赋值给硬件时间

[root@localhost ~]# hwclock -r
2023年02月25日 星期六 13时56分24秒  -0.323442 秒
[root@localhost ~]# hwclock -w

注:我们常用的date命令,获取的是系统时间
[root@localhost ~]# date
2023年 03月 02日 星期四 18:18:44 CST 

    


版权声明:本文为adminstate原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。