个人网站iptables的几条简单设置

iptables是Linux操作系统中常用的防火墙,在一些发行版如CentOS中内置,虽然功能强大,操作也很方便,但需要用户对于防火墙和主机的使用环境有相当的了解,因而在实际应用中往往并不容易配置的好。对于个人网站来说,采用更复杂的防火墙系统似无必要,且由于网站架构并不复杂,因此化简为宜列出几条适配的规则很有必要。以下内容基于CentOS 6.9版本,以个人网站(静态网页和博客型为主)为例。

先介绍一下iptables的几个基本知识。

  • iptables包括三个链区域,分别是INPUT、FORWARD和OUTPUT,INPUT是指进入主机的数据流向,FORWARD是指“路过”主机的数据流向,OUTPUT是指从主机出去的数据流向。通常防火墙主要限制的是INPUT这一环,FORWARD和OUTPUT这两个环节基本可以不管[1]
  • iptables采用三大策略,分别是ACCEPT、REJECT和DROP,ACCEPT是指放行数据,REJECT是指拒绝数据通过,DROP是指丢弃数据包。通常采用ACCEPT和DROP这两种策略就可以了。
  • iptables的几个重要参数[2]
    • -A 在尾部添加新规则
    • -I 在已有规则表中插入新规则,若不增加行数,默认在首行插入
    • -L 列出当前生效的规则
    • -D 删除当前列表中的规则
    • -F 清空当前规则表,一般可与-Z、-X连用
    • -p 网络协议参数,如tcp、udp等,大多数情况下tcp协议就够了
    • -j 指代规则的适用目标
    • –line-numbers 列出每条规则的行号,一般在插入规则和删除规则时用到
    • –dport 目标端口,在INPUT块中以这个参数来制定规则相对容易,因为很多规则是基于端口的,如网页访问通常是走80或443端口,SSH登录是走22端口,收取电子邮件一般是25端口。

一般的个人网站所用到网络服务有80端口和443端口的浏览器访问、20和21端口的FTP文件上传下载、22端口的SSH远程主机登录、3306端口的MySQL数据库查询及操作服务。以下的操作步骤就是打开这几个常用端口,并把其它端口的数据流入封锁住以保障服务器安全。

以root用户[3]SSH方式登录入服务器,输入iptables -L命令以查看当前iptables的规则,三个链默认都是采用的放行策略,且各块均空,没有事先建立规则。

1
2
3
4
5
6
7
8
9
[root@host ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

在打开服务器内外部数据流动的关口、建立有效规则之前,先放行服务器内部的数据流动,比如主机内部程序之间的通讯,输入以下命令在INPUT链增加一条放行内部数据通行的规则:

1
iptables -A INPUT -i lo -j ACCEPT

生成类似如下的结果:

1
2
3
4
5
6
7
8
9
Chain INPUT (policy ACCEPT)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT)
pkts bytes target prot opt in out source destination

接着,输入以下命令到INPUT区域以放行几种常用的协议,分别为SSH、网页浏览、安全网页浏览、FTP文件传输(分别需要20和21两个端口,有关这两个端口的介绍,请查阅Bradley Mitchell的文章Learn the Purpose of the TCP Port 21 and How it Works With FTP)。

1
2
3
4
5
iptables -A INPUT -p tcp --dport 22 -j ACCEPT    # SSH远程主机连接
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 网页浏览
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 安全网页浏览
iptables -A INPUT -p tcp --dport 20 -j ACCEPT # FTP文件传输数据通讯控制端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT # FTP文件传输数据传输端口

上述几条命令也可集成在以下这样一条命令中:

1
iptables -A INPUT -p tcp -m multiport --dports 22,80,443,20,21 -j ACCEPT

生成的结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:20
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

为INPUT区域增加的最后一条规则应当是丢弃掉符合以上规则以外的所有数据包,执行iptables -A INPUT -j DROP得到以下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:20
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
DROP all -- 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

至此基本能够保证只有必要的数据可以进入服务器,在一定程度上保障了服务器的安全。

在网站建好、相关文件已经通过FTP上传之后,有些个人网站也许已经不再需要使用FTP了,这时可以通过以下三条命令将FTP协议部分的规则删除。第一条命令加上了--line-numbers参数以显示各条规则对应的行数数值。

1
iptables -nvL --line-numbers

执行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Chain INPUT (policy ACCEPT)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:20
6 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
7 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT)
num pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT)
num pkts bytes target prot opt in out source destination

这样便可以通过行数数值对应到具体的规则来实现简单的删除操作。执行第二条命令两次[4]

1
iptables -D INPUT 5

生成如下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
Chain INPUT (policy ACCEPT)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
5 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT)
num pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT)
num pkts bytes target prot opt in out source destination

当然,iptables还有更严格的防火墙实践,比如只允许SSH登录和操作而将其它数据包尽数丢弃的策略和规则[5]


  1. FORWARD和OUTPUT也很重要,但对个人小网站来说,如果服务器已经被从内部攻破,比如植入了木马、获取了root权限这样的情形,那么防火墙有没有INPUT、OUTPUT这些已经没什么意义了。 ↩︎

  2. 具体的参数和命令细节可以浏览官方网站说明页面↩︎

  3. 严格来说,Linux主机上的操作应当以一般用户而非root权限的管理员身份进行,但个人小网站通常在实践中不再建立多套账号,也就直接使用root账号操作了。 ↩︎

  4. 因为删除掉第5条规则后,位于第6行的下一条规则“上升”为第5行,因此需要执行两次命令。 ↩︎

  5. Gite, V. (2005). Linux Iptables: Block All Incoming Traffic But Allow SSH - nixCraft. [online] nixCraft. Available at: https://www.cyberciti.biz/tips/linux-iptables-4-block-all-incoming-traffic-but-allow-ssh.html [Accessed 15 Apr. 2018]. ↩︎