双网卡主机环境配置:
单网卡主机配置:
一、火墙介绍
netfilter
内核上有个安全插件netfilter(访问控制列表),这个列表里有许多详细的规则,当对这个规则允许或拒绝时,可以控制其他主机是否能访问,极大的提高了安全性。
iptables
管理netfilter的工具,通过iptables往netfilter表格里面写网络安全策略
iptables|firewalld
对iptables进行管理,用iptables / firewalld手段来写网络安全策略
二、火墙管理工具切换
firewalld----->iptables
dnf install iptables-services-1.8.4-10.el8.x86_64 -y
systemctl disable --now firewalld.service
systemctl mask firewalld.service
systemctl enable --now iptables.service
iptales -------> fiewalld
systemctl disable --now iptables.service
systemctl mask iptables.service
systemctl unmask firewalld.service
systemctl enable --now firewalld.service
三、iptables的使用及火墙默认策略
-
/etc/sysconfig/iptables
iptables 策略记录文件
- 永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save
1.默认策略中的5条链
~ |
~ |
input |
输入 |
output |
输出 |
forward |
转发 |
postrouting |
路由之后 |
prerouting |
路由之前 |
2.默认的3张表
~ |
~ |
~ |
filter |
经过本机内核的数据(input output forward) |
控制访问我主机的数据 |
nat |
不经过内核的数据(postrouting,prerouting,input,output) |
经过我主机作地址转换的数据 |
mangle |
当filter和nat表不够用时使用(input output forward postrouting,prerouting,) |
对于以上俩表的过滤方式进行补充说明 |
3.iptables命令
~ |
~ |
-t |
指定表名称 |
-n |
不做解析 |
-L |
查看 |
-A |
添加策略 |
-p |
协议 |
–dport |
目的地端口 |
-s |
来源 |
-j |
动作—ACCEPT #允许 DROP #丢弃 REJECT #拒绝 SNAT #源地址转换 DNAT #目的地地址转换 |
-N |
新建链 |
-E |
更改链名称 |
-X |
删除链 |
-D |
删除规则 |
-I |
插入规则 |
-R |
更改规则 |
-P |
更改默认规则 |
iptables -nL #查看所有表
iptables -t filter -L #查看filter表
iptables -t filter -nL #不解析查看filter表
iptables -t filter -F #清除filter表信息(临时清除,重启iptables服务后仍有信息)
service iptables save #保存更改
iptables-save > /etc/sysconfig/iptables #保存更改
iptables -A INPUT -j ACCEPT #建立内容
iptables -D INPUT 1 #删除
iptables -A INPUT -j REJECT #建立所有都无法接入主机
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #指定端口22可以接入
iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT #插入顺序第一条
iptables -R INPUT 1 -s 172.25.51.0/24 -p tcp --dport 22 -j ACCEPT #允许指定网段
iptables -P INPUT DROP #更改默认规则为DROP
iptables -N jiajiren #新建链
iptables -E jiajiren jiajirenlove #更改链名称
iptables -X jiajirenlove #删除链
4.数据包状态
~ |
~ |
RELATED |
建立过连接的 |
ESTABLISHED |
正在连接的 |
TABLISHED |
正在连接的 |
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 22 -j ACCEPT #允许所有主机都可通过22端口[ssh服务]进入此主机
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT #允许所有主机都可通过80端口[httpd服务]进入此主机
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT #允许所有主机都可通过53端口[dns服务]进入此主机
iptables -A INPUT -m state --state NEW -j REJECT #拒绝所有主机接入
service iptables save #保存
5.iptables中的NAT
(1)SNAT(源地址转换)
iptables -t nat -nL #查看nat表
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.151 #
sysctl -a | grep ip_forward #检测内核路由功能,双网卡的俩网卡能通信的原因是内核是具备路由功能的,开了之后,双网卡的处于不同网段的ip才可以互相通信
vim /etc/sysctl.conf
///
net.ipv4.ip_forward=1
///
sysctl -p #功能立即开启
做了源地址转换后,172.25.51网段的主机也可以和172.25.254网段的主机通信
注:单网卡主机172.25.51网段的主机网关为双网卡主机中的172.25.51.100
(2)DNAT(目的地地址转化)
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 172.25.51.200
做了目的地地址转换后,登陆双网卡172.25.254.151主机,ip地址显示的会是单网卡主机172.25.51.200的ip
四、firewalld
1.firewalld的开启
systemctl disable --now iptables.service
systemctl unmask firewalld.service
systemctl enable --now firewalld
systemctl mask iptables.service
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 |
实验
dnf install httpd -y
systemctl enable --now httpd
echo hello > /var/www/html/index.html
cat /var/www/html/index.html
(1)当默认域设定为trusted:
真机和b主机都可以访问且连接
(2)当默认域设定为public:
都不能访问但ssh服务可以连接
(3)当默认域设定为block:
不能做任何操作
3.关于firewalld的设定原理及数据存储
- 火墙当前域的设定状态文件
/etc/firewalld/firewalld.conf
- firewalld服务将域的工作方式设定完成在
/usr/lib/firewalld
当中有已经封装好的域的配置文件
- 火墙的应用模块在
/usr/lib/firewalld/services
4.firewalld的管理命令firewalld的管理命令
firewall-cmd --state #查看火墙状态
firewall-cmd --list-all #查看默认域中的火墙策略
firewall-cmd --get-active-zones #查看当前火墙中生效的域
firewall-cmd --get-default-zone #查看默认域
firewall-cmd --set-default-zone=trusted #设定默认域
firewall-cmd --list-all --zone=dmz #查看指定域的火墙策略
firewall-cmd --get-services #查看所有可以设定的服务
firewall-cmd --permanent --add-service=westos #添加服务
firewall-cmd --reload #重新加载服务
firewall-cmd --permanent --remove--service=westos #移除服务
firewall-cmd --permanent --add-source 172.25.51.0/24 --zone=trusted #指定数据来源访问指定域
firewall-cmd --permanent --remove-source 172.25.51.0/24 --zone=trusted #删除自定域中的数据来源
firewall-cmd --permanent --remove-interface=ens3 --zone=public #删除指定域的网络接口
firewall-cmd --permanent --add-interface=ens3 --zone=trusted #添加指定域的网络接口
firewall-cmd --permanent --change-interface=ens3 --zone=public #更改网络接口到指定域
自己编写一个服务(firewalld的原理):
当指定数据来源访问指定域172.25.51.0/24:
当删除添加指定域的网络接口:
5.firewalld的高级规则
firewall-cmd --direct --get-all-rules #查看高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.51 -p tcp --dport 80 -j REJECT #除了254.51主机以外其他都拒绝
firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 ! -s 172.25.254.51 -p tcp --dport 80 -j REJECT #删除
6.firewalld中的NAT
(1)SNAT
firewall-cmd --permanent --add-masquerade #开启地址伪装
firewall-cmd --reload
此时51.200的网段主机也可以访问254.51网段的主机
(2)DNAT
firewall-cmd --permanent --add-masquerade #必须在开启地址伪装的基础上
firewall-cmd --add-forward-port=port=22:proto=tcp:toaddr=172.25.51.200 #将登陆本台主机的地址转换到200的地址上去
firewall-cmd --reload
firewall-cmd --remove-forward-port=port=22:proto=tcp:toaddr=172.25.51.200 #删除