默认策略中的5条链
input ##输入
output ##输出
forward ##转发
postrouting ##路由之后
prerouting ##路由之前
默认的3张表
filter ##经过本机内核的数据(input output forward)
nat ##不经过内核的数据(postrouting,prerouting,input,output)
mangle ##当filter和nat表不够用时使用(input output forward postrouting,prerouting,)
环境搭建:
westosa ens3 172.25.254.120 24
ens9 1.1.1.1 24westosb ens3 1.1.1.1 24
1.火墙介绍
1.netfilter
2.iptables
3.iptables|firewalld
2.火墙管理工具切换
在rhel8中默认使用的是firewalld #火墙只能生效一个
firewalld----->iptables
dnf install iptables-services -y
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld mask锁unmask解锁
systemctl enable --now iptables
iptales -------> fiewalld
dnf install firewalld -y
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable --now firewalld
3. iptables
#火墙策略的暂时清除
iptables -F
systemctl restart iptables #重启服务会恢复#火墙策略的永久保存#
/etc/sysconfig/iptables ##iptables 策略记录文件永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save
火墙默认策略
默认策略中的5条链
input ##输入
output ##输出
forward ##转发
postrouting ##路由之后
prerouting ##路由之前
默认的3张表
filter ##经过本机内核的数据(input output forward)
nat ##不经过内核的数据(postrouting,prerouting,input,output)
mangle ##当filter和nat表不够用时使用(input output forward postrouting,prerouting,)
iptables命令
iptables
-t ##指定表名称
-n ##不做解析
-L ##查看
-A ##添加策略
-p ##协议
--dport ##目的地端口
-s ##来源
-j ##动作
ACCEPT ##允许
DROP ##丢弃
REJECT ##拒绝
SNAT ##源地址转换
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 新建的指定端口允许
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW ! -s 172.25.254.20 -p tcp --dport 22 -j ACCEPT 除了172.25.254.20号主机的22端口服务外都不允许
iptables -A INPUT -m state --state NEW -j REJECT 新建的不被接受
service iptables save
nat表中的dnat snat
首先开服务:
sysctl -a | grep ip_forward 查看服务状态
vim /etc/sysctl.conf 添加:net.ipv4.ip_forward = 1 #(1表开启,0表关闭)snat
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.120
1.1.1.220主机可以连接172.25.254.20 w -i显示的是120号主机
dnat
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 1.1.1.220
20号主机连接172.25.254.120会转向1.1.1.220
4.firewalld
firewalld图形配置:
dnf whatprovides */firewall-config #查询firewalld-config软件包
dnf install firewall-config-0.8.0-4.el8.noarch -y 3安装图形软件包
firewall-config #开启火墙图形
1 firewalld的开启
systemctl stop iptables
systemctl disable iptables
systemctl mask iptablessystemctl 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 --permanent --add-service=cockpit ##添加服务
firewall-cmd --reload
firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定数据来源访问指定域
firewall-cmd --reload
firewall-cmd --permanent --remove-source=172.25.254.0/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.20 -p tcp --dport 80 -j REJECT
add添加规则
INPUT限制输入
1 第一条规则
!除了
-s 172.25.254.20指定ip
-p tcp 协议
--dport 80 端口
-j REJECT 拒绝
remove删除规则
表:filter (input forward output)
nat (input prerouting postrouting output)
mangle (input prerouting forward postrouting output)
6 firewalld中的NAT
环境:
a-双网卡172.25.254.120;1.1.1.120
b-单网卡1.1.1.220
c-真机172.25.254.20
SNAT
firewall-cmd --permanent --add-masquerade 双网卡中添加地址伪装服务
firewall-cmd --reload
vim /etc/sysconfig/network 单网卡中添加a的网关 GATEWAY=1.1.1.120
ssh -l root 172.25.254.20 单网卡连接真机
w -i 真机显示是172.25.254.120连接
DNAT
主机连接172.25.254.120转接到1.1.1.220
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.220
firewall-cmd --reload