CentOS7自动更新时间。
1.查看当前时间是否与网络同步
#首先熟悉一下date命令的基本用法
#使用date命令查看当前时间
[root@localhost system]# date
#按照指定模式格式化当前时间
[root@localhost examples]# date +"%Y-%m-%d %H:%M:%S"
2023-08-29 12:42:57
#获取当前的时间戳,从1970-1-1到现在经过了多少秒
[root@localhost examples]# date +"%s"
1693284284
#获得指定日期的时间戳
[root@localhost examples]# date --date="2008-08-08" +"%s"
1218124800
#获取某年二月份有多少天
[root@localhost examples]# date --date="2008-03-01 1 day ago" +"%d"
29
#查看硬件时间的命令hwclock
[root@localhost system]# hwclock
2.自动同步网络时间
如果不同步,需要安装ntpdate工具。
yum -y install ntp ntpdate
设置系统时间与网络时间同步。
ntpdate cn.pool.ntp.org
#其它的时间服务器
ntpdate -u ntp.sjtu.edu.cn
将系统时间写入硬件时间。
hwclock --systohc
把更新时间设置到定时任务中
vim /etc/crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root /usr/sbin/ntpdate -u ntp.sjtu.edu.cn