在CentOS系统中,时间戳同步通常是通过NTP(网络时间协议)服务来实现的。以下是同步CentOS系统时间戳的步骤:
方法一:使用timedatectl
命令
-
检查当前时间同步状态
timedatectl status
-
启用NTP服务
sudo timedatectl set-ntp true
-
手动同步时间 如果需要立即同步时间,可以使用以下命令:
sudo ntpdate pool.ntp.org
-
重启NTP服务 有时重启NTP服务可以解决同步问题:
sudo systemctl restart chronyd
或者如果你使用的是
ntpd
:sudo systemctl restart ntpd
方法二:配置chrony
服务(CentOS 7及更高版本)
-
安装
chrony
sudo yum install chrony -y
-
启动并启用
chronyd
服务sudo systemctl start chronyd sudo systemctl enable chronyd
-
检查
chrony
状态sudo systemctl status chronyd
-
手动同步时间
sudo chronyc makestep
-
配置NTP服务器 编辑
/etc/chrony.conf
文件,添加或修改NTP服务器:sudo vi /etc/chrony.conf
添加以下行:
server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst
-
重启
chronyd
服务sudo systemctl restart chronyd
方法三:配置ntpd
服务(CentOS 6)
-
安装
ntpd
sudo yum install ntp -y
-
启动并启用
ntpd
服务sudo service ntpd start sudo chkconfig ntpd on
-
检查
ntpd
状态sudo service ntpd status
-
手动同步时间
sudo ntpdate pool.ntp.org
-
配置NTP服务器 编辑
/etc/ntp.conf
文件,添加或修改NTP服务器:sudo vi /etc/ntp.conf
添加以下行:
server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst
-
重启
ntpd
服务sudo service ntpd restart
通过以上步骤,你可以确保CentOS系统的时间戳与NTP服务器保持同步。