Vmware中虚拟机设置NTP服务时间同步

时间:2024-03-25 09:50:21

实验状况:虚拟机中的一台Centos7系统想要连接内部网络NTP服务器(IP:192.168.100.100),但其本身不能连接互联网。

一、更改yum源从本地硬盘

打开VMware中“虚拟机M选择设置”,再选择“CD/DVD(IDE)”,选择“使用ISO映像文件(M)”,选择合适的系统版本。Vmware中虚拟机设置NTP服务时间同步

Vmware中虚拟机设置NTP服务时间同步

二、挂载光盘

光盘放好后挂载光盘

先新建挂载点:# mkdir -p  /mnt/cdrom,然后再重新进行挂载。

mount /dev/cdrom  /mnt/cdrom

或者 mount /dev/sr0  /mnt/cdrom

Vmware中虚拟机设置NTP服务时间同步

三、修改yum源(临时更改

[[email protected] ~]# cd /etc/yum.repos.d/

Vmware中虚拟机设置NTP服务时间同步

[[email protected]_share_02 yum.repos.d]# vi CentOS-Base.repo

# CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and

# geographically close to the client.  You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you can try the

# remarked out baseurl= line instead.

#

#

[base]

name=CentOS-$releasever - Base

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra

baseurl=file:///mnt/cdrom/     #设置路径

gpgcheck=0                #表示对从这个源下载的rpm包不进行校验

enable=1           #表示启用这个源

最后 wq! 保存退出。

四、安装NTP

1、查看/etc/yum.repo.d列表里面已经安装和可以安装的程序包

[[email protected] ~]# yum list 

若发现出现如下报错。基本的原因就是自己的网络设置问题,解决好就正常了。

Vmware中虚拟机设置NTP服务时间同步

若发现自己的网络问题已正常,使用yum list 还是报如下错误:

Vmware中虚拟机设置NTP服务时间同步

可以先查看自己之前挂载的情况:

Vmware中虚拟机设置NTP服务时间同步

发现自己的挂载没有了,需要重新再挂载。

Vmware中虚拟机设置NTP服务时间同步

这时候再使用[[email protected] ~]# yum list 查看,就会发现已正常。

Vmware中虚拟机设置NTP服务时间同步

2、安装NTP

[[email protected] ~]# yum install ntp

Vmware中虚拟机设置NTP服务时间同步

 

四、修改配置文件

[[email protected] ~]#  vi /etc/ntp.conf

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not

# permit the source to query or modify the service on this system.

restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could

# be tightened as well, but to do so would effect some of

# the administrative functions.

restrict 127.0.0.1

restrict ::1

# Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 192.168.100.100   增加的NTP服务器地址

server 0.centos.pool.ntp.org iburst

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey        # broadcast server

#broadcastclient                        # broadcast client

#broadcast 224.0.1.1 autokey            # multicast server

#multicastclient 224.0.1.1              # multicast client

#manycastserver 239.255.254.254         # manycast server

#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

五、设置NTP服务

[[email protected] ~]# systemctl restart ntpd.service      #重启NTP服务

[[email protected] ~]# systemctl status ntpd.service       #查看NTP服务状态

● ntpd.service - Network Time Service

   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)

   Active: active (running) since 一 2018-07-30 13:15:15 CST; 52s ago

  Process: 34667 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 34668 (ntpd)

   CGroup: /system.slice/ntpd.service

           ├─34668 /usr/sbin/ntpd -u ntp:ntp -g

           └─34669 /usr/sbin/ntpd -u ntp:ntp -g

可以看到当前的状态为 running

六、查看是否成功

[[email protected] ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

 192.168.100.100     .GPS.            1 u    1   64    7    7.840  240671.   3.119

七、设置开机自启动

[[email protected] ~]# systemctl enable  ntpd.service

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.