CentOS7系统的VPS云服务器安装iptables防火墙(禁用/停止自带的firewalld服务)
云服务器
CentOS7系统的VPS云服务器安装iptables防火墙(禁用/停止自带的firewalld服务)
2026-01-21 15:10
centos7系统的VPS云服务器如何安装iptables防火墙?本篇文章主要介绍了centos7安装iptables防火墙的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。不仅有安装iptables防火墙的命令和说明,还有如何禁用/停止自带的firewalld服务,起跟随小编过来看看吧 使用背景: 1,CentOS7默认的防火墙不是iptables,而是firewalle. 2,一些软件的端口号要放开来提供服务,如:22,80等常用端口 3,提供web服务的需要 使用步骤: 一、安装centos7,自行购买一诺网络科技VPS/云服务器,选择centos7即可! 二、centos7基本配置 三、CentOS7安装iptables防火墙: 0,CentOS7默认的防火墙不是iptables,而是firewalle. 查看:打开/etc/sysconfig/目录后,并未发现iptables文档(可能会有iptables-config文档等,别混淆了) [root@localhost~]#cd/etc/sysconfig/ 1,安装iptable与iptable-service #先检查是否安装了iptables [root@localhost~]#serviceiptablesstatus #安装iptables [root@localhost~]#yuminstall-yiptables#通常情况下是安装好了的 yuminstall-yiptables*(网友推荐,看下面的评论,在这里,谢谢他/她) #升级iptables [root@localhost~]#yumupdateiptables #安装iptables-services [root@localhost~]#yuminstalliptables-services#通常情况下没有安装 2,禁用/停止自带的firewalld服务 #停止firewalld服务 [root@localhost~]#systemctlstopfirewalld #禁用firewalld服务 [root@localhost~]#systemctlmaskfirewalld #查看iptables现有规则 [root@localhost~]#iptables-L-n #先允许所有,不然有可能会杯具 [root@localhost~]#iptables-PINPUTACCEPT [root@localhost~]#systemctlenableiptables.service#设置防火墙开机启动 ―――――――启动/关闭/重启iptables―――――――――――― [root@localhost~]#systemctlstopiptables [root@localhost~]#systemctlstartiptables [root@localhost~]#systemctlrestartiptables ――――――――――――――――――――――――――――――C #@1,手动添加端口号(方法一) [root@localhost~]#vi/etc/sysconfig/iptables#配置防火墙端口 -AINPUT-mstateCstateNEW-mtcp-ptcpCdport3306-jACCEPT-AINPUT-mstateCstateNEW-mtcp-ptcpCdport8080-jACCEPT-AINPUT-mstateCstateNEW-mtcp-ptcpCdport6379-jACCEPT [root@localhost~]#serviceiptablessave#保存防火墙配置 Cend ###################################具体配置说明###################################### @2,命令添加端口号(方法二): 参考:https://www.linuxidc.com/Linux/2017-10/147238.htm 一、安装iptableiptable-service 1,先检查是否安装了iptables [root@localhost~]#serviceiptablesstatus 2,安装iptables [root@localhost~]#yuminstall-yiptables 3,升级iptables [root@localhost~]#yumupdateiptables 4,安装iptables-services [root@localhost~]#yuminstalliptables-services 二、禁用/停止自带的firewalld服务 1,停止firewalld服务 [root@localhost~]#systemctlstopfirewalld 2,禁用firewalld服务 [root@localhost~]#systemctlmaskfirewalld 三、设置现有规则 #查看iptables现有规则[root@localhost~]#iptables-L-n#先允许所有,不然有可能会杯具[root@localhost~]#iptables-PINPUTACCEPT#清空所有默认规则[root@localhost~]#iptables-F#清空所有自定义规则[root@localhost~]#iptables-X#所有计数器归0[root@localhost~]#iptables-Z#允许来自于lo接口的数据包(本地访问)[root@localhost~]#iptables-AINPUT-ilo-jACCEPT#开放22端口iptables-AINPUT-ptcpCdport22-jACCEPT#开放21端口(FTP)iptables-AINPUT-ptcpCdport21-jACCEPT#开放80端口(HTTP)iptables-AINPUT-ptcpCdport80-jACCEPT#开放443端口(HTTPS)iptables-AINPUT-ptcpCdport443-jACCEPT…====>按照实际要求:是否加下面的:――――――――――――――――――――――-#允许pingiptables-AINPUT-picmpCicmp-type8-jACCEPT#允许接受本机请求之后的返回数据RELATED,是为FTP设置的iptables-AINPUT-mstateCstateRELATED,ESTABLISHED-jACCEPT#其他入站一律丢弃iptables-PINPUTDROP#所有出站一律绿灯iptables-POUTPUTACCEPT#所有转发一律丢弃iptables-PFORWARDDROP――――――――――――――――――――――- 四、其他规则设定 #如果要添加内网ip信任(接受其所有TCP请求)iptables-AINPUT-ptcp-s45.96.174.68-jACCEPT#过滤所有非以上规则的请求iptables-PINPUTDROP#要封停一个IP,使用下面这条命令:iptables-IINPUT-s***.***.***.***-jDROP#要解封一个IP,使用下面这条命令:iptables-DINPUT-s***.***.***.***-jDROP 五、保存规则设定 #保存上述规则serviceiptablessave 六、开启iptables服务 #注册iptables服务#相当于以前的chkconfigiptablesonsystemctlenableiptables.service#开启服务systemctlstartiptables.service#查看状态systemctlstatusiptables.service ―――――――――――――――――――――――――――――――――- 问题 问题:解决vsftpd在iptables开启后,无法使用被动模式的问题 1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容 #添加以下内容,注意顺序不能调换IPTABLES_MODULES=”ip_conntrack_ftp”IPTABLES_MODULES=”ip_nat_ftp” 2.重新设置iptables设置 iptables-AINPUT-mstateCstateRELATED,ESTABLISHED-jACCEPT 以下为完整设置脚本 #!/bin/shiptables-PINPUTACCEPTiptables-Fiptables-Xiptables-Ziptables-AINPUT-ilo-jACCEPTiptables-AINPUT-ptcpCdport22-jACCEPTiptables-AINPUT-ptcpCdport21-jACCEPTiptables-AINPUT-ptcpCdport80-jACCEPTiptables-AINPUT-ptcpCdport443-jACCEPTiptables-AINPUT-picmpCicmp-type8-jACCEPTiptables-AINPUT-mstateCstateRELATED,ESTABLISHED-jACCEPTiptables-PINPUTDROPiptables-POUTPUTACCEPTiptables-PFORWARDDROPserviceiptablessavesystemctlrestartiptables.service CentOS7 [root@localhost~]#cat/etc/redhat-release#查看centos版本 CentOSLinuxrelease7.2.1511(Core) 1.关闭firewall [root@localhost~]#systemctlstopfirewalld.service#停止firewall [root@localhost~]#systemctldisablefirewalld.service#禁止firewall开机启动 2.安装iptables [root@localhost~]#yuminstalliptables-services#安装 [root@localhost~]#systemctlrestartiptables.service#重启防火墙使配置生效 [root@localhost~]#systemctlenableiptables.service#设置防火墙开机启动 [root@localhost~]#systemctldisableiptables.service#禁止防火墙开机启动 CentOS6 [root@localhost~]#serviceiptablestatusC查看防火墙状态 [root@localhost~]#servcieiptablesstopC临时关闭防火墙 [root@localhost~]#serviceiptablesstartC临时启动防火墙 [root@localhost~]#serviceiptablesrestartC重启防火墙 [root@localhost~]#chkconfigiptablesoffC永久关闭防火墙 [root@localhost~]#chkconfigiptablesonC永久开启防火墙