查看系统时间
date
查看硬件时间
hwclock
将系统时间写入硬件时间
hwclock -w
修改时间
date -s
(将系统日期设定为2010年05月16日)
date -s 05/16/2010
date -s "2022-02-21 19:41:28"
NTP的时间同步
ntpdate 192.168.1.1
ntpdate ntpseerver.com
时间服务器上时间同步的方法
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
...
# 查看服务状态
service ntpd status
# 启动ntpd服务
systemctl start ntpd.service
# 停止ntpd服务
systemctl stop ntpd.service
# 设置开机自启动
systemctl enable ntpd.service
# 停止开机自启动
systemctl disable ntpd.service
# 查看服务当前状态
systemctl status ntpd.service
# 重新启动服务
systemctl restart ntpd.service
# 查看所有已启动的服务
systemctl list-units --type=service
启动ntp服务器
systemctl start ntpd
查看时钟源
ntpq -p
确保防火墙允许NTP流量通过:
sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload
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
00 01 * * * root /usr/sbin/ntpdate -q 192.168.85.20