大网数据广告活动
服务器运维问答

An iptables rule doesn't work

· 52CE 我爱测

I'm facing the following problem: I want to block the port 8000 being accessed from the network (block all connections except the ones from 127.0.0.1). So to simplify my problem and as a first step, I decided to block all access …

问题原文

原帖链接 · rsabir · CC BY-SA 3.0

I'm facing the following problem: I want to block the port 8000 being accessed from the network (block all connections except the ones from 127.0.0.1). So to simplify my problem and as a first step, I decided to block all access to the port 8000 by using this rule :

iptables -A INPUT -p tcp --dport 8000 -j DROP

Now, I know that I should take the reverse approch wich is drop all connections except the trusted ones but I decided to make this for educational purpose.

iptables -L shows me this output:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8000

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  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
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.1          tcp dpt:80

Unfortunately, I still succeed to access the server from port 8000. Can you tell me what I have to do? I didn't do anything after executing the drop rule command.

已采纳解答

原帖链接 · basvdlei · CC BY-SA 3.0

Looks like you are using Docker. When exposing a container port Docker will create rules to forward packets to your container. These packets never go through the INPUT chain. See: http://www.faqs.org/docs/iptables/traversingoftables.html

You can filter using the FORWARD chain.

iptables -I FORWARD -p tcp -d 172.17.0.1 --dport 80 -j DROP

Note, this will not drop connections from localhost and you have to use the rewritten destination (ie listen port in the container.)

If you want more control you can run the Docker daemon with --iptables=false, but you will have to manage the packet forwarding to your containers manually.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。