如何在Ubuntu上设置简单的防火墙?

有人可以给一些简单的步骤与configuration示例如何在Ubuntu上设置简单的防火墙(仅使用控制台)? 只允许ssh,http和https访问。

sudo ufw默认拒绝

sudo ufw允许http

sudo ufw允许https

sudo ufw允许ssh

sudo ufw启用

使用这个脚本。

只需决定是否允许传入ICMP(ping)。

# Clear any existing firewall stuff before we start iptables --flush iptables -t nat --flush iptables -t mangle --flush # As the default policies, drop all incoming traffic but allow all # outgoing traffic. This will allow us to make outgoing connections # from any port, but will only allow incoming connections on the ports # specified below. iptables --policy INPUT DROP iptables --policy OUTPUT ACCEPT # Allow all incoming traffic if it is coming from the local loopback device iptables -A INPUT -i lo -j ACCEPT # Accept all incoming traffic associated with an established # connection, or a "related" connection iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow incoming connections # SSH iptables -A INPUT -p tcp -i eth0 --dport 22 -m state --state NEW -j ACCEPT # HTTP iptables -A INPUT -p tcp -i eth0 --dport 80 -m state --state NEW -j ACCEPT # HTTPS iptables -A INPUT -p tcp -i eth0 --dport 443 -m state --state NEW -j ACCEPT # Allow icmp input so that people can ping us iptables -A INPUT -p icmp -j ACCEPT # Reject all other incoming packets iptables -A INPUT -j REJECT 

正如在对另一个答案的评论中指出的那样,在允许ssh端口之前,您不希望丢失连接。 从手册页:

“远程pipe理

当ufw通过它的脚本启动或启动ufw时,ufw将刷新它的链。 这是必需的,所以ufw可以保持一致的状态,但可能会丢弃现有的连接(例如ssh)。 ufw支持在启用防火墙之前添加规则,因此pipe理员可以执行以下操作:

 ufw allow proto tcp from any to any port 22 

在运行'ufw enable'之前。 规则仍然会被刷新,但启用防火墙后,ssh端口将会打开。 请注意,一旦ufw被启用,ufw在添加或删除规则时将不会刷新链(但会在修改规则或更改默认策略时)。

所以这是一个使用脚本来设置它的方法。 当你运行这个脚本时,你将会注销,但是运行它之后,你可以通过ssh重新login。

将以下内容放在脚本中,并将其称为start-firewall.sh

 #!/bin/sh ufw allow ssh ufw enable ufw default deny ufw allow http ufw allow https 

然后使其可执行并通过执行来运行它

 $ chmod + x start-firewall.sh $ sudo ./start-firewall.sh 

要了解更多信息,请阅读手册页 。

如果你熟悉iptables脚本,你将完全控制所有的防火墙function。 它远不如Firestarter友好,但可以在nano / vi编辑器的控制台上完成。 看看这个教程面向Ubuntu。

Quicktables帮助我学习了iptables规则。 只要运行这个脚本,它就会为你生成一个iptables脚本…然后你可以打开它并查看由你提出的问题产生的相关命令。 这是一个很好的学习资源。

不幸的是,它不再被维护。

http://qtables.radom.org/

我真的很喜欢使用火炮 ( 包 )。

要创build您喜欢的设置规则,您需要编辑文件/ etc / default / firehol并更改START_FIREHOL = YES

你会想让你的/etc/firehol/firehol.conf看起来像这样。

 version 5 interface any IfAll client any AnyClient accept server "ssh http https" accept # Accept everything from trusted networks server anystateless AllInside accept src "10.3.27.0/24" 

“火枪手”的一个伟大的事情是“尝试”命令。 你可以调整你的configuration文件,并做一个'firehol尝试',如果你通过ssh连接,并且你所改变的东西杀了你的networking访问,然后firehol会恢复变化。 为了让这些改变真正生效,你必须说提交。

我宁愿Shorewall 。 它很容易安装,但同时灵活。

也许你应该看看http://iptables-tutorial.frozentux.net/iptables-tutorial.html 。 您也可以在lartc.org上find更多信息

sudo apt-get install firestarter

然后,查看系统 – >pipe理菜单。