firewalld&iptables

本文介绍了Firewalld和iptables的配置方法,包括Firewalld的动态管理特性、常用命令及应用场景,以及iptables的安装配置、命令使用、规则配置等核心内容。

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

一. Firewalld

动态防火墙后台程序-firewalld,提供了一个动态管理的防火墙,用以支持网络“zones”,以分配对一个网络及其相关链接和界面一定程序的信任。它具备对ipv4和ipv6防火墙设置的支持,它支持以太网桥,并有分离运行时间和永久性配置选择。它还具备一个通向服务或者应用程序以直接增加防火墙规则的接口。

1. firewall-sysconfig        #图像化配置工具

yum install -y firewalld firewall-config    #安装图像化配置工具


2. 火墙策略

firewall-cmd --state                       

firewall-cmd --get-zones               

firewall-cmd --get-active-zones    

firewall-cmd --get-default-zone     


firewall-cmd --list-all

firewall-cmd --list-all --zone=trusted


firewall-cmd --list-all-zones    #列出所有


firewall-cmd --get-services

firewall-cmd --set-default-zone=trusted

firewall-cmd --get-default-zone


firewall-cmd --permanent --add-service=***

firewall-cmd --reload


firewall-cmd --permanent --add-source=172.25.254.150 --zone=trusted   #对172.25.254.150永久信任

firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=trusted

firewall-cmd --reload

 firewall-cmd --permanent --remove-source=172.25.254.150--zone=trusted   #移除

firewall-cmd --reload


firewall-cmd --add-interface=eth1 --zone=trusted     


重启火墙恢复默认


firewall-cmd --remove-interface=eth1 --zone=trusted


firewall-cmd --change-interface=eth1 --zone=public


3. 修改httpd火墙策略

firewall-cmd --permanent --add-service=http    #永久添加httpd服务

firewall-cmd --reload                                            #永久添加需重新加载

firewall-cmd --permanent --add-port=8080/tcp #添加tcp协议8080端口

firewall-cmd --reload                                            #永久添加需重新加载

vim /usr/lib/firewalld/services/http.xml                #修改火墙服务端口,rpm -ql firewalld  可查看火墙服务的相关服务信息

vim /etc/httpd/conf/httpd.conf                              #修改httpd服务端口

systemctl restart httpd.service                            #重启httpd服务


测试:


firewall-cmd --list-ports --zone=public                          #列出火墙服务端口

firewall-cmd --permanent --remove-port=8080/tcp    #删除火墙服务端口


注:

semanage port -l | grep http                              #查询服务接口

semanage port -a -t http_port_t -p tcp 6666  #添加服务接口


4. 修改sshd火墙策略

firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.150 -p tcp --dport 22 -j REJECT   #添加火墙规则,只允许150连接

firewall-cmd --direct --get-all-rules      #查看火墙规则

注:“!”表示否定,除什么之外 ;s表示来源;p表示协议;j表示动作

测试:


firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 ! -s 172.25.254.150 -p tcp --dport 22 -j REJECT    #移除规则

测试:


firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.150 -p tcp --dport 22 -j DROP   #添加火墙规则,除150可以链接,其余全部等待,没有任何回应

测试:


注:

小规则写在前,加数字表序号,由上到小逐条匹配,只要有一条匹配,匹配完成将不再读取后续规则

拓展:

三张表:filter;mangle;nat

五条链:input(输入);output(输出);prerouting(路由前);postrouting(路由后);forward(转换)

配置双网卡-路由前:

vim /etc/sysconfig/network-scripts/ifcfg-eth1     #配置网卡,eth1与eth0在不同网段

systemctl restart network


firewall-cmd --add-masquerade --zone=public

firewall-cmd --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254. 150    #访问本机,在路由前不经过内核,进行地址转换,直接转换为150主机


单网卡测试:

配置双网卡-路由后:

firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.25.254.170 masquerade'    #访问本机另一不同网段ip,路由后经过内核进行源地址转换,可以连接成功



单网卡测试:

ip与双网卡其中一块ip处于同一网段,通过连接不同网段的ip,连接成功


注:若ip可以ping通,但不能连接,使用以下方法进行恢复

sysctl -p

sysctl -a | grep ip_forward

vim /etc/sysctl.conf      #编辑net.ipv4.ip_forward = 1

二. iptables

1. 安装配置iptables

yum search iptables

yum install -y iptables-services.x86_64

systemctl stop firewalld.service

systemctl mask firewalld.service

systemctl start iptables.service

systemctl enable iptables.service


2. iptables的命令使用

iptables -nL                        #默认查看filter表,包含三条链

iptables -F                          #表示刷新,重启后将还原

service iptables save       #保存刷新,重启后不会还原


iptables -t filter -nL          #查看filter表

iptables -t nat -nL            #查看nat表

iptables -t mangle -nL    #查看mangle表


3. 配置规则

iptables -A INPUT -p tcp --dport 22 -j ACCEPT        #添加允许tcp协议22端口输入规则;"A"表示添加

iptables -A INPUT -i lo -j ACCEPT                              #添加允许环形网络接口输入规则

iptables -A INPUT -j REJECT                                      #添加拒绝所有输入规则

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT   #添加允许tcp协议8080端口输入规则

iptables -nL


iptables -D INPUT 4                                                      #删除第四条规则;"D"表示删除

iptables -I INPUT 3 -p tcp --dport 80 -j ACCEPT      #将允许tcp协议80端口输入规则插入到第三条;"I"表示插入

ip -nL


iptables -R INPUT 3 -p tcp --dport 8080 -j ACCEPT      #将第三条允许tcp协议80端口改为8080端口输入;"R"表示插入


iptables -P INPUT DROP             #修改默认执行动作为DROP


4. 配置链

iptables -N westos                      #新建链;"N"表示新建

iptables -nL


iptables -E westos WESTOS    #修改链;"E"表示修改

iptables -nL


iptables -X WESTOS                   #删除链;"X"表示删除

iptables -nL


5. 配置规则状态

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT   #添加ESTABLISHED,RELATED状态输入规则;""表示状态


iptables -A INPUT -m state --state NEW -i lo -j ACCEPT                          #添加允许环形网路接口状态NEW输入规则

iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT    #添加允许tcp协议端口80状态NEW输入规则

iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT    #添加允许tcp协议端口22状态NEW输入规则

iptables -A INPUT -m state --state NEW -j REJECT                                  #添加拒绝所有状态NEW输入规则


注:cat /etc/services | grep http可查看服务接口

双网卡-路由后:

配置不同网段ip

iptables -t nat -A POSTROUTING -j SNAT --to-source 172.25.254.170     #路由后经过内核,源地址转换170主机



单网卡测试:

连接不同网段的ip,经过源地址转换,连接成功



双网卡-路由前:

iptables -t nat -A PREROUTING -j DNAT --to-dest 192.168.1.112     #路由前不经过内核,直接进行地址转换,转换到192.168.1.112主机


单卡测试:


注:firewalld与iptables本质区别:

firewalld将配置存储在/usr/lib/firewalld/和/etc/firewalld/中的各种xml文件中

iptables在/etc/sysconfig/iptables中存储配置


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值