防火墙:firewalld 与 docker 打开本地服务端口

防火墙:firewalld 与 docker 打开本地服务端口

我正在尝试在非标准端口上打开 ssh 端口,例如12452,我使用了这些命令,但没有一个起作用

iptables -I INPUT -p tcp --dport 12452 -j ACCEPT 
iptables -I OUTPUT -p tcp --dport 12452 -j ACCEPT
iptables -I DOCKER-USER -p tcp --dport 12452 -j ACCEPT

也使用过firewalld,我使用的活动区域是 docker 所以我使用了:

firewall-cmd --zone=docker --add-port=12452/tcp

仍然不起作用...但实际上我意识到我当前使用的 ssh 端口 22 规则没有在任何地方打开!甚至在规则中也没有iptables

注意:我ufw之前安装过,只​​是将其删除

我的iptables-save输出是这样的:

# Generated by iptables-save v1.8.7 on Mon Aug 14 20:51:29 2023
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:DOCKER - [0:0]
:DOCKER-INGRESS - [0:0]
:DOCKER-ISOLATION-STAGE-1 - [0:0]
:DOCKER-ISOLATION-STAGE-2 - [0:0]
:DOCKER-USER - [0:0]
:f2b-sshd - [0:0]
-A INPUT -p tcp -m multiport --dports 22 -j f2b-sshd
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-INGRESS
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A FORWARD -o docker_gwbridge -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker_gwbridge -j DOCKER
-A FORWARD -i docker_gwbridge ! -o docker_gwbridge -j ACCEPT
-A FORWARD -i docker_gwbridge -o docker_gwbridge -j DROP
-A DOCKER-INGRESS -p tcp -m tcp --dport 443 -j ACCEPT
-A DOCKER-INGRESS -p tcp -m state --state RELATED,ESTABLISHED -m tcp --sport 443 -j ACCEPT
-A DOCKER-INGRESS -j RETURN
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -i docker_gwbridge ! -o docker_gwbridge -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -o docker_gwbridge -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
-A f2b-sshd -j RETURN
COMMIT
# Completed on Mon Aug 14 20:51:29 2023
# Generated by iptables-save v1.8.7 on Mon Aug 14 20:51:29 2023
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:DOCKER - [0:0]
:DOCKER-INGRESS - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER-INGRESS
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT -m addrtype --dst-type LOCAL -j DOCKER-INGRESS
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -o docker_gwbridge -m addrtype --src-type LOCAL -j MASQUERADE
-A POSTROUTING -s x.x.x.x/x ! -o docker0 -j MASQUERADE
-A POSTROUTING -s x.x.x.x/x ! -o docker_gwbridge -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
-A DOCKER -i docker_gwbridge -j RETURN
-A DOCKER-INGRESS -p tcp -m tcp --dport 443 -j DNAT --to-destination x.x.x.x:x
-A DOCKER-INGRESS -j RETURN
COMMIT
# Completed on Mon Aug 14 20:51:29 2023

输出firewall-cmd --zone=docker --list-all是这样的:

docker (active)
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: docker0 docker_gwbridge
  sources: 
  services: 
  ports: 12452/tcp
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

答案1

我不知道为什么,但似乎firewalld仍在使用public区域!

我尝试像这样在公共区域打开端口

firewall-cmd --zone=public --add-port=12452/tcp

令人惊讶的是它有效!

相关内容