0719---Linux虚拟机时间同步问题

Linux虚拟机时间同步问题

​ 近期单位新创建了一台虚拟机(在ESXI上创建),安装的系统是centos7,并为虚拟机配置了正确的时区,运行虚拟机后,发现本地时间总是快8小时,随后配置了ntp服务器,但在虚拟机重新启动后,本地时间还是快8小时。本文详细分析了原因,并给出了彻底解决ESXI虚拟机时间同步问题的方法。

一 ESXI 主机的时间设置

在这里插入图片描述

1 虚拟机时间快8个小时的原因

​ ESXI 主机在有些场合是不连接互联网的,所以也就无法做到与互联网NTP服务器的时间同步,只能手动设置时间。在手动设置时间的过程中,ESXI主机没有时区的概念,只有一个UTC时间,但用户往往错把这个时间设置为此刻的本地时间。所以虚拟机在启动的过程中,往往会自动同步esxi主机的时间,并把虚拟机的UTC时间同步为ESXI的时间,加之虚拟机设置了时区,所有虚拟机本机时间就会加上8个小时,这就是内在的原因。

​ 虚拟机没有自己的BIOS时钟,开机时只能靠ESXI主机的时间来同步。

2 关于ESXI主机的时间和日期

  1. ESXI 没有时区的概念,只有一个UTC时间.
  2. 在ESXI 中可以设定ntp服务器,来自动调整 esxi主机的utc时间.
  3. 在ESXI主机中创建的虚拟机,每当虚拟机重新启动时,都会同步ESXI 主机的时间.
  4. 如果虚拟机可以联网,也可以单独设置虚拟机的NTP时间同步。

二 虚拟机中安装与配置时间同步服务 chrony

# 安装 chrony
yum install chrony ntpdate
# 启动chrony
systemctl start chronyd
# 设置开机启动
systemctl enable chronyd

三 配置本机自动与互联网时间服务器同步

  1. yum 安装的 chrony的配置文件
rpm -ql chrony
  1. chrony 初始配置
vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony
                               
  1. 一般把公共NTP服务器设置为国内源
vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 国家授时中心NTP服务器
pool  ntp.ntsc.ac.cn  iburst
# 阿里云时间源
pool  ntp.aliyun.com   iburst
# 腾讯云源
pool  ntp.tencent.com  iburst
# 上海交通大学
pool  ntp.sjtu.edu.cn  iburst

#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

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

  1. 虚拟机与ntp服务器同步
#方法1运行客户端程序可以随时与 NTP服务器同步
ntpdate -u ntp.aliyun.com、
#方法2重新启动NTP服务
systemctl restart chronyd

四 把本机作为时间服务器对外提供时间服务

  1. 保证本机与其他主机之间的网络畅通
  2. 仅需修改 chrony.conf 的 少量参数
vim /etc/chrony.conf
.......
# 设置允许 10.31.0.0 这个网段的机器作为客户端来本机同步时间
# Allow NTP client access from local network.
allow 10.31.0.0/16

# Serve time even if not synchronized to a time source.
local stratum 10
....
  1. 重新启动 chronyd 服务
systemctl  restart chronyd
  1. 把NTP服务加入防火墙
[root@wyf-test ~]# firewall-cmd  --permanent --add-service=ntp
[root@wyf-test ~]# firewall-cmd  --reload
[root@wyf-test ~]# firewall-cmd  --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens192
  sources:
  services: dhcpv6-client ntp ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
  1. 如何使用自建的NTP服务器
#1 安装 chrony服务,修改 chrony.conf 
...
# 把server 设置为已搭建的NTP服务器的IP
...
# 启动 chronyd 服务

#也可以随时手动同步时间
ntpdate IP(自己大劫案的NTP服务器的IP)

icmp-blocks:
rich rules:


4. 如何使用自建的NTP服务器

#1 安装 chrony服务,修改 chrony.conf

把server 设置为已搭建的NTP服务器的IP

启动 chronyd 服务

#也可以随时手动同步时间
ntpdate IP(自己大劫案的NTP服务器的IP)






  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux虚拟机时间同步可以通过以下几种方式实现: 1. 使用NTP服务同步时间:NTP(Network Time Protocol)是一种用于同步计算机时间的协议,可以通过配置NTP服务器来同步虚拟机时间。在Linux系统中,可以使用ntpdate命令手动同步时间,也可以通过配置ntp.conf文件来自动同步时间。 2. 使用VMware Tools同步时间:VMware Tools是一种虚拟机增强工具,可以提供虚拟机与宿主机之间的时间同步功能。在安装VMware Tools后,可以通过配置vmware-toolbox-cmd命令来启用时间同步功能。 3. 手动设置时间:如果以上两种方法无法实现时间同步,可以手动设置虚拟机时间。在Linux系统中,可以使用date命令手动设置时间。 总之,为了保证虚拟机时间的准确性,建议使用NTP服务或VMware Tools进行时间同步。 ### 回答2: Linux虚拟机时间同步是指在Linux虚拟机上进行时间同步的过程。在一些环境下,不同虚拟机之间的时间差异可能会产生一些问题,因此需要对Linux虚拟机时间进行同步。本文将介绍几种常见的时间同步方法。 1. NTP同步 NTP是网络时间协议,可用于在多个服务器之间同步时间。在Linux虚拟机上使用NTP同步时间需要先在虚拟机上安装ntp服务。在ubuntu系统上可以使用命令 sudo apt-get install ntp 安装。安装完成后可用命令 systemctl status ntp.service 查看服务是否已启动。如果未启动则使用命令 systemctl start ntp.service 启动即可。NTP同步主要使用的是UDP协议,通过和NTP服务器通信来同步时间。 2. VMware Tools时钟同步 VMware Tools是VMware公司开发的一款虚拟化软件,包括了一些虚拟机操作系统中的工具,其中包括时钟同步。使用VMware Tools时钟同步,需要先在虚拟机中安装VMware Tools。在安装完成后,在VMware虚拟机设置中启用时间同步功能即可,使得虚拟机能够自动同步主机的时间。VMware Tools时钟同步可以帮助保持虚拟机和主机时间同步。 3. 使用chrony同步 chrony是一个时间同步工具,能够在本地机器和远程服务器之间进行时间同步。使用chrony能够利用网络延迟来自动计算时钟偏移,并进行调整。使用chrony同步时间需要先在虚拟机中安装chrony服务,并通过配置文件指定要使用的外部NTP服务器地址。在Ubuntu系统上,可以使用命令 sudo apt-get install chrony 安装,并在编辑配置文件 /etc/chrony/chrony.conf 中设置server指令来配置NTP服务器地址。 总之,对于不同的应用场景,需要采用不同的时间同步方法。NTP同步是一种常用且全球通用的时间同步方法,VMware Tools时钟同步适用于在VMware虚拟机上进行时间同步。而chrony同步则采用类似于NTP的方式来进行时间同步。无论是哪种时间同步方法,都应根据实际需要进行选择。 ### 回答3: 在使用Linux虚拟机的过程中,时间同步是一个非常重要的功能,它可以确保虚拟机与物理主机之间的时间同步,从而避免出现一些奇怪的问题。 在Linux虚拟机中,时间同步可以通过两种方式实现:一是使用NTP服务进行时间同步;二是使用VMware Tools中的时间同步功能。 使用NTP服务进行时间同步,首先需要安装一个NTP服务。在Debian/Ubuntu发行版中,可以使用以下命令来安装NTP服务: sudo apt-get update sudo apt-get install ntp 在安装完NTP之后,需要进行一些配置。需要修改/etc/ntp.conf文件,如下所示: # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information. pool 0.ubuntu.pool.ntp.org iburst pool 1.ubuntu.pool.ntp.org iburst pool 2.ubuntu.pool.ntp.org iburst pool 3.ubuntu.pool.ntp.org iburst 在这里,我们使用的是Ubuntu的默认NTP服务器,当然也可以使用其他的NTP服务器。 修改完配置文件之后,需要重启NTP服务: sudo service ntp restart 这样,Linux虚拟机就可以使用NTP服务进行时间同步了。 另外一种方式是使用VMware Tools中的时间同步功能。这种方式比较简单,只需要在VMware Tools中选择“Synchronize guest time with host”选项即可。这样,在虚拟机和物理主机之间同步时间时,VMware会自动进行时间同步。 总的来说,无论使用哪种方法进行时间同步,都可以保证Linux虚拟机与物理主机之间时间同步,避免出现一些奇怪的问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值