在Linux中查看防火墙端口映射的方法有几种,具体取决于你使用的防火墙类型。以下是一些常见的工具和命令:
1. 使用 `iptables` 查看端口映射
`iptables` 是 Linux 上的默认防火墙工具。如果你使用 `iptables` 来管理端口映射,可以使用以下命令查看防火墙的规则:
查看 `iptables` 规则:
```bash
sudo iptables -t nat -L -n -v
```
解释:
- `-t nat`:查看网络地址转换(NAT)表,这对于端口映射特别重要。
- `-L`:列出所有规则。
- `-n`:以数字格式显示IP和端口(不解析为域名和服务名称)。
- `-v`:显示详细信息(如数据包和字节计数)。
你可以看到类似于以下的输出:
```
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:192.168.1.100:8080
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
```
这里的规则显示了端口 80(HTTP)流量被重定向到 `192.168.1.100` 的 8080 端口。
2. 使用 `nftables` 查看端口映射
`nftables` 是 `iptables` 的继任者,提供了更现代化的防火墙配置方式。如果你系统上使用的是 `nftables`,可以使用以下命令查看端口映射:
查看 `nftables` 规则:
```bash
sudo nft list ruleset
```
此命令会列出所有规则和端口映射。输出可能如下:
```
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
tcp dport 80 accept
}
}
table inet nat {
chain prerouting {
type nat hook prerouting priority -100; policy accept;
tcp dport 80 dnat to 192.168.1.100:8080
}
}
```
在这个示例中,`tcp dport 80 dnat to 192.168.1.100:8080` 表示端口 80 的流量被重定向到 `192.168.1.100` 的 8080 端口。
3. 使用 `firewalld` 查看端口映射
如果你使用的是 `firewalld`,可以使用 `firewall-cmd` 工具查看当前的端口映射。
查看 `firewalld` 端口映射:
```bash
sudo firewall-cmd --zone=public --list-all
```
这个命令会列出当前 `public` 区域的所有规则,包括开放的端口和端口转发。
例如,输出可能是:
```
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client
ports: 8080/tcp
protocols:
masquerade: no
forward-ports:
port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
...
```
在这里,`forward-ports` 显示了端口 80 被转发到 `192.168.1.100` 的 8080 端口。
4. 使用 `ss` 或 `netstat` 查看端口
虽然 `ss` 和 `netstat` 主要用于查看网络连接,但它们也可以帮助你查看哪些端口在,以及是否有端口映射到特定的服务上。
使用 `ss` 查看的端口:
```bash
ss -tuln
```
输出示例如下:
```
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
```
在这里,你可以看到端口 80 在。如果有端口映射规则,你可能还需要结合其他工具如 `iptables` 或 `firewalld` 查看具体的映射。
---
最常用的防火墙查看方法是通过 `iptables` 或 `firewalld`,取决于你系统上的防火墙配置工具。