介绍
Linux本身不具备防火墙功能。虽然Linux不具备防火墙,但是Linux中的内核管控着所有的网络流量。
Linux的防火墙功能是通过内核的net_filter提供的。只要内核有这个模块,那么就有防火墙功能。
netfiler才是防火墙真正的安全框架(framework),netfilter位于内核空间
iptables其实是一个命令行工具,位于用户空间,我们用这个工具操作真正的框架
iptables介绍和基础操作
iptables(ip->tables) ,ip指的就是我们的网络(IP协议),tables就是表的意思
iptables就是一个客户端,该客户端直接和内核空间的net_filter交互,告诉net_filter什么样的流量进行放行,什么样的流量进行阻塞
-
linux的防火墙是由net_filer模块提供的
-
iptables是一个客户端,客户端时用来接收用户命令的
-
iptables讲用户命令转换成对应的net_filter里面的过滤规则
流量通过网卡进入到内核空间,通过input的规则进行筛选,符合规则规则,就允许进入,对应,又进就有出,流量出去的规则对应的是output
iptables基本操作
主机 | 管理网络 | 数据网络 |
---|---|---|
client | 192.168.100.134/24 | 192.168.200.129/24 |
server | 192.168.100.129/24 | 192.168.200.128/24 |
先关闭防火墙
## client节点
[root@client ~]# systemctl stop firewalld
## server节点
[root@server ~]# systemctl stop firewalld
查看iptables表
## client节点
[root@client ~]# iptables -L
Chain INPUT (policy ACCEPT) ## 流量的输入,默认INPUT的规则,所有的流量全都允许
target prot opt source destination
Chain FORWARD (policy ACCEPT) ## 流量的转发,默认INPUT的规则,所有的流量全都允许
target prot opt source destination
Chain OUTPUT (policy ACCEPT) ## 流量的输出,默认INPUT的规则,所有的流量全都允许
target prot opt source destination
[root@client ~]# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
iptables -L
可以查看INPUT
,OUTPUT
和FORWARD
三个表
iptables -L -t nat
能查看prerouting
和postrouting
的规则
所有网卡的进入流量都在INPUT
设置,下面没有任何规则表示所有流量都可以通过
清空当前的所有规则和计数
如果不知道你在干什么,就不要动,这三个操作十分危险
-
iptables -F
:清空所有的防火墙规则 -
iptables -X:删除用户自定义的空链
-
iptables -Z:清空计数
iptables规则的添加与删除
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
-
-A:表示添加,INPUT表示添加的位置
-
-s:表示源,192.168.1.0/24表示数据包中源ip是192.168.1.0/24网段将会匹配
-
-p:表示协议,tcp就表示匹配的协议是tcp将会被匹配
-
--dport:表示目的端口,22就表示目的的端口是22将会被匹配
-
-j:表示动作,ACCEPT就表示接受(允许)
192.168.100.134对192.168.200.128进行访问,然后使用iptables规则拒绝192.168.200.128,看看结果
## 测试连通性
[root@client ~]# ssh 192.168.200.128
root@192.168.200.128's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Mar 20 14:56:34 2024 from 192.168.200.129
[root@server ~]#
## 在server中编写iptables规则,拒绝192.168.100.0/24网段的22号端口的访问
[root@server ~]# iptables -A INPUT -s 192.168.100.0/24 -p tcp --dport 22 -j REJECT
[root@server ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.100.0/24 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
## 在client进行ssh访问
[root@client ~]# ssh 192.168.200.128
ssh: connect to host 192.168.200.128 port 22: Connection refused
## 会发现拒绝连接,这是因为在server端拒绝了`192.168.200.0/24`网段的22号端口
## 删除刚才定义的规则
[root@server ~]# iptables -D INPUT -s 192.168.200.0/24 -p tcp --dport 22 -j REJECT
[root@server ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
## 在client再次使用ssh
[root@client ~]# ssh 192.168.200.128
root@192.168.200.128's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Mar 20 15:05:11 2024 from 192.168.100.1
## 会发现现在可以连通
client连接192.168.200.128
拒绝连接,这是因为在server端拒绝了192.168.200.0/24
网段的22号端口
删除刚才定义的规则
iptables默认规则
一般iptables是不会针对output和forward流量进行控制, 第一,一般服务器操作系统不会开启流量转发 第二,就是output的流量没有必要限制
默认规则
iptables -P INPUT DROP
:配置默认的不让进去
iptables -P FORWARD DROP
:配置默认的不让转发
iptables -P OUTPUT ACCEPT
:配置默认的可以出去
白名单和黑名单的概念
黑名单就是默认放行所有,把想拒绝规则写进去
白名单就是默认拒绝所有,把想放行的规则写进去,白名单永远优于黑名单