且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CentOS 7下iptables配置添加修改规则端口方法

更新时间:2022-06-07 02:08:13


Linux CentOS 7默认没有安装iptables,默认的防火墙是firewalld,小编分享CentOS 7系统下iptables安装、iptables规则配置(放行或者禁用端口)教程方法:


以下大致步骤为:先关闭firewalld防火墙,然后按照iptables,最后配置添加或者禁用端口规则,详细如下:


检查firewalld并禁用


安装iptables之前先检查下当前CentOS 7系统下的firewalld防火墙状态,如果是开启状态,需要先关闭firewalld防火墙。


先查看firewalld状态


查看firewalld状态,命令:


systemctl status firewalld

CentOS 7下iptables配置添加修改规则端口方法


开启状态:显示绿色的“active (running)”,则代表开启;
关闭状态:显示“inactive (dead)”,代表关闭。


再关闭firewalld


如果firewalld是开启状态,需要执行命令:


systemctl stop firewalld

,关闭firewalld即可。


一:安装iptables


安装命令:iptables:yum install -y iptables-services


二:启动iptables


启动命令:systemctl start iptables


三:查看当前iptables状态


运行iptables启动命令后,可以查看下iptables是否启动成功,确保iptables启动成功
查看状态:systemctl status iptables
如果提示绿色的“active (exited)”,则iptables已经启动成功。


四:查看iptables默认规则


查看默认规则命令:iptables -L
安装iptables后,可以查看下当前系统下的iptables规则,熟悉一下。


五:备份iptables规则


安全起见,可以先备份下当前的iptables规则,然后再修改/添加规则
备份命令:cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak


六:添加或修改规则(放行或禁用端口)


小编以放行80号端口为例:


  • 1、添加以下规则可以放行80端口命令:

iptables -I INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT

  • 2、查看规则是否生效,命令:

iptables -L

  • 3、生效后保存添加的规则,命令:

iptables-save > /etc/sysconfig/iptables

  • 4、设置iptables开启启动,命令:

systemctl enable iptables.service

以上为CentOS 7系统下iptables安装、配置规则和启用的方法,前面啰嗦比较多,重点在第六步,设置完毕后重启服务器(命令:systemctl reboot)去测试刚刚配置的iptables规则吧,或者移步:云服务器 ECS Linux CentOS 7 下使用iptables服务,小编再次提醒大家,如果之前编辑过iptables先备份再更改。