Status:已弃用
本文介绍不再受支持的 Ubuntu 版本。如果您当前运行的服务器运行 Ubuntu 12.04,我们强烈建议您升级或迁移到受支持的 Ubuntu 版本:
-
升级到Ubuntu 14.04.
- 从 Ubuntu 14.04 升级到 Ubuntu 16.04
- 将服务器数据迁移到支持的版本
Reason:
Ubuntu 12.04 已于 2017 年 4 月 28 日终止生命周期 (EOL)并且不再接收安全补丁或更新。本指南不再维护。
请参阅:
本指南可能仍然可以作为参考,但可能不适用于其他 Ubuntu 版本。如果有的话,我们强烈建议您使用针对您所使用的 Ubuntu 版本编写的指南。您可以使用页面顶部的搜索功能来查找更新的版本。
关于iptables
为了使服务器在初始设置后更加安全,Ubuntu 附带了 iptables,它是发行版的默认防火墙。一开始,虽然配置了 Ubuntu 防火墙,但它被设置为允许虚拟专用服务器上的所有传入和传出流量。为了对服务器提供更强的保护,我们可以添加一些基本的 iptables 规则。
iptables 规则来自一系列选项,这些选项可以组合起来创建每个特定的进程。每个穿过防火墙的数据包都会按顺序由每条规则进行检查。一旦与规则匹配,数据包就会遵循关联的操作,否则它将继续沿线进行。
Note:本教程涵盖 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
我们可以继续分解:
-
-A告诉 iptables 将规则附加到表中。
-
INPUT将此规则指定为输入链的一部分。
-
米康跟踪接下来是--cstate 已建立,相关保证此规则的结果仅适用于当前连接,并且允许与它们相关的连接
-
-j 接受告诉 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
-
-I 输入 1将此规则放在表格的开头
-
lo指环回接口
-
-j 接受然后保证环回流量将被接受
现在我们已经完成了基本防火墙的创建。您的规则应如下所示(我们可以通过输入 -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 permanent 的包。
我们可以使用 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
任何服务器重新启动后,您都会看到规则仍然存在。
埃特尔·斯维尔德洛夫