本文共 7549 字,大约阅读时间需要 25 分钟。
在rhel8中默认使用的是firewalldfirewalld----->iptablesdnf install iptables-services -ysystemctl stop firewalldsystemctl disable firewalld systemctl mask firewalld systemctl enable --now iptablesiptales -------> fiewalld dnf install firewalld -ysystemctl stop iptablessystemctl disable iptablessystemctl mask iptablessystemctl enable --now firewalld
#火墙策略的永久保存#
/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 ##新的[root@node1 named]# iptables -nL ##默认是filter表 n表示不做解析[root@node1 named]# iptables -t nat -nL[root@node1 named]# iptables -t mangle -nL[root@node1 named]# iptables -t filter -L[root@node1 named]# vim /etc/sysconfig/iptables[root@node1 named]# service iptables save[root@node1 named]# iptables -F ##清理 [root@node1 named]# service iptables save ##永久清理[root@node1 named]# systemctl restart iptables.service [root@node1 named]# iptables -nL[root@node1 network-scripts]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT[root@node1 network-scripts]# iptables -A INPUT -j REJECT[root@node1 network-scripts]# iptables -nL[root@node1 network-scripts]# iptables -D INPUT 2 ##删除第2条[root@node1 network-scripts]# iptables -I INPUT 2 -p tcp --dport 22 -j ACCEPT ##ping不通 但是ssh能连接
[root@node1 network-scripts]# iptables -R INPUT 2 ! -s 172.25.254.250 -p tcp --dport 22 -j ACCEPT ##不允许250访问[root@node1 network-scripts]# iptables -N westos ##新建一个链[root@node1 network-scripts]# iptables -E westos REDHAT ##更改链名称[root@node1 network-scripts]# iptables -E REDHAT westos[root@node1 network-scripts]# iptables -X westos ##删除[root@node1 network-scripts]# iptables -P INPUT DROP[root@node1 network-scripts]# iptables -P INPUT ACCEPT ##更改默认规则
[root@node1 named]# iptables -nL[root@node1 named]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT[root@node1 named]# iptables -A INPUT -i lo -m state --state NEW -j ACCEPT[root@node1 named]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT ##dns53 httpd 80 sshd 22 [root@node1 named]# iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT ##[root@node1 named]# iptables -A INPUT -m state --state NEW -j ACCEPT[root@node1 named]# iptables -nL[root@node1 named]# iptables -D INPUT 5[root@node1 named]# iptables -A INPUT -m state --state NEW -j REJECT[root@node1 named]# iptables -nL
1.设定建立和正在连接的数据包的火墙策略为允许iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT2.设定本机的回环接口允许新的数据包iptables -A INPUT -m state --state NEW -i lo -j ACCEPT3.对于新的指定服务的数据包添加火墙策略:设定为允许iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPTiptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPTiptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT4.添加火墙策略。设定除指定ip的所有主机都可以进行的服务iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPTiptables -A INPUT -m state --state NEW -j REJECTservice iptables save %火墙策略永久保存注意:iptables命令添加策略时优先级是从上至下nat表中的dnat snatsnatiptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.20 ##外网dnatiptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.254.30 ## 内网
systemctl stop iptables systemctl disable iptablessystemctl mask iptables systemctl unmask firewalldsystemctl enable --now firewalld
[root@node1 ~]# sysctl -a | grep ip_forwardnet.ipv4.ip_forward = 1trusted ##接受所有的网络连接home ##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-clientwork ##工作网络 ssh ipp-client dhcp-clientpublic ##公共网络 ssh dhcp-clientdmz ##军级网络 sshblock ##拒绝所有drop ##丢弃 所有数据全部丢弃无任何回复internal ##内部网络 ssh mdns ipp-client samba-client dhcp-clientexternal ##ipv4网络地址伪装转发 sshd
/etc/firewalld ##火墙配置目录
/lib/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 ##更改网络接口到指定域
firewall-cmd --direct --get-all-rules ##查看高级规则 firewall-cmd --direct --add-rule ipv4 filter INPUT 0 ! -s 172.25.254.250 -p tcp --dport 22 -j REJECT ##设定只有172.25.254.250主机可以连接本机。
SNATfirewall-cmd --permanent --add-masqueradefirewall-cmd --reload DNATfirewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30firewall-cmd --reload
[root@node1 firewalld]# firewall-cmd --permanent --direct --get-all-rules [root@node1 firewalld]# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.250 -p tcp --dport 22 -j REJECT ##250 ssh no [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd --direct --get-all-rules[root@node1 firewalld]# firewall-cmd --list-all[root@node1 firewalld]# firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -s 172.25.254.250 -p tcp --dport 22 -j REJECT[root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd --direct --get-all-rules[root@node1 firewalld]# firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.212:toport=22 ##目的地址转换 [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd --list-all[root@node1 firewalld]# firewall-cmd --permanent --add-masquerade ##原地址转换 [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd --list-all
Cockpit 是红帽开发的网页版图像化服务管理工具,优点是无需中间层,且可以管理多种服务。根据其项目主站描述,Cockpit 有如下特点: 从易用性考虑设计,方便管理人员使用,而不是仅仅的终端命令按钮化。 不会打乱已有终端或脚本服务配置,通过 Cockpit 启用的服务可以在终端停止,脚本运行的错误亦会被 Cockpit 捕获。 支持一次性管理多个服务,实现自动化和批处理。
转载地址:http://fnhzi.baihongyu.com/