Linux之firewalld火墙策略

1. 防火墙介绍

  1. netfilter
  2. iptables
  3. iptables|firewalld

 
 
2. 火墙管理工具切换

在rhel8中默认使用的是firewalld

firewalld----->iptables

安装iptables
 

dnf install iptables-services -y 

停止firewalld

systemctl stop firewalld 

systemctl disable firewalld 

systemctl mask firewalld 

启动iptables

systemctl enable --now iptables

 

 

iptales -------> fiewalld
 

systemctl stop iptables 

systemctl disable iptables 

systemctl mask iptables 

systemctl enable --now firewalld

 

3. iptables的使用

iptables 策略记录文件:

/etc/sysconfig/iptables

永久保存策略:
 

iptales-save > /etc/sysconfig/iptables 

service iptables save

4. 火墙默认策略

默认策略中的5条链

  1. input ##输入
  2. output ##输出
  3. forward ##转发
  4. postrouting ##路由之后
  5. prerouting ##路由之前

默认的3张表

  1. filter ##经过本机内核的数据(input output forward)
  2. nat ##不经过内核的数据(postrouting,prerouting,input,output)
  3. mangle ##当filter和nat表不够用时使用(input output forward postrouting prerouting)

iptables命令

iptables

  • -t ##指定表名称
  • -n ##不做解析
  • -L ##查看
  • -A ##添加策略
  • -p ##协议
  • --dport ##目的地端口
  • -s ##来源
  • -j ##动作
  1. ACCEPT ##允许
  2. DROP ##丢弃
  3. REJECT ##拒绝
  4. SNAT ##源地址转换
  5. DNAT ##目的地地址转换
  • -N ##新建链
  • -E ##更改链名称
  • -X ##删除链
  • -D ##删除规则
  • -I ##插入规则
  • -R ##更改规则
  • -P ##更改默认规则

 
 
数据包状态

  • RELATED         ##建立过连接的
  • ESTABLISHED ##正在连接的
  • NEW                 ##新的
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #建立过连接的和正在连接的允许
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT #本地的新的 允许
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT # tcp 80端口允许(http) 
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT #443端口允许(https)
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT #53端口允许(dns)
iptables -A INPUT -s 192.168.43.21 -m state --state NEW -p tcp --dport 22 -j ACCEPT #21主机 22端口允许(ssh)
iptables -A INPUT -m state --state NEW -j REJECT #全部拒绝
service iptables save      #保存到永久策略文件

 
 
前面第六条规则网段写错了,这里删除掉重新插入

测试:

在192.168.2.21端口ssh连接

换一台主机,192.168.2.10

 

nat表中的dnat snat (内核路由需要开启)

 内核路由状态(开启)

一台双网卡主机

两台不同网段主机

 

火墙策略未写入之前,无法与192.168.2.21主机通信

snat

iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.2.20

 再次尝试

dnat

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

在192.168.2.21主机ssh登录192.168.2.20

 实际登录的是172.25.254.10主机

 

5. firewalld

1. firewalld的开启

systemctl stop iptables

systemctl disable iptables

systemctl mask iptables 

systemctl unmask firewalld

systemctl enable --now firewalld 

 

 

2. 关于firewalld的域

  • trusted   ##接受所有的网络连接
  • home     ##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-client
  • work      ##工作网络 ssh ipp-client dhcp-client
  • public    ##公共网络 ssh dhcp-client
  • dmz       ##军级网络 ssh
  • block     ##拒绝所有
  • drop      ##丢弃 所有数据全部丢弃无任何回复
  • internal ##内部网络 ssh mdns ipp-client samba-client dhcp-client
  • external ##ipv4网络地址伪装转发 sshd

 

3. 关于firewalld的设定原理及数据存储

  • /etc/firewalld ##火墙配置目录
  • /lib/firewalld ##火墙模块目录

4. firewalld的管理命令

firewall-cmd --state ##查看火墙状态

firewall-cmd --get-active-zones ##查看当前火墙中生效的域

firewall-cmd --get-default-zone ##查看默认域

firewall-cmd --list-all ##查看默认域中的火墙策略

firewall-cmd --list-all --zone=work ##查看指定域的火墙策略

firewall-cmd --set-default-zone=trusted  ##设定默认域

firewall-cmd --get-services ##查看所有可以设定的服务

firewall-cmd --permanent --remove-service=cockpit ##移除服务

firewall-cmd --reload 

firewall-cmd --permanent --add-source=172.25.254.20/24 --zone=block ##指定数据来源访问指定域

firewall-cmd --reload

firewall-cmd --permanent --remove-source=172.25.254.20/24 --zone=block ##删除自定域中的数据来源

firewall-cmd --permanent --remove-interface=ens224 --zone=public ##删除指定域的网络接口

firewall-cmd --permanent --add-interface=ens224 --zone=block ##添加指定域的网络接口

firewall-cmd --permanent --change-interface=ens224 --zone=public ##更改网络接口到指定域

 

 

 

 

 

 

5. firewalld的高级规则

firewall-cmd --direct --get-all-rules ##查看高级规则

firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.10/24 -p tcp -dport 22  -j ACCEPT

 

6. firewalld中的NAT

SNAT

firewall-cmd --permanent --add-masquerade 

firewall-cmd --reload 

 

DNAT

firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30

firewall-cmd --reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值