Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 简单防火墙(UFW)-如何阻止本地主机上的单个端口_Linux_Port_Firewall_Iptables - Fatal编程技术网

Linux 简单防火墙(UFW)-如何阻止本地主机上的单个端口

Linux 简单防火墙(UFW)-如何阻止本地主机上的单个端口,linux,port,firewall,iptables,Linux,Port,Firewall,Iptables,我试图测试当一个应用程序与特定服务的连接被切断时会发生什么。我正在运行Ubuntu13.10,它听起来像是ufw(简单的防火墙)是正确的选择,但我不能让它像预期的那样工作。我确信这很简单,我做错了,但ufw的手册页给出了一个例子,阻止了我正在跟踪的端口的所有访问,但它不起作用。。。以下是我所经历的步骤 启动应用程序并运行netstat,以查明它连接到外部服务的端口: $ netstat Proto Recv-Q Send-Q Local Address Foreign Add

我试图测试当一个应用程序与特定服务的连接被切断时会发生什么。我正在运行Ubuntu13.10,它听起来像是ufw(简单的防火墙)是正确的选择,但我不能让它像预期的那样工作。我确信这很简单,我做错了,但ufw的手册页给出了一个例子,阻止了我正在跟踪的端口的所有访问,但它不起作用。。。以下是我所经历的步骤

启动应用程序并运行netstat,以查明它连接到外部服务的端口:

$ netstat
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
...
tcp6       0      0 mymachine.blah:39163    remoteservice.blah:2181 ESTABLISHED
...
因此,现在我尝试阻止本地端口39163进行任何通信:

$ sudo ufw deny 39163
Rule added
Rule added (v6)
我可以查一下,规则是这样的:

$ sudo ufw status
Status: active
To                         Action      From
--                         ------      ----
39163                      DENY        Anywhere
39163                      DENY        Anywhere (v6)
但我可以在我的应用程序日志中看到它仍在与远程服务通信,netstat的情况也是如此:

$ netstat
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
...
tcp6       0      0 mymachine.blah:39163    remoteservice.blah:2181 ESTABLISHED
...
为了更好地衡量,我还尝试对远程端口2181使用相同的ufw deny命令(以防我误解,您指定的端口应该是远程端口而不是本地端口),但这也没有什么区别

[编辑] iptables的输出相当大。我挑选了一些我认为不相关的链(所有ufw链要么没有规则,要么没有引用,要么两者都有)。其余部分如下。注意我从来没有手动使用iptables做过任何事情,我总是使用ufw或gufw(GUI前端到ufw)

您可以在底部看到我的规则(引用端口39163的链ufw用户输入)。我怀疑问题可能在于链ufw用户输入(使用我的规则)在输入之前被链ufw引用,但这反过来又没有被引用。但我对iptables的了解还不够,无法确认这是问题所在,也无法确定如何解决问题——我希望只使用更简单的ufw实用程序

$ sudo iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain ufw-before-input (0 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ufw-logging-deny  all  --  anywhere             anywhere             state INVALID
DROP       all  --  anywhere             anywhere             state INVALID
ACCEPT     icmp --  anywhere             anywhere             icmp destination-unreachable
ACCEPT     icmp --  anywhere             anywhere             icmp source-quench
ACCEPT     icmp --  anywhere             anywhere             icmp time-exceeded
ACCEPT     icmp --  anywhere             anywhere             icmp parameter-problem
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     udp  --  anywhere             anywhere             udp spt:bootps dpt:bootpc
ufw-not-local  all  --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             224.0.0.251          udp dpt:mdns
ACCEPT     udp  --  anywhere             239.255.255.250      udp dpt:1900
ufw-user-input  all  --  anywhere             anywhere            

Chain ufw-logging-deny (2 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere             state INVALID limit: avg 3/min burst 10
LOG        all  --  anywhere             anywhere             limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "

Chain ufw-not-local (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type LOCAL
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type MULTICAST
RETURN     all  --  anywhere             anywhere             ADDRTYPE match dst-type BROADCAST
ufw-logging-deny  all  --  anywhere             anywhere             limit: avg 3/min burst 10
DROP       all  --  anywhere             anywhere            

Chain ufw-user-input (1 references)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere             tcp dpt:39163
DROP       udp  --  anywhere             anywhere             udp dpt:39163

这不是真正的答案,因为我对UFW几乎一无所知。似乎。。复杂。如果您想简单地阻止您的服务,那么不使用ufw非常容易:

iptables -A OUTPUT -p tcp --dport 39163 -j DROP

/etc/ufw/before.rules
中,ufw似乎接受已建立的连接,请参见第24行:

# quickly process packets for which we already have a connection

所以我猜这是在(重新)配置的方式。不知道
ufw-reload
是否会忽略这一点,但我想不会,因为这会对已建立的连接造成“太”侵入性。

您能告诉我们它正在生成的实际iptables规则吗?@cnicutar-添加到原始帖子中。请参见此。要关闭39163端口,只需在中使用
ufw default allow,然后使用
ufw deny 39163
。我停止了ufw,并使用
sudo iptables-X
清除了iptables中的所有ufw链,然后如上所述运行您的命令,但之后运行netstat,仍然可以看到以下行:
tcp6 0 mymachine.blah:39163remoteservice.blah:2181已建立
运行
iptables--list确认规则存在,但netstat和应用程序日志表明数据包未被丢弃…抱歉,发现如果我使用端口2181而不是39163(即远程端口而非本地端口)运行此操作,它会正常工作。我正在阅读iptables手册页,试图找出如何指定本地端口。。。我想使用本地端口,因为我想运行应用程序的多个实例,并在其中一个实例连接失败时测试其行为。如果您想要源端口,可以使用
--sport
。干杯,刚刚发现-已编辑答案并接受它。