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