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

相关推荐
哈基咪怎么可能是AI7 小时前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行1 天前
Linux和window共享文件夹
linux
木心月转码ing1 天前
WSL+Cpp开发环境配置
linux
蝎子莱莱爱打怪2 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀2 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应2 天前
vi编辑器使用
linux·后端·操作系统
何中应2 天前
Linux进程无法被kill
linux·后端·操作系统
何中应2 天前
rm-rf /命令操作介绍
linux·后端·操作系统
何中应2 天前
Linux常用命令
linux·操作系统
葛立国2 天前
从 / 和 /dev 说起:Linux 文件系统与挂载点一文理清
linux