如何在Ubuntu12.04上使用iptables设置防火墙

来自菜鸟教程
跳转至:导航、​搜索


状态: 已弃用

本文介绍了不再受支持的 Ubuntu 版本。 如果您当前正在运行运行 Ubuntu 12.04 的服务器,我们强烈建议您升级或迁移到受支持的 Ubuntu 版本:

原因: Ubuntu 12.04 已于 2017 年 4 月 28 日终止生命周期 (EOL) and no longer receives security patches or updates. This guide is no longer maintained.

请参阅:
本指南可能仍可用作参考,但可能不适用于其他 Ubuntu 版本。 如果可用,我们强烈建议使用为您正在使用的 Ubuntu 版本编写的指南。 您可以使用页面顶部的搜索功能来查找更新的版本。


关于 iptables

为了在初始设置后使服务器更加安全,Ubuntu 附带了 iptables,它是发行版的默认防火墙。 一开始,虽然配置了 Ubuntu 防火墙,但它被设置为允许虚拟专用服务器上的所有传入和传出流量。 为了在服务器上启用一些更强的保护,我们可以添加一些基本的 iptables 规则。

iptables 规则来自一系列选项,这些选项可以组合起来创建每个特定的进程。 每个穿过防火墙的数据包都由每个规则按顺序检查。 一旦它匹配一个规则,数据包就会遵循相关的动作,否则它会继续下一行。

笔记: 本教程涵盖 IPv4 安全性。 在 Linux 中,IPv6 的安全性与 IPv4 分开维护。 例如,“iptables”仅维护 IPv4 地址的防火墙规则,但它有一个称为“ip6tables”的 IPv6 对应项,可用于维护 IPv6 网络地址的防火墙规则。

如果您的 VPS 配置为 IPv6,请记住使用适当的工具保护您的 IPv4 和 IPv6 网络接口。 有关 IPv6 工具的更多信息,请参阅本指南:如何配置工具以在 Linux VPS 上使用 IPv6

iptables 命令

尽管本教程将介绍为服务器提供一些基本安全性的有限数量的命令,但是可以为 iptables 开发各种细微的和特定的案例。 以下是为您的 VPS 开发防火墙的一些最有用的命令,但请记住,这是一个简短的列表,还有许多其他选项。

-A: (Append), adds a rule to iptables
-L:  (List), shows the current rules
-m conntrack: allows rules to be based on the current connection state, elaborated in the the --cstate command.
--cstate: explains the states that connections can be in, there are 4: New, Related, Established, and Invalid
-p: (protocol), refers to the the protocol of the rule or of the packet to check.The specified protocol can be one of tcp, udp, udplite, icmp, esp, ah, sctp or the special keyword "all".
--dport: (port), refers to the the port through which the machine connects
-j: (jump), this command refers to the action that needs to be taken if something matches a  rule perfectly. It translates to one of four possibilities:
    -ACCEPT: the packet is accepted, and no further rules are processed
    -REJECT: the packet is rejected, and the    sender is notified, and no further rules are processed
    -DROP: the packet is rejected, but the  sender is not notified, and no further rules are processed
    -LOG: the packet is accepted but logged, and the following rules are processed 
-I: (Insert), adds a rule between two previous ones
-I INPUT 3: inserts a rule to make it the third in the list
-v: (verbose), offers more details about a rule

创建 iptables 规则:

如果您输入以下内容,您可以看到当前的 iptables 规则:

sudo 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

如果您有另一组规则或想要重新开始,您始终可以通过刷新和删除所有规则将规则设置回默认值:

sudo iptables -F

此外,如果您想加快使用 iptables 的工作,您可以在命令中包含 -n。 此选项禁用 DNS 查找并阻止命令尝试查找规则集中每个 IP 的反向。 您可以使用它来列出规则,例如:

iptables -L -n

基本防火墙

目前的规则允许所有连接,包括传入和传出。 没有任何安全措施。 当我们建立表格时,请记住,一旦数据包被接受、拒绝或丢弃,就不会处理进一步的规则。 因此,首先出现的规则优先于后面的规则。

在创建规则时,我们必须确保防止自己意外阻止 SSH(我们连接到服务器的方法)。

首先,让我们确保允许所有当前连接,在制定规则时的所有连接都将保持在线:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

我们可以继续分解:

  1. -A 告诉 iptables 将规则附加到表中。
  2. INPUT 将此规则指定为输入链的一部分。
  3. m conntrack 后跟 --cstate ESTABLISHED,RELATED 保证此规则的结果将仅适用于当前连接,并且允许与它们相关的连接
  4. -j ACCEPT 告诉 JUMP 数据包接受并且连接仍然存在。

在我们确信所有当前与虚拟专用服务器的连接都可以保持不间断后,我们可以继续开始阻止其他不安全的连接。

假设我们想要阻止所有传入的流量,除了那些来自 2 个公共端口的流量:22 用于 SSH,80 用于 Web 流量。 我们继续使用以下命令允许指定端口上的所有流量:

sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

在这两个命令中,-p 选项代表正在建立连接的协议,在本例中为 tcp,而 --dport指定传输数据包的端口。

在我们保证所需的流量将通过防火墙之后,我们可以通过阻止所有剩余流量访问我们的虚拟服务器来完成。 因为这是列表中的最后一条规则,所以与 iptables 中的任何先前规则匹配的所有流量都不会受到影响,并将按照我们之前设置的方式处理。

让我们制定一个规则来阻止所有剩余的流量:

sudo iptables -P INPUT DROP

有了它,我们可以看到我们更新的规则是什么样的:

sudo iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            ctstate RELATED,ESTABLISHED 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 

我们快完成了。 但是,我们还缺少一条规则。 我们需要为我们的 VPS 提供环回访问。 如果我们现在在没有其他限定符的情况下添加规则,它将转到列表的末尾,并且由于它会遵循阻止所有流量的规则,因此永远不会生效。

为了解决这个问题,我们需要在列表中首先制定这个规则,使用 INPUT 选项:

sudo iptables -I INPUT 1 -i lo -j ACCEPT
  1. -I INPUT 1 将此规则放在表格的开头
  2. lo 指环回接口
  3. -j ACCEPT 然后保证环回流量将被接受

现在我们已经完成了基本防火墙的创建。 您的规则应如下所示(我们可以通过键入 -v 查看 iptable 的详细信息):

sudo iptables -L -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            
 1289 93442 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    2   212 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http     

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

Chain OUTPUT (policy ACCEPT 157 packets, 25300 bytes)
 pkts bytes target     prot opt in     out     source               destination       

但是,一旦虚拟服务器重新启动,iptables 规则就会被清除。 下一步将介绍保存和恢复 iptables 规则。

保存 iptables 规则

iptables 规则虽然有效,但如果服务器重启,它们会自动被删除。 为了确保它们保持有效,我们可以使用一个名为 IP-Tables persistent 的包。

我们可以使用 apt-get 安装它:

sudo apt-get install iptables-persistent

在安装过程中,系统会询问您是否要将 iptables 规则同时保存到 IPv4 规则和 IPv6 规则中。 对两者都说是。

然后,您的规则将保存在 /etc/iptables/rules.v4 和 /etc/iptables/rules.v6 中。

安装完成后,启动 iptables-persistent 运行:

sudo service iptables-persistent start

在任何服务器重新启动后,您将看到规则仍然存在。

埃特尔·斯维尔德洛夫