Linux系统(CentOS)安装iptables防火墙

1,先检查是否安装了iptables

检查安装文件-执行命令:rpm -qa|grep iptables

检查安装文件-执行命令:service iptables status

2,如果安装了就卸装(iptables-1.4.21-35.el7.x86_64 是上面命令查出来的版本)

执行命令:rpm -e --nodeps iptables-1.4.21-35.el7.x86_64

3,安装iptables-services

执行命令:yum install iptables-services

4,查看是否安装成功

执行命令:iptables --version

5,查看iptables运行状态

执行命令:systemctl status iptables

看到以下信息就说明正常运行了:

6,禁用/停止自带的firewalld

a,停止firewalld服务命令:systemctl stop firewalld

b,禁用firewalld服务命令:systemctl mask firewalld

7,iptables启动

执行命令:systemctl start iptables

8,设置iptables系统启动时自动启动

执行命令:systemctl enable iptables

9,清理iptables

先允许所有,不然有可能会出现奇怪的问题

执行命令:iptables -P INPUT ACCEPT

清空所有默认规则

执行命令:iptables -F

清空所有自定义规则

执行命令:iptables -X

所有计数器归0

执行命令:iptables -Z

10,设置开放端口

执行命令:

powershell 复制代码
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
powershell 复制代码
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
powershell 复制代码
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
powershell 复制代码
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
powershell 复制代码
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
powershell 复制代码
iptables -A INPUT -p tcp --dport 6379 -j ACCEPT

或者

powershell 复制代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
powershell 复制代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
powershell 复制代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

11,让上面的规则生效

执行命令:service iptables save

12,注册iptables服务

执行命令:systemctl enable iptables.service

13,开启iptables服务

执行命令:systemctl start iptables.service

14,查看iptables服务状态

执行命令:systemctl status iptables.service

或者命令:systemctl status iptables

看到以下信息就说明正常运行了:

15,关闭SELINUX

在linux系统找到文件:/etc/selinux/config

或者命令:vi /etc/selinux/config

16,注释config文件以下配置

powershell 复制代码
SELINUX=enforcing 
SELINUXTYPE=targeted

注释如下:

powershell 复制代码
#SELINUX=enforcing 
#SELINUXTYPE=targeted

17,设置使配置立即生效,在config文件添加:

添加 :setenforce 0

18,查看iptables开放了哪些端口号

执行命令:iptables -L -n

或者:/sbin/iptables -L -n

19,其他规则

其他入站一律丢弃

powershell 复制代码
iptables -P INPUT DROP

所有出站一律绿灯

powershell 复制代码
iptables -P OUTPUT ACCEPT

所有转发一律丢弃

powershell 复制代码
iptables -P FORWARD DROP

20,其他命令:

启动服务命令:service iptables start

停止服务命令:service iptables stop

重启服务命令:service iptables restart

相关推荐
福建佰胜张工2 小时前
深度解析 Allen-Bradley 150-B135NBDA 智能电机控制器:选型、参数与工业应用
运维·数据库·单片机·嵌入式硬件·安全·自动化
wefg12 小时前
【Docker】镜像相关指令
运维·docker·容器
feng_you_ying_li2 小时前
linux之网络层
linux·运维·网络
未来之窗软件服务2 小时前
Linux 运维日志删除的多种方式—东方仙盟
linux·运维·服务器·仙盟创梦ide·东方仙盟
卓豪终端管理2 小时前
IT 运维视角下的终端数据安全:能力框架与落地清单
运维
小雉系统3 小时前
BcoreOS实战:用一个升级包,让Rocky Linux跑进Debian服务器
linux·运维·服务器·debian
thethefighter3 小时前
快速预览Word、WPS秒开,支持空格弹窗
linux·word·wps·快速预览·空格弹窗·nhdeepview
甜到心里的蛋糕3 小时前
从 QQ 机器人到 B 站 AI 运营:一套可复现的视频流水线踩坑实录
运维·人工智能·python·自动化·视频制作
QYR-分析3 小时前
26.2% 高增!场内及园区无人物流车,厂内短驳自动化的 L4 货运载体
运维·自动化
用户8351414049493 小时前
把日志和寄存器值喂给AI做故障定位,我的实际问法
linux·ubuntu