大网数据广告活动
服务器运维问答

Forward traffic from port 22 to virtual computer on ubuntu

· 52CE 我爱测

I'm trying to forward traffic from port 22 on a host machine to port 22 on a virtual machine. My host is running ubuntu. I have tried to execute this command on my host: iptables -t nat -A PREROUTING -d 192.168.1.161 -p tcp --dpo…

问题原文

原帖链接 · Jonas Laursen · CC BY-SA 4.0

I'm trying to forward traffic from port 22 on a host machine to port 22 on a virtual machine.

My host is running ubuntu.

I have tried to execute this command on my host: iptables -t nat -A PREROUTING -d 192.168.1.161 -p tcp --dport 22 -j DNAT --to 192.168.122.2:22, which had no return. No errors, or other information shown.

When running tcpdump -i eno1 port 22 and try to connect to port 22, i do see packages comming into the host. Running the equivilent on the VM shows no incomming package.s

192.168.1.161 is the local ip of eno1. 192.168.122.2 is the ip of the virtual machine.

I have checked that I can connect from the host to the VM.

cat /proc/sys/net/ipv4/ip_forward returns 1

If i try to ssh into the VM from the host (ssh [email omitted]) it works fine. If i try from my workstation (same network as host) ssh [email omitted] it times out. As mentioned from tcpdump i can see the packages arriving at the host, but dont seem to be forwarded to the VM. I have the SSH to the host itself running on a non-default port.

The host is running Ubuntu 17.10. The guest is running Debian GNU/Linux 9

The VM is run by KVM and i manage it using virt-manager.

ifconfig of the host:

eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.161  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2a00:7660:142d:0:8a51:fbff:fe4a:9ed1  prefixlen 64  scopeid 0x0<global>
        inet6 2a00:7660:142d::140  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::8a51:fbff:fe4a:9ed1  prefixlen 64  scopeid 0x20<link>
        ether 88:51:fb:4a:9e:d1  txqueuelen 1000  (Ethernet)
        RX packets 62604  bytes 10855077 (10.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69941  bytes 36442632 (36.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf7f00000-f7f20000

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 10625  bytes 27445185 (27.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10625  bytes 27445185 (27.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:04:bb:9b  txqueuelen 1000  (Ethernet)
        RX packets 1155  bytes 142293 (142.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1667  bytes 650997 (650.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe12:71ae  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:12:71:ae  txqueuelen 1000  (Ethernet)
        RX packets 1155  bytes 158463 (158.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4349  bytes 794450 (794.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

已采纳解答

原帖链接 · Jonas Laursen · CC BY-SA 4.0

I found this to be working: https://ubuntuforums.org/showthread.php?t=2261173&p=13210545#post13210545

TL;DR

iptables -t nat -I PREROUTING -p tcp -d x.x.104.49 --dport 22 -j DNAT --to-destination 192.168.122.20:22
iptables -I FORWARD -m state -d 192.168.122.20/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。