linux NTP时间同步

linux NTP

timesyncd 服务

默认情况下 timesyncd 会定期检测并同步时间

它还会在本地存储更新的时间,以便在系统重启时做时间单步调整。

如果是虚拟机环境,应该把与主机时间同步功能关闭后在启用systemd-timesyncd

systemd-timesyncd只能作为客户端 , 不能作为NTP服务器

查看是否NTP同步:

timedatectl

在这里插入图片描述

图中显示NTP(System clock synchronized: no)没有同步。

NTP没有同步,我们可以查看一下timesyncd的状态

systemctl status systemd-timesyncd.service
//图中显示的Status为“Idle.”,是没有进行时钟同步的。

在这里插入图片描述

通过设置net命令:timedatectl set-ntp true去设置但是没有成功,如下图显示:

在这里插入图片描述

systemd-timesyncd 启动时会读取 /etc/systemd/timesyncd.conf 配置文件 :

sudo vi /etc/systemd/timesyncd.conf

我们可以修改成如下,只需要修改NTP=这一行:

[Time]
NTP=192.168.7.134
#FallbackNTP=ntp.ubuntu.com   #备用NTP服务器
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

这样看,只需设置一个NTP就可以了,因为默认有备用NTP服务器。

linux NTP强制更新

sudo vi /etc/systemd/timesyncd.conf


//强制更新可以在配置好时间同步后再重启时间同步服务,让他自动同步。在restart十秒之后会去同步备用NTP服务器
sudo systemctl restart systemd-timesyncd

//查看
sudo journalctl -u systemd-timesyncd
  • 虽然NTP设置成功了,也没有出现time out。说面ntp服务器是奏效的。但是本地时间却没有变,这是为什么?
执行timedatectrl查看是否同步成功的时候显示no
	System clock synchronized: no
用wireshark抓包的时候出现:
	Leap Indicator unknown (clock unsynchronized)

​ 经过google百度加上实验,找到了原因:这是ntp服务器的问题,需要把ntp服务器设置正确,经测试下面的软件和配置可用。

  1. windows NTP服务器下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm#ntp_stable
  • 实验目的:了解timesyncd

当NTP服务器和timesyncd一切都ok了(见‘NTP服务器’章节),我们再测试一下时间同步功能。

前提: 当你的ntp服务器没有问题,你的timesyncd也起来了。

操作:

  1. 打开wireshark抓包。输入ntp进行过滤。
  2. 在linux设备上用sudo date -s '2020-12-12 12:12:12'去改变时间
  3. 查看时间timedatectrl

现象:我们发现不管我们怎么设置时间,时间都不是你设置的那个时间。只要使用data -s去设置,在wireshark上都会马上抓到NTP的网络数据。

原因: 当你改变时间的时候,timesyncd会检测到时间的变化,然后马上又进行同步。

NTP服务器

NTP服务器可以linux上的,也可以是windows上的。linux的NTP服务器这里不讲述,因为网上太多这方面的资料。这里介绍一下windows上的NTP服务器,其实是更加简单的,可以使用ntp服务器默认配置。

  1. windows NTP服务器下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm#ntp_stable
  2. ntp配置
# NTP Network Time Protocol 
# **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes* 
# PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED 
# Configuration File created by Windows Binary Distribution Installer Rev.: 1.27  mbg
# please check http://www.ntp.org for additional documentation and background information
# restrict access to avoid abuse of NTP for traffic amplification attacks 
# see http://news.meinberg.de/244 for details  
restrict default nomodify
 
# allow status queries and everything else from localhost 
restrict 127.0.0.1 
restrict ::1 
 
# if you need to allow access from a remote host, you can add lines like this: 
# restrict <IP OF REMOTE HOST> 
restrict 192.168.7.0 mask 255.255.255.0 nomodify

# Use drift file 
driftfile "C:\Program Files (x86)\NTP\etc\ntp.drift"

# your local system clock, could be used as a backup
# (this is only useful if you need to distribute time no matter how good or bad it is)
server 127.127.1.0
# but it should operate at a high stratum level to let the clients know and force them to
# use any other timesource they may have.
fudge 127.127.1.0 stratum 8

# Use specific NTP servers
# server ntp.aliyun.com iburst minpoll 18000 maxpoll 36000

# End of generated ntp.conf --- Please edit this to suite your needs

设置系统时间

//timedatectl set-time '2020-11-20 11:11:11' 需要timedatectl set-ntp no才能设置。

sudo date -s '2020-11-11 11:11:11'

sudo date -s '2020-12-12 12:12:12'

sudo reboot

timedatectl

sudo date -s '2020-12-12 12:12:12'

sudo hwclock --systohc --utc  //或者sudo hwclock -w -u
sudo hwclock --systohc --localtime  //或者sudo hwclock -w -l

timedatectltime

尽量不要将set-local-rtc设置成yes状态。

上面是测试的过程,通过测试发现了一个较好的设置时间的方案:

# 1. 设置时间
sudo date -s '2020-12-12 12:12:12'

# 2. 把时间更新到RTC
sudo hwclock --systohc --utc  //或者sudo hwclock -w -u

这样可以在下次reboot后,将RTC中的时间读出来,设置成系统时间。而RTC的作用是即使设备很长时间不通电也可以依靠电池或电容让RTC中的时间继续跑。

设置网络

# /lib/systemd/network/10-eth0.network
[Match]
Name=eth0
[Network]
Address=192.168.7.70/16
Gateway=192.168.7.1
[Link]
MACAddress=00:00:9f:05:93:99


# ip
cat 10-eth0.network | sed -e '/^Address=/d;/^\[Network\]/a\Address=11.11.11.11'
	sed -i '/^Address=/d;/^\[Network\]/a\Address=11.11.11.11'


#Gateway
cat 10-eth0.network | sed -e '/^Gateway=/d;/^Address/a\Gateway=22.22.22.22'


# mac
cat 10-eth0.network | sed -e '/^MACAddress=/d;/^\[Link\]/a\MACAddress=00:00:9f:05:93:88'

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值