Update from Sync Service
This commit is contained in:
@@ -1,50 +1,165 @@
|
||||
**NTP****简介:**
|
||||
NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。
|
||||
在计算机的世界里,时间非常地重要
|
||||
例如:对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,是按照A这台计算机的时间,还是按照B这台计算机的时间?
|
||||
NTP就是用来解决这个问题的,NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。
|
||||
它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。
|
||||
它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。
|
||||
# CentOS7 搭建 NTP 服务器
|
||||
|
||||
**环境:**
|
||||
## 系统:====CentOS Linux release 7.4.1708 (Core)
|
||||
## NTP Server**==服务器****IP****:**==192.168.0.15
|
||||
## NTP Client**==客户端****IP****:**==192.168.0.16
|
||||
> NTP (Network Time Protocol) 时间同步
|
||||
|
||||
**1****、搭建****NTP****服务器**
|
||||
**1.1****、查看服务器是否安装****ntp****,系统默认安装****ntpdate****;**
|
||||
[root@localhost ~]# rpm -qa |grep ntpntpdate-4.2.6p5-28.el7.centos.x86_64ntp-4.2.6p5-28.el7.centos.x86_64
|
||||
**1.2****、安装****ntp****,****ntpdate****已经系统默认安装过了**
|
||||
[root@localhost ~]# yum install -y ntp
|
||||
**1.3****、修改****ntp****配置文件**
|
||||
---
|
||||
|
||||
[](javascript:void\(0\);)
|
||||
## 简介
|
||||
|
||||
[root@localhost ~]# vim /etc/ntp.conf
|
||||
把配置文件下面四行注释掉:server 0.cn.pool.ntp.org iburstserver 1.cn.pool.ntp.org iburstserver 2.cn.pool.ntp.org iburstserver 3.cn.pool.ntp.org iburst然后在下面添加这几行:server 0.cn.pool.ntp.org iburstserver 1.cn.pool.ntp.org iburstserver 2.cn.pool.ntp.org iburstserver 3.cn.pool.ntp.org iburst
|
||||
NTP 用于同步网络中各个计算机的时间,精度在局域网可达 **0.1ms**。
|
||||
|
||||
[](javascript:void\(0\);)
|
||||
---
|
||||
|
||||
**1.4****、启动****ntp****服务,并开机自启动**
|
||||
[root@localhost ~]# systemctl start ntpd[root@localhost ~]# systemctl enable ntpd
|
||||
**1.5****、查询****ntp****是否同步**
|
||||
[root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter**=****=****=****=****=****=****=****=****=****=****=****=****=****=****=**===*119.28.206.193 100.122.36.196 2 u 128 128 377 19.711 -0.468 5.363
|
||||
**1.6****、开启防火墙****ntp****默认端口****udp123**
|
||||
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=123/udpsuccess[root@localhost ~]# firewall-cmd --reloadsuccess
|
||||
## 环境
|
||||
|
||||
**2****、****NTP****客户端配置**
|
||||
安装的NTP跟上面的步骤一样
|
||||
**2.1****、修改****ntp****配置文件,将上面的****NTP****服务器作为客户端同步****NTP****时间服务器**
|
||||
| 角色 | IP |
|
||||
|------|-----|
|
||||
| NTP Server | 192.168.0.15 |
|
||||
| NTP Client | 192.168.0.16 |
|
||||
|
||||
[](javascript:void\(0\);)
|
||||
---
|
||||
|
||||
[root@localhost ~]# vim /etc/ntp.conf#配置允许NTP Server时间服务器主动修改本机的时间restrict 192.168.0.15 nomodify notrap noquery#注释掉其他时间服务器#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#配置时间服务器为本地搭建的NTP Server服务器server 192.168.0.15
|
||||
## NTP 服务器配置
|
||||
|
||||
[](javascript:void\(0\);)
|
||||
### 1. 检查并安装 NTP
|
||||
|
||||
**2.2****、与****NTP server****服务器同步一下时间:**
|
||||
[root@localhost ~]# ntpdate -u 192.168.0.15
|
||||
**2.3****、查看****ntp****同步状态**
|
||||
能看到已经成功同步,要记得开启ntpd这个服务器
|
||||
[root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter**=****=****=****=****=****=****=****=****=****=****=****=****=****=****=**=== 192.168.0.15 119.28.206.193 3 u 7 64 1 0.217 -288085 0.000
|
||||
> 来自 <[https://www.cnblogs.com/Sungeek/p/10197345.html](https://www.cnblogs.com/Sungeek/p/10197345.html)>
|
||||
```bash
|
||||
# 查看是否已安装
|
||||
rpm -qa | grep ntp
|
||||
|
||||
# 安装 NTP
|
||||
yum install -y ntp
|
||||
```
|
||||
|
||||
### 2. 修改配置文件
|
||||
|
||||
```bash
|
||||
vim /etc/ntp.conf
|
||||
```
|
||||
|
||||
注释默认时间服务器,添加:
|
||||
```conf
|
||||
# 中国 NTP 服务器
|
||||
server 0.cn.pool.ntp.org iburst
|
||||
server 1.cn.pool.ntp.org iburst
|
||||
server 2.cn.pool.ntp.org iburst
|
||||
server 3.cn.pool.ntp.org iburst
|
||||
|
||||
# 允许上层时间服务器修改本机时间
|
||||
restrict 0.cn.pool.ntp.org nomodify notrap noquery
|
||||
restrict 1.cn.pool.ntp.org nomodify notrap noquery
|
||||
```
|
||||
|
||||
### 3. 启动 NTP 服务
|
||||
|
||||
```bash
|
||||
# 启动
|
||||
systemctl start ntpd
|
||||
|
||||
# 开机自启
|
||||
systemctl enable ntpd
|
||||
|
||||
# 查看状态
|
||||
systemctl status ntpd
|
||||
```
|
||||
|
||||
### 4. 查看同步状态
|
||||
|
||||
```bash
|
||||
ntpq -p
|
||||
```
|
||||
|
||||
输出示例:
|
||||
```
|
||||
remote refid st t when poll reach delay offset jitter
|
||||
==============================================================================
|
||||
*119.28.206.193 100.122.36.196 2 u 128 128 377 19.711 -0.468 5.363
|
||||
```
|
||||
|
||||
> `*` 表示已成功同步
|
||||
|
||||
### 5. 开放防火墙端口
|
||||
|
||||
```bash
|
||||
firewall-cmd --permanent --zone=public --add-port=123/udp
|
||||
firewall-cmd --reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## NTP 客户端配置
|
||||
|
||||
### 1. 安装 NTP
|
||||
|
||||
```bash
|
||||
yum install -y ntp
|
||||
```
|
||||
|
||||
### 2. 修改配置文件
|
||||
|
||||
```bash
|
||||
vim /etc/ntp.conf
|
||||
```
|
||||
|
||||
添加 NTP 服务器地址:
|
||||
```conf
|
||||
# 允许 NTP Server 主动修改本机时间
|
||||
restrict 192.168.0.15 nomodify notrap noquery
|
||||
|
||||
# 时间服务器
|
||||
server 192.168.0.15
|
||||
```
|
||||
|
||||
### 3. 立即同步时间
|
||||
|
||||
```bash
|
||||
ntpdate -u 192.168.0.15
|
||||
```
|
||||
|
||||
### 4. 启动 NTP 服务
|
||||
|
||||
```bash
|
||||
systemctl start ntpd
|
||||
systemctl enable ntpd
|
||||
```
|
||||
|
||||
### 5. 查看同步状态
|
||||
|
||||
```bash
|
||||
ntpq -p
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 常用命令
|
||||
|
||||
```bash
|
||||
# 手动同步(客户端)
|
||||
ntpdate -u 192.168.0.15
|
||||
|
||||
# 查看 NTP 服务器列表
|
||||
ntpq -p
|
||||
|
||||
# 查看时间
|
||||
date
|
||||
|
||||
# 查看硬件时间
|
||||
hwclock
|
||||
|
||||
# 同步硬件时间
|
||||
hwclock --systohc
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 中国 NTP 服务器
|
||||
|
||||
| 服务器 | 地址 |
|
||||
|--------|------|
|
||||
| 中国教育网 | 1.cn.pool.ntp.org |
|
||||
| 阿里云 | ntp.aliyun.com |
|
||||
| 腾讯云 | time1.cloud.tencent.com |
|
||||
|
||||
---
|
||||
|
||||
> 参考:[CentOS7 NTP 搭建](https://www.cnblogs.com/Sungeek/p/10197345.html)
|
||||
|
||||
Reference in New Issue
Block a user