在CentOS 7上安装iptables

Page content

安装并启用iptables

  • 执行以下步骤在CentOS 7系统上安装Iptables

1.运行以下命令以iptables-service从CentOS存储库安装软件包:

sudo yum install iptables-services

2.安装软件包后,启动Iptables服务:

sudo systemctl start iptables

3.启用Iptables服务以在系统启动时自动启动:

sudo systemctl enable iptables

4.使用以下命令检查iptables服务状态:

sudo systemctl status iptables

5.要检查当前的iptables规则,请使用以下命令:

sudo iptables -nvL

默认情况下,仅SSH端口22是打开的。输出应如下所示:

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5400 6736K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    2   148 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    3   180 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4298 packets, 295K bytes)
 pkts bytes target     prot opt in     out     source               destination     
  • 至此,您已经成功启用了iptables服务,并且可以开始构建防火墙了。重新启动后,更改将继续存在。