欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 【Linux】的火墙管理及优化

【Linux】的火墙管理及优化

2025/6/21 9:51:30 来源:https://blog.csdn.net/FJ70940927/article/details/148053589  浏览:    关键词:【Linux】的火墙管理及优化

目录

iptables与firewalld服务

iptables的三表五链

iptables的安装和启用

iptables命令的格式及常用参数

命令格式

常用参数

编写规则示例

firewalld的域

firewalld的启用

firewalld-cmd命令的常用参数

firewalld的高级规则

firewalld的地址伪装与端口转发


iptables与firewalld服务

kernel space通过netfilter来管理火墙的策略

而iptables域firewalld是netfilter的管理工具

netfilter这个内核网络栈过滤框架的使用需要通过iptables或nftables来进行

与netfilter进行交互工具常用种类
iptables服务使用iptables交互,管理手段丰富,配置比较复杂。
firewalld服务使用nftables交互 ,配置类似windows火墙,功能模块度高,使用简单。


iptables的三表五链

iptables使用基于表的规则集

常用的有三个表

五条链chain

每个表控制的链表 


iptables的安装和启用

dnf install iptables-nft-services.noarch -y
systemctl disable --now firewalld.service
systemctl mask firewalld.service
systemctl enable --now iptables.service
systemctl status iptables.service


iptables命令的格式及常用参数

命令格式

iptables [-t 表名] 管理选项 [链名] [匹配条件] [-j 控制类型]

tips:
不指定表名时,默认指filter表
不指定链名时,默认指表内的所有链
除非设置链的默认策略,否则必须指定匹配条件
控制类型使用大写字母,其余均为小写
 

常用参数

指定与匹配条件

常用的管理选项

常用的控制类型

编写规则示例

查看iptables策略

iptables -nL
iptables -t nat -nL

清空指定表中的策略 

iptables -F -t filter 
#-F清空的策略是临时的,iptables的规则默认保存在内存中,如果需要永久保存需要把策略以字符的形式写入到配置文件/etc/sysconfig/iptables中
systemctl restart iptables.service
iptables -nL
cat /etc/sysconfig/iptables    #查看配置文件中以文字形式保存的策略
iptables -F
service iptables save        #将当前表中的策略以文字形式保存在配置文件中
cat /etc/sysconfig/iptables
iptables -F

 添加规则,禁止主机访问,未禁止的能访问

iptables -t filter -A INPUT -p tcp --dport 80 -s 192.168.44.30 -j REJECT
iptables -nL

测试

 在编写规则“ ! ”为取反

iptables -t filter -A INPUT -p tcp --dport 80 ! -s 192.168.81.10 -j REJECT

测试

允许本机回环接口访问

iptables -t filter -A INPUT -i lo -j ACCEPT

 插入规则到链中

iptables -t filter -I INPUT 1 -p tcp --dport 80 -s 192.168.81.10 -j REJECT

删除表中指定链的规则

iptables -t filter -D INPUT 2

 修改表中指定链的规则

iptables -t filter -R INPUT 1 -p tcp --dport 80 ! -s 192.168.44.30 -j REJECT

在指定表中创建一个新的链

iptables -N fjw

重命名用户自定义链中的名称

iptables -E fjw FJW

删除用户自定义链

iptables -X FJW

snat地址转换

第一步,先给与不同网段的服务器的测试机添加路由

第二步,打开使用iptables火墙的服务器的内核路由功能

第三步,在nat表中编写规则

测试

snat地址转换

iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 192.168.44.30

测试


firewalld的域

firewalld采用基于域的规则集


firewalld的启用

firewalld默认是开启

由于做完iptables的实验要,重新开启

systemctl disable --now iptables.service
systemctl enable --now  firewalld.service


firewalld-cmd命令的常用参数

firewall-cmd --reload    #添加端口转发后+permanent参数要reload后才能生效不然不生效

firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=trusted
firewall-cmd --change-interface=ens192

firewall-cmd --get-zones
firewall-cmd --get-active-zones

firewall-cmd --get-services
cd /lib/firewalld/services/

firewall-cmd --list-all
firewall-cmd --list-all-zones

firewall-cmd --add-source=192.168.44.30/24 --zone=trusted
firewall-cmd --get-active-zones
firewall-cmd --remove-source=192.168.44.30/24 --zone=trusted

firewall-cmd --add-service=http
firewall-cmd --add-port=80/tcp


firewalld的高级规则

vim /etc/firewalld/firewalld.conf
systemctl restart firewalld.service
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 ! -s 192.168.81.10 -j ACCEPT
firewall-cmd --direct --get-all-rules

firewalld的地址伪装与端口转发

要想开启火墙功能的服务器充当路由器就要开启地址伪装

firewall-cmd --permanent --add-masquerade
firewall-cmd --reload

端口转发

firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=192.168.44.30
firewall-cmd --reload    #添加端口转发后+permanent参数要reload后才能生效不然不生效

测试 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词