实验环境:
三台主机
1.双网卡主机 添加网卡
添加网址 192.168.4.212
172.25.254.212
2.单网卡主机
添加网址 192.168.4.112
1.火墙介绍
1.netfilter
2.iptables
3.iptables|firewalld
2.火墙的工具切换
在rhel8中默认使用的是firewalld
iptables ##用来管理内核是那个的插件,只可开启一个
iptables ##可以用iptables和firewalld来管理
-
firewalld----->iptables ##切换
systemctl disable --now firewalld ##关闭firewalld
systemctl mask firewalld ##锁定firewalld
dnf install iptables-services -y ##安装iptables软件
systemctl enable --now iptables ##开启iptables管理
iptables -nL ##查看iptables策略
-
iptables----->firewalld ##iptables切换至firewalld
systemctl stop iptables ##关闭iptables服务
systemctl disable iptables
systemctl mask iptables
systemctl unmask firewalld ##解锁firewalld服务
systemctl enable --now firewalld ##开启firewalld服务
firewall-cmd --list-all ##查看firewalld策略
3.iptables的使用
火墙策略的永久保存
/etc/syscofig/iptables ##iptables 策略记录文件
firewalld
1.firewalld的开启
systemctl stop iptables
systemctl disable iptables
sysytemctl 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-clientssamba-client dhcp-client
external ##ipv4网络地址伪装转发 sshd
–permanent ##永久设定
-
test:
firewall-cmd --get-default-zone ##查看默认域
firewall-cmd --list-all-zones ##查看所有域
firewall-cmd --get-active-zone ##查看可用域
firewall-cmd --set-default-zone=trusted ##修改域为trusted
firewall-cmd --list-all ##查看可使用服务
-
dnf install httpd -y ##安装dhcp
systemctl enable --now httpd ##开启httpd
firefox 172.25.254.212 ##访问httpd
3.关于firewalld的设定原理及存储数据
/etc/firewalld ##火墙配置目录
/lib/firewalld ##火墙模块目录
4.firewalld的管理命令
-
firewall-cmd --state ##查看火墙状态
firewall-cmd --list-all ##查看当前火墙状态
firewall-cmd --list-all --zone=trusted ##查看指定域trusted的状态
##查看默认域
firewall-cmd --get-services ##查看系统当中可允许的所有服务名称
firewall-cmd --get-services | grep http ##查看http服务
修改服务文件名,服务不变
可通过名字设定端口
firewall-cmd --add-service=http ##(临时添加,刷新无)添加服务即改即生效
firewall-cmd --permanent --add-service=http
firewall-cmd --reload ##永久添加服务,非即改即生效,需更新,更改配置文件
firewall-cmd --permanent --remove-services=http ##删除服务永久删除,删除完需更新
/etc/httpd/conf/httpd.conf ##服务端口设定
firewall-cmd --permanent --add-port=8080/tcp ##添加服务与端口设定
firewall-cmd --permanent --remove-port=8080/tcp ##取消端口设定
-
ip设定
##设置ip与网络接口
firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block
firewall-cmd --reload
firewall-cmd --list-all --zone=block ##将172网段加入黑名单
firewall-cmd --permanent --remove-source=172.25.254.0/24 --zone=block
firewall-cmd --reload
firewall-cmd --list-all --zone=block ##删除172网段黑名单
-
网络端口
firewall-cmd --permanent --remove-service=http ##关闭http
firewall-cmd --reload
firewall-cmd --list-all ##查看服务状态
内部网路全部开放
firewall-cmd --permanent --remove-interface=ens160 --zone=public
firewall-cmd --reload ##删去域public
firewall-cmd --permanent --add-interface=ens160 --zone=trusted ##添加域ens160trusted
firewall-cmd --list-all --zone=trusted
firewall-cmd --permanent --change-interface=ens160 --zone=public ##修改ens3域为public
firewall-cmd --list-all --zone=public
-
火墙的内部结构
nat表中都不经过内核
filter 经过内核
mangle 当nat与filter不够用时
firewall 高级规则
-
##指定一个用户不可访问(设定input)
##查看高级规则
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 192.168.1.212 -j REJECT ##添加规则 input 指定操作 -p 协议 -s ip -dport ##端口-j拒绝
firewall-cmd --reload ##加载
firewall-cmd --direct --get-all-rules ##查看高级规则
访问服务器
firewall-cmd --permanent --add-masquerade ##打开地址隐藏功能
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 ##打开内核路由功能(两者才可通信)
如果未开启
##编写内核参数文件(使之成为路由器)
-
客户端1:
添加网关
临时添加
ip route add default via 192.168.1.100
-
登录客户端2:172网段登录192网段
w -i ##查看链接
目的地址转接
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=192.168.1.212
iptables管理方式
-
火墙策略的永久保存
/etc/sysconfig/iptables ##iptables策略记录文件
永久保存策略
iptables-save > /etc/sysconfig/iptables
sevice iptables save -
切换至iptales
systemctl disable --now firewalld
systemctl mask firewalld
dnf install iptables-services
systemctl enable --now iptables
iptables -nL
##查看服务 n 不做解析 L 列举
iptables -F
iptables-save > /etc/sysconfig/iptables
service iptables save
systemctl restart iptables.service ##永久保存策略规则
iptables -t nat -nL ##nat列表查看 - t 指定表
iptables -t filter -nL
iptables -t mangle -nL ##指定列表
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j REJECT
##添加协议 -j 动作 -p 协议 -dport 目的地端口
##协议向后写(施行匹配规则)
iptables -I INPUT 2 -s 172.25.254.112 -p tcp --dport 22 -j REJECT ##不允许250访问,此策略不读 -I 插入 INPUT 1写在第一条
-
DROP ##丢弃
REJECT ##拒绝
ACCEPT ##允许
SNAT ##源地址转换
DNAT ##目的地址转换 -
iptables -N WESTO ##新建链
iptables -E WESTO WESTOS ##修改链
iptables -X WESTOS ##删除链
iptables -D INPUT 3 ##删除第三行
iptables -R INPUT 1 -s 172.25.254.100 -p tcp --dport 22 -j REJECT ##修改规则
iptables -P INPUT DROP
iptables -P INPUT ACCEPT ##修改默认规则
-
只允许smb,iscsi,sshd服务通过
对应端口
3260 ##iscsi
139,445 ##smb
22 ##sshd
test:
-
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 ##允许tcp协议端口22sshd发出的策略通过
iptables -A INPUT -m state --state NEW -p tcp --dport 139 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 445 -j ACCEPT ##允许 tcp协议139 445端口smb发出的信息通过
iptables -A INPUT -m state --state NEW -p tcp --dport 3260 -j ACCEPT ##允许3260iscsi服务通过
iptables -A INPUT -m state --state NEW -j REJECT ##其余都拒绝
-
PREROUTING ##路由前
POSTROUTING ##路由后
INPUT ##输入
OUTPUT ##输出
FORWARD ##转发 -
iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source
172.25.254.100 ##把由172发出的信息地址都伪装做172.25.254.100
##连接,由172.25.254.100连接
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 192.168.1.100 ##将进来的信息都转至192网段
iptables -t nat -D POSTROUTING 1 ##删除链规则