即使我将规则设置为允许,UFW也会阻止所有操作

我正在使用ubuntu服务器,现在我正在尝试使用以下命令启用防火墙:

ufw default deny incoming ufw default allow outgoing ufw allow ssh ufw allow www ufw allow https ufw enable 

我也试过让ufw default deny incoming最后一个,但仍然没有运气,当我启用防火墙它阻止eveything当我将默认设置为拒绝时,但是当我将其设置为允许时,它运行良好,就像规则被忽略了。 什么可能导致这个?

编辑

这是我输出的iptables -L -v -n我也尝试了所提出的解决方案,但仍然没有运气, 只有在我default allow incoming时它才能正常工作

 Chain INPUT (policy DROP 30 packets, 1764 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 2 packets, 104 bytes) pkts bytes target prot opt in out source destination Chain ufw-after-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw-after-input (0 references) pkts bytes target prot opt in out source destination 0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137 0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138 0 0 ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139 0 0 ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445 0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:67 0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68 0 0 ufw-skip-to-policy-input all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type BROADCAST Chain ufw-after-logging-forward (0 references) pkts bytes target prot opt in out source destination 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] " Chain ufw-after-logging-input (0 references) pkts bytes target prot opt in out source destination 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] " Chain ufw-after-logging-output (0 references) pkts bytes target prot opt in out source destination Chain ufw-after-output (0 references) pkts bytes target prot opt in out source destination Chain ufw-before-forward (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 4 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 0 0 ufw-user-forward all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-before-input (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ufw-logging-deny all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 4 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp spt:67 dpt:68 0 0 ufw-not-local all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT udp -- * * 0.0.0.0/0 224.0.0.251 udp dpt:5353 0 0 ACCEPT udp -- * * 0.0.0.0/0 239.255.255.250 udp dpt:1900 0 0 ufw-user-input all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-before-logging-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw-before-logging-input (0 references) pkts bytes target prot opt in out source destination Chain ufw-before-logging-output (0 references) pkts bytes target prot opt in out source destination Chain ufw-before-output (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ufw-user-output all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-logging-allow (0 references) pkts bytes target prot opt in out source destination 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW ALLOW] " Chain ufw-logging-deny (2 references) pkts bytes target prot opt in out source destination 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID limit: avg 3/min burst 10 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] " Chain ufw-not-local (1 references) pkts bytes target prot opt in out source destination 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type MULTICAST 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type BROADCAST 0 0 ufw-logging-deny all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-reject-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw-reject-input (0 references) pkts bytes target prot opt in out source destination Chain ufw-reject-output (0 references) pkts bytes target prot opt in out source destination Chain ufw-skip-to-policy-forward (0 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-skip-to-policy-input (7 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-skip-to-policy-output (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-track-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw-track-input (0 references) pkts bytes target prot opt in out source destination Chain ufw-track-output (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW Chain ufw-user-forward (1 references) pkts bytes target prot opt in out source destination Chain ufw-user-input (1 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:22 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:443 Chain ufw-user-limit (0 references) pkts bytes target prot opt in out source destination 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 5 LOG flags 0 level 4 prefix "[UFW LIMIT BLOCK] " 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain ufw-user-limit-accept (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain ufw-user-logging-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw-user-logging-input (0 references) pkts bytes target prot opt in out source destination Chain ufw-user-logging-output (0 references) pkts bytes target prot opt in out source destination Chain ufw-user-output (1 references) pkts bytes target prot opt in out source destination 

打开终端并键入以下命令:

通过重置开始,这将删除所有现有规则:

 sudo ufw reset 

下一个,

 sudo ufw app list 

这将列出可用的应用程序配置文件,例如OpenSSH等。 要获取应用程序的信息,请键入以下命令,如下例所示:

 sudo ufw app info OpenSSH 

这是输出:

 Profile: OpenSSH Title: Secure shell server, an rshd replacement Description: OpenSSH is a free implementation of the Secure Shell protocol. Port: 22/tcp 

要允许OpenSSH访问,您可以使用以下规则:

 sudo ufw allow 22/tcp 

与Debian不同,www和https通常不包含在应用程序配置文件中,但是,我们知道它们在端口80和443上运行,因此请使用以下命令:

 sudo ufw allow 80/tcp sudo ufw allow 443/tcp 

如果你想添加UDP,也可以这样做。

 sudo ufw allow 80/udp sudo ufw allow 443/udp 

禁用并启用ufw以应用更改:

 sudo ufw disable sudo ufw enable 

显示您的规则:

 sudo ufw status 

最后,ufw不太友好的一个方面是拒绝规则通常特朗普允许规则。 例如,您无法将所有内容设置为拒绝,然后将端口设置为允许。 所有端口仍将被阻止。 有关详细信息,请参见此处


您可以添加这些规则以全局阻止除22,53,80和443之外的所有端口。我添加了端口53以允许DNS请求。 如果您不需要进行DNS查询,只需相应地修改规则即可。

要仅为传入设置这些阻止规则,您可以sudo ufw deny in 1:22/tcp使用sudo ufw deny in 1:22/tcp 。 或者,设置为传出sudo ufw deny out 1:22/tcp输出sudo ufw deny out 1:22/tcp等等。

 sudo ufw deny 1:21/tcp sudo ufw deny 1:21/udp sudo ufw deny 23:52/tcp sudo ufw deny 23:52/udp sudo ufw deny 54:79/tcp sudo ufw deny 54:79/udp sudo ufw deny 81:442/tcp sudo ufw deny 81:442/udp sudo ufw deny 444:65535/tcp sudo ufw deny 444:65535/udp 

仅供参考:如果其他人有这个问题。

在详细的iptables输出中,我注意到INPUT,OUTPUT和FORWARD链中缺少ufw规则。 当我在某些时候启用ufw后运行iptables -F来删除我的自定义FW规则时,我的系统就像这样结束了。 如果iptables中已经存在一些自己的链,那么ufw似乎不会添加顶级规则。

我最终取消安装ufw,重新启动,运行’iptables -F’(删除以前仍处于活动状态的iptables规则),然后重新安装和配置ufw。 最高级别的ufw规则现在又回来了。 可能没有必要卸载/重新安装。 只是通过禁用ufw并重新启动从iptables中删除所有ufw规则可能已经成功了。

这是顶级链应该是什么样的(在Debian 9.4上)。

 Chain INPUT (policy DROP) target prot opt source destination ufw-before-logging-input all -- 0.0.0.0/0 0.0.0.0/0 ufw-before-input all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-input all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-logging-input all -- 0.0.0.0/0 0.0.0.0/0 ufw-reject-input all -- 0.0.0.0/0 0.0.0.0/0 ufw-track-input all -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy DROP) target prot opt source destination ufw-before-logging-forward all -- 0.0.0.0/0 0.0.0.0/0 ufw-before-forward all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-forward all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-logging-forward all -- 0.0.0.0/0 0.0.0.0/0 ufw-reject-forward all -- 0.0.0.0/0 0.0.0.0/0 ufw-track-forward all -- 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT) target prot opt source destination ufw-before-logging-output all -- 0.0.0.0/0 0.0.0.0/0 ufw-before-output all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-output all -- 0.0.0.0/0 0.0.0.0/0 ufw-after-logging-output all -- 0.0.0.0/0 0.0.0.0/0 ufw-reject-output all -- 0.0.0.0/0 0.0.0.0/0 ufw-track-output all -- 0.0.0.0/0 0.0.0.0/0