CentOS个版本的防火墙应用方法

本文介绍了在CentOS系统中如何使用firewall-cmd和iptables进行IP屏蔽、端口开放与关闭的操作,以及如何查看和管理SELinux的状态,包括临时和永久关闭方法。同时提供了开启和关闭防火墙的命令,并强调了规则生效的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

屏蔽IP

firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=111.1.25.122 reject"

修改

firewall-cmd --permanent --change-rich-rule="rule family=ipv4 source address=111.1.25.122 reject"

删除

firewall-cmd --permanent --remove-rich-rule="rule family=ipv4 source address=111.1.25.122 reject"

查看屏蔽结果

firewall-cmd --list-rich-rules

热加载才能生效

sudo firewall-cmd --reload

开放端口

永久的开放需要的端口

添加:sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.96" port protocol="tcp" port="10050" accept"

sudo firewall-cmd --zone=public --add-port=8082/tcp --permanent

删除:

firewall-cmd --zone=public --remove-port=80/tcp --permanent

端口范围30000-30999:firewall-cmd --zone=public --add-port=30000-30999/tcp --permanent

查看已开放的端口

sudo firewall-cmd --zone=public --list-ports   

删除: firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"

sudo firewall-cmd --reload  

之后检查新的防火墙规则

sudo firewall-cmd --list-all 

centos 6

iptables -I INPUT 3 -s 192.168.0.96  -m state --state NEW  -p TCP --dport 10050 -j ACCEPT  插入在第三条,千万别默认插入在最后一条,导致一直不生效

iptables -A INPUT  -p tcp -m state --state NEW -m tcp --dport 12345 -j ACCEPT

service iptables save,记得保存

在root用户下执行上面2行命令后,重启iptables, service iptables restart,查看生效iptables -L -n

查看防火墙

~]# iptables -L -n

CentOS7:

    ~]# sudo systemctl disable firewalld.service

    ~]# sudo systemctl stop firewalld.service

CentOS6

    ~]# sudo service iptables stop

    ~]# sudo chkconfig iptables off

临时关闭setenforce 0

永久关闭selinux:

[root@localhost ~]# vim /etc/sysconfig/selinux

SELINUX=enforcing 改为 SELINUX=disabled

在root状态下,Redhat7或者centos7以上查看防火墙状态。

临时关闭防火墙命令。重启电脑后,防火墙自动起来。

systemctl stop firewalld

永久关闭防火墙命令。重启后,防火墙不会自动启动。

 systemctl disable firewalld

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

打开防火墙命令。

systemctl enable firewalld

root

关闭防火墙:

systemctl stop firewalld.service

放行端口:firewall-cmd --add-port=8080/tcp,此处需要注意cmd和--之间有空格

service iptables stop

sercice sshd start  SSH开启

sudo passwd root修改密码

查看SELinux状态:

1、/usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态

SELinux status:                 enabled

2、getenforce                 ##也可以用这个命令检查

关闭SELinux:

1、临时关闭(不用重启机器):

setenforce 0                  ##设置SELinux 成为permissive模式

                              ##setenforce 1 设置SELinux 成为enforcing模式

2、修改配置文件需要重启机器:

修改/etc/selinux/config 文件

将SELINUX=enforcing改为SELINUX=disabled

重启机器即可

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值