在虚拟机中同步网络时间

在是用虚拟机的时候,往往时间对不上,这就使强迫症不得安心学习了,解决方法如下:

作为中国的一员,在安装虚拟机的时候,可以设置上海时间作为参考:

1.把上海时间文件cp到etc文件下的localtime文件里。

[root@localhost ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp:是否覆盖"/etc/localtime"? y

2.同步时间需要用到ntpdate服务,虚拟机没有的就简单安装一下:

[root@localhost ~]# yum install -y ntpdate

已加载插件:fastestmirror, refresh-packagekit, security

设置安装进程

Loading mirror speeds from cached hostfile

 * base: mirror.bit.edu.cn

 * extras: mirror.bit.edu.cn

 * updates: mirrors.163.com

包 ntpdate-4.2.6p5-15.el6.centos.x86_64 已安装并且是最新版本

无须任何处理

3.安装完之后,同步服务器时间与虚拟机时间:

[root@localhost ~]# ntpdate time.nist.gov

7 Sep 19:00:17 ntpdate[3064]: no server suitable for synchronization found

如果出现报错,尝试启动一下ntpdate服务:

[root@localhost ~]# service ntpdate start

ntpdate: 与时间服务器同步:                                [确定]

8.ntpdate服务启动后,输入命令date,发现时间已经同步:

[root@localhost ~]# date

2019年 09月 07日 星期六 11:06:55 CST

9.如果想看同步过程,可以安装ntp-perl服务:

[root@localhost ~]# yum install -y ntp-perl(查看同步过程)

已加载插件:fastestmirror, refresh-packagekit, security

设置安装进程

Loading mirror speeds from cached hostfile

 * base: mirror.bit.edu.cn

 * extras: mirror.bit.edu.cn                                            

完毕!

[root@localhost ~]# ntptrace

/usr/sbin/ntpq: read: Connection refused

10.最后一步,把系统时间同步到硬件,防止系统重启后时间恢复:

[root@localhost ~]# hwclock -w

[root@localhost ~]# hwclock

2019年09月07日 星期六 11时08分01秒  -0.250896 seconds

[root@localhost ~]#

11.重启虚拟机,执行date命令:

[E:\~]$

Connecting to 10.0.0.11:22...

Connection established.

To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Sat Sep  7 11:12:03 2019 from 10.0.0.1
===========================================
       
         Welcome to HSP's system,please not use rm command.
         大胆折腾,百味才是人生!
        2019-09-07 11:12:35 星期六
       
===========================================
[root@localhost ~]# ntpdate time.nist.gov

 7 Sep 11:21:01 ntpdate[2694]: no server suitable for synchronization found

[root@localhost ~]# date

2019年 09月 07日 星期六 11:21:04 CST

[root@localhost ~]#

发布了49 篇原创文章 · 获赞 149 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/youmatterhsp/article/details/100593317