如何在CentOS 7上安装iptables

时间:2020-03-05 15:24:37  来源:igfitidea点击:

从CentOS 7开始,FirewallD取代iptables作为默认的防火墙管理工具。

FirewallD是一个完整的防火墙解决方案,可以使用名为firewall-cmd的命令行实用程序进行控制。
如果我们对Iptables命令行语法更满意,则可以禁用FirewallD并返回经典的iptables设置。

本教程将向我们展示如何禁用FirewallD服务和安装iptables。

准备工作

在开始本教程之前,请确保我们以具有sudo特权的用户身份登录。

禁用防火墙D

要在CentOS 7系统上禁用FirewallD,请执行以下步骤:

  • 键入以下命令以停止FirewallD服务:
sudo systemctl stop firewalld
  • 禁用FirewallD服务以在系统启动时自动启动:
sudo systemctl disable firewalld
  • 屏蔽FirewallD服务以防止其被其他服务启动:
sudo systemctl mask --now firewalld

安装并启用Iptables

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

  • 运行以下命令以从CentOS存储库安装“ iptables-service”软件包:
sudo yum install iptables-services
  • 安装软件包后,启动Iptables服务:
sudo systemctl start iptablessudo systemctl start ip6tables
  • 启用Iptables服务以在系统启动时自动启动:
sudo systemctl enable iptablessudo systemctl enable ip6tables
  • 使用以下命令检查iptables服务状态:
sudo systemctl status iptablessudo systemctl status ip6tables
  • 要检查当前的iptables规则,请使用以下命令:
sudo iptables -nvLsudo ip6tables -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服务,并且可以开始构建防火墙了。
重新启动后,修改的内容将依然存在。