166 lines
2.4 KiB
Markdown
Executable File
166 lines
2.4 KiB
Markdown
Executable File
# CentOS7 搭建 NTP 服务器
|
|
|
|
> NTP (Network Time Protocol) 时间同步
|
|
|
|
---
|
|
|
|
## 简介
|
|
|
|
NTP 用于同步网络中各个计算机的时间,精度在局域网可达 **0.1ms**。
|
|
|
|
---
|
|
|
|
## 环境
|
|
|
|
| 角色 | IP |
|
|
|------|-----|
|
|
| NTP Server | 192.168.0.15 |
|
|
| NTP Client | 192.168.0.16 |
|
|
|
|
---
|
|
|
|
## NTP 服务器配置
|
|
|
|
### 1. 检查并安装 NTP
|
|
|
|
```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)
|