Linux服务器与网络时间不同步解决办法
1.查看时区
[root@lw ~]# timedatectl
如果不是北京时区,修改为北京时间
[root@lw ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2.时间同步脚本撰写
[root@lw ~]# cat /mnt/TimeSync.sh
#!/bin/bash
ntpdate us.pool.ntp.org
[ $? == 0 ] && hwclock -w
3.定时执行时间同步任务【 crontab -e 不生效就写在/etc/crontab里面】
[root@lw ~]# cat /etc/crontab
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
*/1 * * * * root /usr/bin/bash /mnt/TimeSync.sh > /dev/null 2>&1