温馨提示×

centos iptables为何拒绝

小樊
36
2026-09-29 20:54:02
栏目: 智能运维

在 CentOS 上 iptables 规则“拒绝(REJECT / DROP)” 通常由以下几类原因导致,我按常见程度给你梳理一下,并给出排查方法。


一、最常见原因:规则本身就是拒绝的

1. 显式 REJECT / DROP 规则

你或系统已经写了类似规则:

iptables -A INPUT -p tcp --dport 22 -j REJECT
iptables -A INPUT -s 192.168.1.100 -j DROP

查看当前规则:

iptables -L -n -v
iptables -S

特点:

  • REJECT:对方会收到 “Connection refused”
  • DROP:对方表现为“超时”

二、默认策略是 DROP / REJECT

CentOS 默认策略可能是:

iptables -P INPUT DROP

这表示:没有匹配允许规则的流量全部被拒绝

检查:

iptables -L | grep policy

如果是 DROP,需要放行:

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

三、CentOS 7+ 使用了 firewalld(你却在用 iptables)

现象

  • 你改了 iptables
  • 但规则“不生效”或“被重置”
  • 端口仍然被拒绝

原因:

CentOS 7/8 默认用 firewalld,它会管理 iptables

查看:

systemctl status firewalld

解决方式(任选其一):

方案 1:用 firewalld(推荐)

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

方案 2:关闭 firewalld,用 iptables

systemctl stop firewalld
systemctl disable firewalld
yum install iptables-services
systemctl enable iptables
systemctl start iptables

四、SELinux 导致的“看起来像被拒绝”

SELinux 不是 iptables,但表现类似:

  • 服务起来了
  • 端口也放了
  • 但仍连不上

检查:

getenforce

临时关闭测试:

setenforce 0

若解决,需配置 SELinux 而非关掉它。


五、Docker / K8s 自动写入 iptables

如果你装了 Docker:

  • Docker 会写大量 iptables 规则
  • 可能把你的规则“插到前面”
  • 导致被 DROP

查看:

iptables -L DOCKER-USER -n -v

六、连接被拒绝的常见对照表

现象 可能原因
Connection refused REJECT / 服务没起
Timeout DROP / 防火墙
本地能连,外部不能 INPUT 没放行
刚重启就失效 没保存 iptables

保存规则(CentOS 6/iptables-services):

service iptables save

七、快速排查顺序(建议)

  1. iptables -L -n -v
  2. systemctl status firewalld
  3. netstat -tunlp
  4. getenforce
  5. 从另一台机器 telnet ip port

如果你愿意,可以告诉我:

  • CentOS 版本(6 / 7 / 8)
  • 被拒绝的端口 / 服务
  • iptables -L -n 的输出

我可以直接帮你定位是哪一条规则在“拒绝”。

0 踩