玩转Linux三大防火墙工具!

本文涉及的产品
云防火墙,500元 1000GB
简介: 玩转Linux三大防火墙工具!

一:iptables


精华:iptables和firewalld都不是防火墙,他们都只是管理防火墙的工具、服务而已!


重点:iptables防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,从而导致任何主机都无法访问我们的服务


1.常用操作

iptables -I INPUT -s 网段/掩码 -p tcp --dport 端口号 -j ACCEPT 
iptables -A INPUT-p tcp --dport 端口号 -j REJECT
iptables -L 
端口转发
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
查看已有的防火墙规则链:
iptables -L
清空规则链
iptables -F
删除用户自定义 chain 或者所有用户自定义chain (chain就是链的意思)
iptables -X
把 chain 或者所有 chain(当未指定 chain 名称时)的包及字节的计数器清空
iptables -Z

2.在centos系统下想要保存iptables的规则链需要安装iptables服务,因为centos下默认是firewalld服务没有iptables,所以需要手动安装iptables服务后重启服务才行 以下两个都可以保存iptables的配置

[root@b sysconfig]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@b sysconfig]#systemctl enable iptables 
[root@b sysconfig]# systemctl restart iptables

3.把所有input规则链的默认策略设置拒绝

iptables -P INPUT DROP

4.向INPUT链中添加允许ICMP流量进入的策略规则:

iptables -I INPUT -p icmp -j ACCEPT

5.删除INPUT规则链中刚刚加入的那条策略(允许ICMP流量)

iptables -D INPUT 1

6.将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有主机的流量:

iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j REJECT
iptables -L

7.向INPUT规则链中添加拒绝所有人访问本机12345端口的策略规则:

iptables -I INPUT -p tcp --dport 12345 -j REJECT
iptables -I INPUT -p udp --dport 12345 -j REJECT
iptables -L

8.向INPUT规则链中添加拒绝所有主机访问本机1000~1024端口的策略规则:

iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
iptables -L

二:Firewalld


精华:firewalld支持动态更新技术并加入了区域(zone)的概念


重点:区域就是我提前准备几套防火墙策略针对不通的场景来用,在firewalld中可以来回切换域,这样就满足工作生产中不同的需求啦!


1.查看firewalld服务当前所使用的区域


firewall-cmd --get-default-zone
public


2.把firewalld服务的当前默认区域设置为public

firewall-cmd --set-default-zone=public
success
firewall-cmd --get-default-zone 
public

3.启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎用):

firewall-cmd --panic-on
success
[root@linuxprobe ~]# firewall-cmd --panic-off
success

4.重点:流量转发命令格式为firewall-cmd --permanent --zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标IP地址>)*

firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.10
firewall-cmd --reload
success

5.我们可以在firewalld服务中配置一条规则,使其拒绝192.168.10.0/24网段的所有用户访问本机的ssh服务(22端口):

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"
success
firewall-cmd --reload
success


三:TCP wrappers


精华: TCP Wrappers服务的防火墙策略由两个控制列表文件所控制,用户可以编辑允许控制列表文件来放行对服务的请求流量,也可以编辑拒绝控制列表文件来阻止对服务的请求流量。


重点文件: 控制列表文件(/etc/hosts.allow) 拒绝控制列表文件(/etc/hosts.deny)


编写规则: 编写拒绝策略规则时,填写的是服务名称,而非协议名称; 建议先编写拒绝策略规则,再编写允许策略规则,以便直观地看到相应的效果。


1.只允许192.168.10.0网段能够访问sshd服务

vim /etc/hosts.deny
#
#hosts.deny This file contains access rules which are used to
#deny connections to network services that either use
#the tcp_wrappers library or that have been
#started through a tcp_wrappers-enabled xinetd.
#
#The rules in this file can also be set up in
#/etc/hosts.allow with a 'deny' option instead.
#
#See 'man 5 hosts_options' and 'man 5 hosts_access'
#for information on rule syntax.
#See 'man tcpd' for information on tcp_wrappers
sshd:

ssh 192.168.10.10
ssh_exchange_identification: read: Connection reset by peer

 vim /etc/hosts.allow
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
sshd:192.168.10.

ssh 192.168.10.10
The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.
ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.
root@192.168.10.10's password: 
Last login: Wed May 4 07:56:29 2017

小结:三者之间有着相似的地方也有不同的地方,没有必要把各种参数都背会,在实际工作做能够根据工作需求灵活的使用这些工具才是王道!


四、实操演练1.配置端口转发在系统 serverx 中配置端口转发 在 172.25.x.0/24 网络中的系统,访问 server1 的本地端口 5423 将被转发到 80 此设定时永久生效的


#1.允许172.25.4.0/24 网段的主机可以访问本机(添加到信任域)

[root@server4 ~]# firewall-cmd --permanent --add-source=172.25.4.0/24 --zone=trusted 
success

#重启火墙,注意:每此添加火墙策略都需要重启

[root@server4 ~]# systemctl restart firewalld [root@server4 ~]# firewall-cmd --list-all --zone=trusted

image.png


#2.端口转发

[root@server4 ~]# firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 1 -s 172.25.4.0/24 -p tcp --dport 5423 -j DNAT --to-dest :80
success
[root@server4 ~]# systemctl restart firewalld
[root@server4 ~]# firewall-cmd --direct --get-all-rules 
ipv4 filter INPUT 1 -s 192.168.0.0/24 -p tcp --dport 22 -j REJECT
ipv4 nat PREROUTING 1 -s 172.25.4.0/24 -p tcp --dport 5423 -j DNAT --to-dest :80

 **实验测试:**
```bash
[root@server4 ~]# yum install -y httpd
[root@server4 ~]# systemctl start httpd
[root@server4 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

输入:172.25.4.11 可访问到apahce的默认发布页面(默认使用的是80端口)

image.png


输入:172.25.4.11:5423 也可访问到apahce的默认发布页面,即说明5423端口转发到80端口


image.png


相关文章
|
13天前
|
安全 Linux Shell
四、Linux核心工具:Vim, 文件链接与SSH
要想在Linux世界里游刃有余,光会“走路”还不够,还得配上几样“高级装备”。首先是Vim编辑器,它像一把瑞士军刀,让你能在命令行里高效地修改文件。然后要懂“软硬链接”,软链接像个快捷方式,硬链接则是给文件起了个别名。最后,SSH是你的“传送门”,不仅能让你安全地远程登录服务器,还能用scp轻松传输文件,设置好密钥更能实现免-密登录,极大提升效率。
179 3
|
12天前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
84 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
28天前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
104 16
|
5月前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
3月前
|
缓存 监控 Linux
Linux系统性能调优技巧和相关工具
Linux 作为一种应用应展和系统服务的优选操作系统,在处理性能和端到端点评估上持有出色表现。但是,在处理进程或系统处于低效状态时,性能调优就显得十分重要。本文将探讨一些 Linux 系统性能调优的常用技巧,并介绍相关工具
92 1
Linux系统性能调优技巧和相关工具
|
3月前
|
Linux 数据安全/隐私保护 iOS开发
推荐Linux环境下效能优良的双向文件同步工具
综合上述条件,对于Linux环境下的双向文件同步需求,Unison 和 Syncthing 是两个非常出色的选择。它们都有良好的社区支持和文档资源,适用于不同规模的环境,从个人使用到商业部署。Unison 特别适合那些需要手动干预同步过程、需要处理文件冲突解决的场景。而 Syncthing 更加现代化,适合需要自动、实时的数据同步与备份的环境。对于选择哪一个,这将取决于个人的使用场景和具体需求。
335 16
|
2月前
|
数据采集 编解码 运维
一文讲完说懂 WowKey -- WowKey 是一款 Linux 类设备的命令行(CLT)运维工具
WowKey 是一款面向 Linux 类设备的命令行运维工具,支持自动登录、批量执行及标准化维护,适用于企业、团队或个人管理多台设备,显著提升运维效率与质量。
|
5月前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
297 32
|
7月前
|
自然语言处理 数据库 iOS开发
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
457 12
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具