实验环境
1)两台主机
linux单网卡 ----> ens160:1.1.1.55(内网主机)
linux双网卡 ----> ens160:192.168.0.55 (外网主机)==>可以连接windows网络
ens192:1.1.1.65(内网主机)
2)配置软件仓库
1.什么是火墙
防火墙是指设置在不同网络(如可信任的企业内部网和不可信的公共网)或网络安全域之间的一系列部件的组合。
它可以通过监测、限制、更改跨越防火墙的数据流,尽可能地对外部屏蔽网络内部的信息、结构和运行状况,以此来实现网络的安全保护。
在逻辑上,防火墙是一个分离器,一个限制器,也是一个分析器,有效地监控了内部网和Internet之间的任何活动,保证了内部网络的安全。
- 防火墙机制:netfliter
Netfilter/IPTables是Linux2.4.x之后新一代的Linux防火墙机制,是linux内核的一个子系统。Netfilter采用模块化设计,具有良好的可扩充性。其重要工具模块IPTables从用户态的iptables连接到内核态的Netfilter的架构中,Netfilter与IP协议栈是无缝契合的,并允许使用者对数据报进行过滤、地址转换、处理等操作。
2.火墙管理工具切换
iptables
- iptables策略记录文件
/etc/sysconfig/iptables - 永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save
dnf install iptables-services -y 安装iptable插件
systemctl disable --now firewalld
systemctl mask firewalld
systemctl enable --now iptables.service
systemctl status iptables.service
firewalld
- rhel8中默认使用的是firewalld
systemctl disable --now iptables.service
systemctl mask iptables.service
systemctl unmask firewalld
systemctl enable --now firewalld
systemctl status firewalld.service
3.火墙默认策略
默认策略中的5条链
input 输入
output 输出
forward 转发
postrouting 路由之后
prerouting 路由之前
默认的3张表
filter 经过本机内核的数据(input output forward)
nat 不经过内核的数据(postrouting,prerouting,input,output)
mangle 当filter和nat表不够用时使用(input output forward postrouting,prerouting,)
4.iptables命令的使用
iptables
-t 指定表解析
-n 不做解析
-L 查看
-A 添加策略
-p 协议
--dport 目的地端口
-s 来源
-j 动作
ACCEPT 允许
DROP 丢弃
REJECT 拒绝
SNAT 源地址转换
DNAT 目的地地址转化
-N 新建链
-E 更改链名称
-X 删除链
-D 删除规则
-I 插入规则
-R 更改规则
-P 更改默认规则
iptables -nL
iptables -F
service iptables save
iptables -t filter -L
iptables -t filter -A INPUT -j ACCEPT
iptables -t filter -A INPUT -s 192.168.0.24 -j REJECT
iptables -t filter -I INPUT 3 -s 192.168.0.25 -j REJECT
iptables -t filter -I INPUT -s 192.168.0.26 -j REJECT
iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.0.27 -j REJECT
iptables -N redhat
iptables -E redhat westos
iptables -D INPUT 3
iptables -X westos
iptables -R INPUT 1 -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
iptables -P INPUT ACCEPT
5.iptables火墙策略读取优化方案
iptables -A INPUT -m state --state ESTABLISHED,RELATED -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
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -j REJECT
iptables -nL
service iptables save
6.iptables中的数据转换(NAT)
iptables -t nat -nL
通过火墙如何让内网和外网通信?
在单网卡主机中:
vim /etc/sysconfig/network
添加:
1.1.1.65
nmcli connection reload
nmcli connection up ens160
测试:
ping 1.1.1.65
在双网卡主机中:
iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.55
sysctl -a | grep ip_forward 查看内核路由功能
vim /etc/sysctl.conf
写入:
net_ipv4_forward=1
sysctl -p 刷新内核路由功能
测试:
在单网卡主机中:
ping 192.168.0.55
ping 192.168.0.110
通过火墙如何让外网和内网通信?
iptables -t nat -A PREOUTING -i ens160 DNAT --to-dest 1.1.1.55
iptables-t -nat -nL
测试:
ssh -l root 192.168.0.55 连接192.168.0.55主机,通过查看IP可以看到,实际连接的主机IP为1.1.1.55
7.关于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
8.firewall的储存及管理
(1)firewalld的数据存储
/etc/firewalld 火墙配置目录
/lib/firewalld 火墙模块目录
(2)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.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
更改网络接口到指定域
9.firewalld的高级规则
firewall-cmd --direct --get-all-rules
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 192.168.0.110 -p tcp --dport 80 -j REJECT
firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -s 192.168.0.110 -p tcp --dport 80 -j REJECT 删除添加的高级规则
测试:
在windows主机中:
http://192.168.0.55
10.firewalld中的NAT
通过火墙如何让内网和外网通信?
在单网卡主机中:
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
测试:
在单网卡主机中:
ping 192.168.0.55
ping 192.168.0.110
通过火墙如何让外网和内网通信?
在单网卡主机中:
firewalld-cmd --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.55:toport=22
firewalld-cmd --reload
测试:
ssh -l root 192.168.0.55 远程登录192.168.0.55,登录后通过查看IP为1.1.1.55