温馨提示×

centos iptables如何安装

小樊
40
2026-09-02 16:04:07
栏目: 智能运维

在 CentOS 上安装和启用 iptables,通常分几种情况(CentOS 6 / CentOS 7+ 略有不同)。下面按常见场景说明。


一、CentOS 7 / 8 / Stream(使用 firewalld 的系统)

1. 安装 iptables 服务

yum install -y iptables-services

(CentOS 8 / Stream 可用 dnf)

dnf install -y iptables-services

2. 停止并禁用 firewalld(可选)

systemctl stop firewalld
systemctl disable firewalld

3. 启动 iptables

systemctl start iptables
systemctl enable iptables

4. 查看状态

systemctl status iptables
iptables -L -n

二、CentOS 6

CentOS 6 默认使用 iptables:

yum install -y iptables
service iptables start
chkconfig iptables on

三、只安装 iptables 命令(不启用服务)

如果你只是想用 iptables 命令而不作为系统服务:

yum install -y iptables

查看规则:

iptables -L -n

四、常见说明

  • iptables:用户空间命令
  • iptables-services:提供 systemctl 管理的 iptables 服务
  • CentOS 7+ 默认使用 firewalld,不是必须换 iptables

五、示例:允许 SSH

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
service iptables save   # CentOS 6
# 或
/usr/libexec/iptables/iptables.init save  # CentOS 7

如果你告诉我 CentOS 具体版本(如 7.9 / 8 / Stream),我可以给你更精确的步骤。

0 踩