date 命令:
Linux查看时间:
# date
Tue Mar 23 18:15:03 CST 2021
date '+%Y%M%D' 按照格式显示当前日期:
# date "+%Y-%m-%d %H:%M:%S"
2021-03-23 18:16:30
Linux设置时间:
# date -s "2021323 18:18:00"
2021年 03月 23日 星期五 18:18:00 CST
Linux 服务器时间同步:
# ntpdate -u ntp.sjtu.edu.cn 202.120.2.101
注释:时间调整就是同步一下ntp服务器
# hwclock -w
注释:同步硬件时间
在系统里添加定时任务:每60分钟同步一次服务器时间
# vi /etc/crontab
* 60 * * * root ntpdate -u ntp.sjtu.edu.cn 202.120.2.101;hwclock -w
注释:请在最后添加这一段命令
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
* 60 * * * root ntpdate -u ntp.sjtu.edu.cn 202.120.2.101;hwclock -w
# wq
注释:保存退出
此方法只适用于:服务器可以正常访问外网