在 Debian 中,“端口映射”通常有两种理解:
80 端口的流量转到 8080下面分别说明(以 Debian + iptables / nftables 为主)。
开启 IP 转发(如果已是本机转发可不改):
sysctl -w net.ipv4.ip_forward=1
示例:把 本机 80 端口 映射到 本机 8080 端口
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
如果是本地访问(localhost):
iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-port 8080
查看规则:
iptables -t nat -L -n -v
编辑 /etc/nftables.conf:
table ip nat {
chain prerouting {
type nat hook prerouting priority 0;
tcp dport 80 redirect to 8080
}
}
应用:
nft -f /etc/nftables.conf
示例:
公网 IP:1.2.3.4
内网机器:192.168.1.10
需求:访问 1.2.3.4:2222 → 192.168.1.10:22
开启转发:
sysctl -w net.ipv4.ip_forward=1
DNAT(入口):
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to-destination 192.168.1.10:22
SNAT(回包):
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.10 --dport 22 -j MASQUERADE
允许转发:
iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 22 -j ACCEPT
table ip nat {
chain prerouting {
type nat hook prerouting priority 0;
tcp dport 2222 dnat to 192.168.1.10:22
}
chain postrouting {
type nat hook postrouting priority 0;
masquerade
}
}
firewall-cmd --add-forward-port=port=2222:proto=tcp:toaddr=192.168.1.10:toport=22 --permanent
firewall-cmd --reload
如果你能说明:
我可以给你更精确的命令。