一.在centos7是安装ntp服务
1.安装ntp
[root@MiWiFi-RA80-srv ~]# yum -y install ntp
2.安装vim
[root@MiWiFi-RA80-srv ~]# yum -y install vim
3.打开配置文件
[root@MiWiFi-RA80-srv ~]# vim /etc/ntp.conf
[root@MiWiFi-RA80-srv ~]# ip a
把以下内容添加到配置文件
# by lcds create ntpd.log
logfile /var/log/ntpd.log
# by lcds create ntpd.pid
pidfile /var/run/ntpd.pid
server times.aliyun.com iburst prefer
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst
4.加载.启动服务及查看服务状态
[root@MiWiFi-RA80-srv ~]# systemctl daemon-reload
[root@MiWiFi-RA80-srv ~]# systemctl restart ntpd.service
[root@MiWiFi-RA80-srv ~]# systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2023-05-31 21:30:40 EDT; 22s ago
Process: 13277 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 13280 (ntpd)
Tasks: 1
Memory: 620.0K
CGroup: /system.slice/ntpd.service
└─13280 /usr/sbin/ntpd -u ntp:ntp -g
5.设置开机自启服务
[root@MiWiFi-RA80-srv ~]# systemctl stop ntpd.service
[root@MiWiFi-RA80-srv ~]# systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@MiWiFi-RA80-srv ~]# ll /var/log/ | grep ntpd.log
-rw-r--r--. 1 root root 1124 5月 31 21:25 ntpd.log
[root@MiWiFi-RA80-srv ~]# ll /var/run/ | grep ntpd.pid
-rw-r--r--. 1 root root 4 5月 31 07:55 ntpd.pid
[root@MiWiFi-RA80-srv ~]# systemctl start ntpd.service
6.查看主机名,修改主机,重启
[root@MiWiFi-RA80-srv ~]# hostname
MiWiFi-RA80-srv
[root@MiWiFi-RA80-srv ~]# hostnamectl set-hostname lcds
[root@MiWiFi-RA80-srv ~]# reboot
7.重启以后查看主机名
[root@lcds ~]# hostname
lcds
[root@lcds ~]# hostnamectl
Static hostname: lcds
Icon name: computer-vm
Chassis: vm
Machine ID: b2f0933b8a380947aea51385d413e093
Boot ID: df7f96b593334507867914e1d71159b6
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.90.1.el7.x86_64
Architecture: x86-64
[root@lcds ~]#
8.查看ntp同步服务器
[root@lcds ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*120.25.115.20 10.137.53.7 2 u 40 64 1 34.122 0.091 0.885
+203.107.6.88 100.107.25.114 2 u 39 64 1 18.603 -1.523 4.855
+tock.ntp.infoma .GPS. 1 u 38 64 1 224.153 -0.158 1.657
[root@lcds ~]#
9. 查看机器是所有防火墙规则
[root@MiWiFi-RA80-srv ~]# iptables -L
10.查看firewalld命令ntp添加防火墙规则
[root@MiWiFi-RA80-srv ~]# iptables -L | grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW,UNTRACKED
11.添加防火墙服务及查看所有服务
1.添加防火墙服务
[root@MiWiFi-RA80-srv ~]# firewall-cmd --add-service=ntp
success
2.查看所有防火墙服务器
[root@MiWiFi-RA80-srv ~]# firewall-cmd --list-all
12.查看同步服务器状况
[root@lcds ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
120.25.115.20 10.137.53.7 2 u - 64 1 34.061 -1.496 0.271
203.107.6.88 100.107.25.114 2 u 1 64 1 20.368 -4.524 0.236
time.cloudflare 10.211.8.4 3 u - 64 1 160.740 -17.446 2.538
[root@lcds ~]#
在ubuntu上安装ntp服务,准备另一台ntp服务器做客户端
1.查看ubuntu版本信息
longchi18@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
longchi18@ubuntu:~$
2.安装NTP
longchi18@ubuntu:~$ sudo apt install ntp -y
3.启动NTP服务
longchi18@ubuntu:~$ vim /etc/ntp.conf
longchi18@ubuntu:~$ sudo systemctl start ntp.service
[sudo] password for longchi18:
longchi18@ubuntu:~$ sudo systemctl status ntp.service
● ntp.service - Network Time Service
Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-06-01 02:31:15 UTC; 16min ago
Docs: man:ntpd(8)
Process: 3276 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
Main PID: 3282 (ntpd)
Tasks: 2 (limit: 2214)
Memory: 1.4M
CPU: 250ms
CGroup: /system.slice/ntp.service
└─3282 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 114:119
Jun 01 02:31:19 ubuntu ntpd[3282]: Soliciting pool server 108.59.2.24
Jun 01 02:31:19 ubuntu ntpd[3282]: Soliciting pool server 202.118.1.81
Jun 01 02:31:20 ubuntu ntpd[3282]: Soliciting pool server 119.28.206.193
Jun 01 02:31:20 ubuntu ntpd[3282]: Soliciting pool server 36.110.233.85
4.查看时间信息
longchi18@ubuntu:~$ timedatectl
Local time: Thu 2023-06-01 02:52:50 UTC
Universal time: Thu 2023-06-01 02:52:50 UTC
RTC time: Thu 2023-06-01 02:52:50
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: n/a
RTC in local TZ: no
longchi18@ubuntu:~$
5.安装防火墙
longchi18@ubuntu:~$ sudo apt install firewalld
6.查看所有防火墙服务状况
longchi18@ubuntu:~$ sudo firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
longchi18@ubuntu:~$
7.添加防火墙服务策略ntp
longchi18@ubuntu:~$ sudo firewall-cmd --add-service=ntp
success
8.查看ntp的服务
1.查看所有防火墙规则
longchi18@ubuntu:~$ sudo iptables -L
2.查看ntp服务
longchi18@ubuntu:~$ sudo iptables -L | grep ntp
9.查看centos上的时间信息
1.查看centos上的ntp服务
[root@lcds ~]# iptables -L | grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW,UNTRACKED
2.查看centos上的同步服务器
[root@lcds ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*120.25.115.20 10.137.53.7 2 u 76 128 377 34.811 0.816 2.146
+203.107.6.88 100.107.25.114 2 u 28 128 377 19.545 -2.851 4.090
+tock.ntp.infoma .GPS. 1 u 70 128 377 201.138 -13.698 9.700
[root@lcds ~]#
3.查看当前centos机器的net-tools包所在位置
[root@lcds ~]# yum provides */netstat
4.安装net-tools包
[root@lcds ~]# yum install net-tools -y
5 查看ntp服务端口
[root@lcds ~]# netstat -ulntp | grep ntp
udp 0 0 172.17.0.1:123 0.0.0.0:* 29896/ntpd
udp 0 0 192.168.31.181:123 0.0.0.0:* 29896/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 29896/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 29896/ntpd
udp6 0 0 fe80::7cb2:bcc8:169:123 :::* 29896/ntpd
udp6 0 0 ::1:123 :::* 29896/ntpd
udp6 0 0 :::123 :::* 29896/ntpd
[root@lcds ~]#
6.查看端口占用情况
[root@lcds ~]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.31.181:22 192.168.31.44:65408 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
[root@lcds ~]#
7.查看指定端口
[root@lcds ~]# netstat -ant | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 52 192.168.31.181:22 192.168.31.44:65408 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
[root@lcds ~]#
8.查看centos的时间信息
[root@lcds ~]# timedatectl
Local time: 四 2023-06-01 03:31:24 EDT
Universal time: 四 2023-06-01 07:31:24 UTC
RTC time: 四 2023-06-01 07:31:23
Time zone: America/New_York (EDT, -0400)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
日 2023-03-12 01:59:59 EST
日 2023-03-12 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
日 2023-11-05 01:59:59 EDT
日 2023-11-05 01:00:00 EST
[root@lcds ~]#
10 在centos上添加ntp服务
[root@lcds ~]# firewall-cmd --add-service=ntp
success
[root@lcds ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: dhcpv6-client ntp ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
11.在ubuntu上设置与centos时间同步
1.设置ubuntu的时间为2002-02-02
longchi18@ubuntu:~$ timedatectl set-time '2002-02-02'
2.查看ubuntu上的时间信息
longchi18@ubuntu:~$ timedatectl
Local time: Sat 2002-02-02 00:00:15 UTC
Universal time: Sat 2002-02-02 00:00:15 UTC
RTC time: Sat 2002-02-02 00:00:15
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: n/a
RTC in local TZ: no
3.设置ubuntuj机器上的时间与centos(192.168.11.181)机器上时间同步
longchi18@ubuntu:~$ ntpdate -u 192.168.11.181
4.在ubuntu上安装ntpdate和ntpsec-ntpdate
sudo apt install ntpdate # version 1:4.2.8p15+dfsg-1ubuntu2, or
sudo apt install ntpsec-ntpdate # version 1.2.1+dfsg1-4
5.添加防火墙ntp服务
longchi18@ubuntu:~$ sudo firewall-cmd --add-service=ntp
success
6.在ubuntu上再次查看时间信息
再次查看ubuntu上的时间信息,此时已经跟centos机器上时间一致
1.设置ubuntu与centos机器上时间同步
longchi18@ubuntu:~$ sudo ntpdate -u 192.168.11.181
{"time":"2023-06-01T08:01:48.396843+0000","offset":672997269.066128,"precision":0.000302,"host":"192.168.11.181","ip":"192.168.11.181","stratum":3,"leap":"no-leap","adjusted":true}
CLOCK: time stepped by 672997269.066128
CLOCK: time changed from 2002-02-02 to 2023-06-01
2.查看同步以后的时间已经由‘2002-02-02’变成‘2023-06-01’
longchi18@ubuntu:~$ timedatectl
Local time: Thu 2023-06-01 08:02:19 UTC
Universal time: Thu 2023-06-01 08:02:19 UTC
RTC time: Sat 2002-02-02 00:21:10
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: n/a
RTC in local TZ: no
longchi18@ubuntu:~$
ntp同步时间服务就差不多了。