Linux【工具 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)

未安装则使用命令进行安装

yum install ntp

启动服务

systemctl start ntpd

开机自启动

systemctl enable ntpd

查看ntp服务器状态

[root@aliyun ~]# ntpstat
synchronised to NTP server (100.100.61.88) at stratum 2
time correct to within 14 ms
polling server every 1024 s


### 2.2 服务器端配置



修改ntp配置文件

vim /etc/ntp.conf


`ntp.conf`修改前(已去掉注释信息):



driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

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

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor


`ntp.conf`修改后:



driftfile /var/lib/ntp/drift

1.default表示对所有的计算机进行控制

restrict default nomodify notrap nopeer noquery

局域网可放行可以指定网段或列出单独IP

restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify

restrict xxx.xxx.xxx.x nomodify

1.end

restrict 127.0.0.1
restrict ::1

2.注释掉公网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

2.end

3.开启自身同步

server 127.127.1.0
Fudge 127.127.1.0 stratum 8

3.end

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor


### 2.3 服务器端测试



重启服务

systemctl restart ntpd

测试

[root@aliyun ~]# ntpstat
synchronised to local net (127.127.1.0) at stratum 6
time correct to within 949 ms
polling server every 64 s


## 3.Linux客户端配置


### 3.1 安装


安装过程跟服务器端一致,这里不再赘述。


### 3.2 客户端配置


`ntp.conf`修改前不再赘述,修改后:



driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

1.注释掉公网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

1.end

2.开启服务器同步

server aliyun
Fudge aliyun stratum 8

2.end

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor


### 3.3 客户端测试



查看NTP服务是否开启

[root@tcloud ~]# timedatectl
Local time: Fri 2022-03-25 15:29:25 CST
Universal time: Fri 2022-03-25 07:29:25 UTC
RTC time: Fri 2022-03-25 07:29:24
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

开启NTP服务

[root@tcloud ~]# timedatectl set-ntp yes
[root@tcloud ~]# timedatectl
Local time: Fri 2022-03-25 15:52:30 CST
Universal time: Fri 2022-03-25 07:52:30 UTC
RTC time: Fri 2022-03-25 07:52:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a



重启服务

systemctl restart ntpd

测试

[root@tcloud ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s

配置后一般要等待几分钟才能与/etc/ntp.conf中的服务器端完成同步

[root@tcloud ~]# ntpstat
synchronised to NTP server (101.201.69.99) at stratum 7
time correct to within 968 ms
polling server every 64 s


### 3.4 另一种方式


使用`ntpdate`命令结合定时任务实现时间同步。此时要停止`ntpd`服务器,测试`ntpdate aliyun`报错,查看报错信息:



[root@tcloud ~]# ntpdate -d aliyun
28 Mar 15:33:22 ntpdate[17145]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host aliyun and service ntp
host found : aliyun
transmit(101.201.69.99)
101.201.69.99: Server dropped: no data
server 101.201.69.99, port 123
28 Mar 15:33:30 ntpdate[17145]: no server suitable for synchronization found


解决:关闭防火墙,配置`aliyun`服务器的安全组放开端口`123`。



端口开放后

[root@tcloud ~]# ntpdate aliyun
28 Mar 16:06:57 ntpdate[2330]: adjust time server 101.201.69.99 offset 0.000112 sec



添加定时任务

vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

/etc/crontab 内容如下

For details see man 4 crontabs

Example of job definition:

.---------------- minute (0 - 59)

| .------------- hour (0 - 23)

| | .---------- day of month (1 - 31)

| | | .------- month (1 - 12) OR jan,feb,mar,apr …

| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

| | | | |

* * * * * user-name command to be executed

每分钟同步一次

*/1 * * * * root ntpdate aliyun


## 4.Windows客户端配置


### 4.1 配置并启用客户端


`Win键+r`输入`gpedit.msc`打开本地组策略编辑器,找到`配置 Windows NTP 客户端`(计算间配置-管理模板-系统-Windows 时间服务-时间提供程序)进行客户端配置:


![在这里插入图片描述](https://img-blog.csdnimg.cn/33a4eddcca1144139b311f3850cf4d86.png#pic_center)


配置页面:


![在这里插入图片描述](https://img-blog.csdnimg.cn/4cc63eea37fa4da8b198a40fb580919e.png#pic_center)


启用客户端配置:


![在这里插入图片描述](https://img-blog.csdnimg.cn/596d8588882a44d9a24b0ceb7e80a09a.png#pic_center)


### 4.2 使用客户端配置


![在这里插入图片描述](https://img-blog.csdnimg.cn/94dd2b447cc045aaac71af2c4d822eb8.png#pic_center)


## 5.ntp服务其他参数配置


### 5.1 查看ntp服务器有无和上层ntp连通



[root@aliyun ~]# ntpstat
synchronised to NTP server (139.199.214.202) at stratum 3
time correct to within 1010 ms
polling server every 64 s


### 5.2 查看ntp服务器与上层ntp的状态



[root@aliyun ~]# ntpq -p
remote refid st t when poll reach delay offset jitter

+skitty.itu.ch 84.16.67.12 2 u 49 64 17 174.891 20.582 5.712
+139.199.214.202 100.122.36.196 2 u 47 64 37 46.355 43.764 9.636
-time.cloudflare 10.28.12.207 3 u 49 64 37 216.628 31.101 12.642
*tick.ntp.infoma .GPS. 1 u 49 64 37 156.665 12.054 11.269

或者使用以下命令查看实时状态

[root@aliyun ~]# watch “ntpq -p”


* remote:列出源的 IP 地址或主机名。第一列中的字符指示源的质量。星号 ( \* ) 表示该源是当前引用。
* refid:参考上一层ntp主机地址。
* st:stratum阶层。
* when:多少秒前曾经同步过时间。指出从轮询源开始已过去的时间(秒)。
* poll:下次更新在多少秒后。指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
* reach:已经向上层ntp服务器要求更新的次数。是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
* delay:网络延迟。
* offset:时间补偿。是源时钟与本地时钟的时间差(毫秒)。
* jitter:系统时间与bios时间差。


### 5.3 从其他博客收集的信息


#### 5.3.1 /etc/ntp.conf 配置内容



driftfile /var/lib/ntp/drift

1. 权限设置 包括放行上层服务器以及开放局域网用户来源

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

放行局域网用户来源 或者列出单独IP

restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify

2. 设定主机来源 请先将原本的 [0|1|2|3].centos.pool.ntp.org 注掉

#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

以这部主机为最优先的server

server xxx.xxx.xxx.xx prefer

3. 默认的一个内部时钟数据 用在没有外部NTP服务器时 使用它为局域网用户提供服务

为了做好运维面试路上的助攻手,特整理了上百道 【运维技术栈面试题集锦】 ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。

本份面试集锦涵盖了

  • 174 道运维工程师面试题
  • 128道k8s面试题
  • 108道shell脚本面试题
  • 200道Linux面试题
  • 51道docker面试题
  • 35道Jenkis面试题
  • 78道MongoDB面试题
  • 17道ansible面试题
  • 60道dubbo面试题
  • 53道kafka面试
  • 18道mysql面试题
  • 40道nginx面试题
  • 77道redis面试题
  • 28道zookeeper

总计 1000+ 道面试题, 内容 又全含金量又高

  • 174道运维工程师面试题

1、什么是运维?

2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

3、现在给你三百台服务器,你怎么对他们进行管理?

4、简述raid0 raid1raid5二种工作模式的工作原理及特点

5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

7、Tomcat和Resin有什么区别,工作中你怎么选择?

8、什么是中间件?什么是jdk?

9、讲述一下Tomcat8005、8009、8080三个端口的含义?

10、什么叫CDN?

11、什么叫网站灰度发布?

12、简述DNS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
NS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中,使用NTP服务来完成时间同步的过程非常简单。NTP(Network Time Protocol)是一种用于网络中时间同步的协议。它能够通过计算网络延迟,并校正系统时间来保证各个机器之间的时间一致性。 在Linux系统中,可以使用ntpdate或者chronyd命令来进行时间同步。其中ntpdate命令是最常见和简单的时间同步命令,但是它在最新版本的Linux系统中被废弃了。而chronyd命令是一个比ntpdate更为先进的时间同步软件,它支持动态校正系统时间,并且可以在网络断开连接的时候保存系统时间的误差值,以便在网络重新连接之后进行快速校正。 在使用NTP同步时间之前,需要确保系统已经安装了ntp或者chrony软件包。一旦安装完成之后,就可以打开ntp或chrony配置文件进行基本设置。在这个配置文件中,需要指定一个或多个NTP服务器地址,即NTP服务器的IP地址或域名。这些服务器将为客户端提供高质量的时间同步服务。可以在配置文件中使用server或pool来指定NTP服务器地址,例如: server ntp.server.com pool pool.ntp.org 在指定了NTP服务器地址之后,接着需要启动NTP服务。可以使用ntpdate命令来进行时间同步,例如: ntpdate ntp.server.com 或者使用chronyd来启动NTP服务,例如: systemctl start chronyd 以上是Linux客户端使用NTP服务进行时间同步的基本过程。使用NTP服务来完成时间同步可以有效避免不同计算机之间的时间误差,从而保证网络上各个计算机进行数据传输时的时间一致性,提高整个网络体系的稳定性和安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值