NTP时间同步服务安装指南(Linux)

本文最后更新于:3 个月前

Linux系统每次启动时间都不正确,若每次手动修改则很不便,安装NTP时间同步服务即可自动同步时间。

Linux安装NTP时间同步服务

安装命令:

1
yum -y install ntp

启动NTP服务:

1
2
systemctl start ntpd
systemctl enable ntpd

查看NTP服务运行状态:

1
systemctl status ntpd

修改配置文件:

我这里没有安装vim,所以用vivim也一样只是换个名。

1
vi /etc/ntp.conf

我这里有个问题,找不到配置文件,但是用文件查找命令发现就在那里,也许是被隐藏了吧。
ntp配置文件查找命令:

1
sudo find / -name ntp.conf

修改内容:新增阿里云时间服务器

1
server ntp.aliyun.com iburst

查看配置文件中设置的NTP授时服务器

1
grep server /etc/ntp.conf 

终端显示:

1
2
3
4
5
6
7
8
9
# Use public servers from the pool.ntp.org project.
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 ntp.aliyun.com iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcast 224.0.1.1 autokey # multicast server
#manycastserver 239.255.254.254 # manycast server

重启NTP服务:

1
systemctl restart ntpd

查看同步状态:

1
ntpq -p

这样再次重启,时间也不会乱了。


详细安装信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@localhost ~]# yum -y install ntp
已加载插件:fastestmirror
Determining fastest mirrors
* base: ftp.sjtu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
updates/7/x86_64/primary_db | 23 MB 00:00:02
软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2023-10-02 09:06:57 CST; 29s ago
Main PID: 2705 (ntpd)
CGroup: /system.slice/ntpd.service
└─2705 /usr/sbin/ntpd -u ntp:ntp -g

10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listen and drop on 1 v6wildcard :: UDP 123
10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listen normally on 2 lo 127.0.0.1 UDP 123
10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listen normally on 3 ens33 192.168.175.131 UDP 123
10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listen normally on 4 lo ::1 UDP 123
10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listen normally on 5 ens33 fe80::62:fd49:3a6c:994c UDP 123
10月 02 09:06:57 localhost.localdomain ntpd[2705]: Listening on routing socket on fd #22 for interface updates
10月 02 09:06:57 localhost.localdomain ntpd[2705]: 0.0.0.0 c016 06 restart
10月 02 09:06:57 localhost.localdomain ntpd[2705]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
10月 02 09:06:57 localhost.localdomain ntpd[2705]: 0.0.0.0 c011 01 freq_not_set
10月 02 09:07:04 localhost.localdomain ntpd[2705]: 0.0.0.0 c614 04 freq_mode
[root@localhost ~]#
[root@localhost ~]# vi /etc/ntp.conf
[root@localhost ~]# grep server /etc/ntp.conf
# Use public servers from the pool.ntp.org project.
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 ntp.aliyun.com iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcast 224.0.1.1 autokey # multicast server
#manycastserver 239.255.254.254 # manycast server
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp8.flashdance 194.58.202.20 2 u 1 64 1 350.605 69.011 11.779
111.230.189.174 100.122.36.196 2 u 2 64 1 64.256 38.951 7.296
a.chl.la 131.188.3.222 2 u 1 64 1 215.988 15.178 12.127
203.107.6.88 100.107.25.114 2 u - 64 1 26.506 39.602 7.992
[root@localhost ~]#

NTP时间同步服务安装指南(Linux)
https://superlovelace.top/2023/10/02/NTP/
作者
棱境
发布于
2023年10月2日
更新于
2023年11月7日
许可协议