iptables:在不同接口(interface)和端口上转发请求

标签 iptables portforwarding

我有一台有 2 个接口(interface)的机器:

eth0      inet addr:1.1.1.1
eth1      inet addr:2.2.2.2

eth0是服务器,eth1是虚拟机上的网络。

我的服务器上有 ssh,所以 1.1.1.1:22 正忙。

我需要一个规则,用于将 eth0 端口 6000 上的传入连接重定向到 eth1、端口 22(虚拟机 IP)上的 ip 2.2.2.100。

在这种模式下,如果我在外部机器上这样做,

ssh -p 6000 root@1.1.1.1

我将登录虚拟机。

我尝试了这个规则,但它不起作用:

sudo iptables -P FORWARD ACCEPT
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 6000 -j DNAT --to 2.2.2.100:22

最佳答案

这个案例大约有 100 万个脚本/教程/东西,但是如果有人从 google 登陆到这里,会是这样的:

iptables -I FORWARD -d 2.2.2.2 -m comment --comment "Accept to forward ssh traffic" -m tcp -p tcp --dport 22 -j ACCEPT    
iptables -I FORWARD -m comment --comment "Accept to forward ssh return traffic" -s 2.2.2.2 -m tcp -p tcp --sport 22 -j ACCEPT    
iptables -t nat -I PREROUTING -m tcp -p tcp --dport 60000 -m comment --comment "redirect pkts to virtual machine" -j DNAT --to-destination 2.2.2.2:22   
iptables -t nat -I POSTROUTING -m comment --comment "NAT the src ip" -d 2.2.2.2 -o eth1 -j MASQUERADE

关于iptables:在不同接口(interface)和端口上转发请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212807/

相关文章:

c# - .NET SSH 端口转发

Vagrant 反向端口转发?

mysql - 使用 IPTABLES 限制 MySQL 3306 端口

configuration - 具有非标准 SSH 端口的 Gitlab(在具有 Iptable 转发的 VM 上)

linux - 使用 iptables 记录所有到关闭端口的连接

docker - docker-compose 中的端口转发

linux - 无法从外部机器访问公开暴露的 Docker 容器端口,只能从本地主机访问?

linux - aws ec2 iptables 端口 80 (http)

wordpress - VirtualBox WordPress 重定向到端口 80

azure - 无法连接到在 Azure VM 上运行的我的网站