国外linux服务器同步北京时间

公司服务器宕机重启后又要重新同步北京时间,但过程中遇到了一点不一样的问题,在此记录下。

首先由于服务器默认是美国时间,所以先更改时区:
将 /usr/share/zoneinfo目录下的上海时间覆盖/etc/localtime目录下默认的美国西部时间,然后输入命令可以看到时区已经改成东八区了

[root@cn2vps102 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y
[root@cn2vps102 ~]# date
Fri Sep 28 19:07:25 CST 2018
[root@cn2vps102 ~]#  date -R; date +%z  
Fri, 28 Sep 2018 19:08:07 +0800
+0800

但在时间上还存在几小时的差距,因此用ntp服务同步北京时间,试了两个地址

[root@cn2vps102 ~]# ntpdate -u 202.120.2.101
28 Sep 19:14:09 ntpdate[2831]: no server suitable for synchronization found
[root@cn2vps102 ~]# ntpdate cn.pool.ntp.org
Exiting, name server cannot be used: Temporary failure in name resolution (-3)28 Sep 19:41:15 ntpdate[3226]: name server cannot be used: Temporary failure in name resolution (-3)

都报错提示。
刚开始不知咋回事,然后想着用rdate命令也行,所以输入

[root@cn2vps102 ~]# rdate -s time-b.nist.gov
rdate: timeout for time-b.nist.gov

同样出现报错,而且时间也没同步成功。
然后开始百度,原因五花白门,各有各的,费尽功夫找到一位貌似问题相同的,根据错误信息Exiting, name server cannot be used: Temporary failure in name resolution (-3)28 Sep 19:41:15 ntpdate[3226]: name server cannot be used: Temporary failure in name resolution (-3) ,提示是你没有名字解析,你需要配置dns服务器地址(dns服务器上必须要有对应的条目),或者你可以使用IP地址来直接进行连接。

所以先ping下百度,发现报 unknown host www.baidu.com错误,但能ping通8.8.8.8的谷歌DNS服务器,查看
DNS客户机配置文件,发现服务器为127.0.0.1,意即本机,因此才解析不对。

[root@cn2vps102 ~]#  ping www.baidu.com
ping: unknown host www.baidu.com
[root@cn2vps102 ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=123 time=1.42 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=123 time=1.24 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=123 time=1.24 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=123 time=1.23 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=123 time=1.23 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=123 time=1.24 ms
64 bytes from 8.8.8.8: icmp_seq=7 ttl=123 time=1.23 ms

[root@cn2vps102 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search com
nameserver 127.0.0.1

然后编辑/etc/resolv.conf 文件更改nameserver为Google公共的DNS服务器8.8.8.8

[root@cn2vps102 ~]#  vi /etc/resolv.conf
[root@cn2vps102 ~]#  cat /etc/resolv.conf
# Generated by NetworkManager
search com
nameserver 8.8.8.8

重新ping百度发现能ping通了,证明dns服务器地址配置正确了,最后输入命令成功同步到北京时间,大功告成!

[root@cn2vps102 ~]#  /usr/sbin/ntpdate  time.stdtime.gov.tw
29 Sep 10:44:10 ntpdate[3236]: step time server 118.163.81.61 offset 53988.771688 sec
[root@cn2vps102 ~]# date
Sat Sep 29 10:44:25 CST 2018

猜你喜欢

转载自blog.csdn.net/u013068184/article/details/82899307