1、查看现在的系统时间:
[root@Itstudy current]# date
2019年 10月 12日 星期六 15:35:18 CST
2、查看本机是否安装ntpdate服务,如果没有安装,先执行命令进行安装:
yum install -y ntpdate
3、输入# ntpdate time.nist.gov
同步网络时间:
(1)如果出现下面的结果,说明同步网络时间成功(直接进行第四步)
[root@Itstudy current]# ntpdate cn.pool.ntp.org
12 Oct 15:14:53 ntpdate[19160]: step time server 84.16.67.12 offset 3124.470738 sec
(2)如果出现下面这种情况:
[root@Itstudy current]# ntpdate time.nist.gov
12 Oct 14:03:52 ntpdate[19085]: the NTP socket is in use, exiting
原因是由于 xntpd 已经绑定到了该 Socket。
可以试以下两种方法:
1)使用命令# lsof –i:123
找到进程的PID
[root@Itstudy current]# lsof -i:123
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ntpd 10378 ntp 16u IPv4 80754 0t0 UDP *:ntp
ntpd 10378 ntp 17u IPv6 80755 0t0 UDP *:ntp
ntpd 10378 ntp 18u IPv6 80759 0t0 UDP localhost:ntp
ntpd 10378 ntp 19u IPv6 80760 0t0 UDP [fe80::20c:29ff:fe9b:3743]:ntp
ntpd 10378 ntp 20u IPv4 80761 0t0 UDP localhost:ntp
ntpd 10378 ntp 21u IPv4 80762 0t0 UDP Itstudy:ntp
可以看到该进程的PID是:10378
然后使用命令:# kill -9 10378
将其杀死
2)使用命令: # service ntpd stop
将ntpd服务停掉
[root@Itstudy current]# service ntpd stop
关闭 ntpd: [确定]
然后再次执行:# ntpdate time.nist.gov
此时应该不会出现(2)中的情况了。
4、同步时间成功后调整硬件时间#hwclock -w
执行成功后, 查看系统硬件时间(不出意外的话,现在date和hwclock现实的时间均为internet时间)
[root@Itstudy current]# hwclock -w
[root@Itstudy current]# date
2019年 10月 12日 星期六 15:16:18 CST
#date
#hwclock
执行上述命令,显示的时间应该一样的
[root@Itstudy current]# date
2019年 10月 12日 星期六 15:17:23 CST
[root@Itstudy current]# hwclock
2019年10月12日 星期六 15时17分30秒 -0.219452 seconds
5、定时执行时间同步任务,利用crontab -e 来添加定时任务
#* 1/ * * * root ntpdatetime.nuri.net;hwclock -w
即:每隔一个小时同步一下internet时间。
[root@Itstudy current]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
* */1 * * * root ntpdatetime.nuri.net;hwclock -w
~
~
附:
如果上面time.nist.gov服务器同步不了,可以换下面几个时间服务器试一下:
0.# ntpdate time.nist.gov
1.# ntpdate time.nuri.net
2.# ntpdate asia.pool.ntp.org
3.# ntpdate ntp.api.bz
希望可以帮到你!!!