Linux:ntpdate服务器同步网络时间方法

4 篇文章 0 订阅
3 篇文章 0 订阅

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
希望可以帮到你!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值