树莓派NTP服务器搭建

3/7/2020 树莓派NTP

# 准备

  • 树莓派,已经配置好网络的
  • Xshell (鼠标键盘也行)

# 安装NTP服务器

sudo -i
apt-get install ntp
apt-get install ufw  #防火墙配置程序
1
2
3

# 配置NTP服务器(sudo下)

nano /etc/ntp.conf
1

然后照着下面的中文注释改:

 /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example
server ntp.ntsc.ac.cn  #这里写要同步的服务器地址

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

restrict 192.168.10.0 mask 255.255.255.0 nomodify 
# 上面的一行是限制访问,格式: 
# restrict <IP地址,请填写本网地址,并把最后一个byte改成0> mask <子网掩码> nomodify
#
# nomodify是不允许客户端更改,允许对时


# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust

......省略...........
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
55
56
57
58

保存退出。

然后执行:

sudo service ntp restart
ps -ef | grep ntp
1
2

看到下面的内容,就配置好了NTP服务器:

ntp        1066      1  0 18:10 ?        00:00:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 104:110
pi         1069   1034  0 18:10 pts/0    00:00:00 grep --color=auto ntp
1
2

# 配置防火墙(sudo下)

ufw enable
ufw deny
ufw allow 123/udp #开启UDP协议123端口
ufw allow 22/tcp  #开启ssh端口
1
2
3
4

然后你会看到:

Status: active

To                         Action      From
--                         ------      ----
123/udp                    ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
123/udp (v6)               ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)             
1
2
3
4
5
6
7
8

好了,现在重启系统(sudo reboot),就OK了!