1

This might turn out to be pretty basic but I am in dilemma right now over this. I have a Virtual Machine on KVM which has some socket server. I am able to connect to this machine via localhost, i.e. using a client on the host machine but what I want to do is to be able to connect remotely. By remotely I mean from another PC.

Right now I haven't even been able to ping these machines. The ip's assigned to this machine by default is 192.168.122.122 and the ip of my host when it connects to the University network is something like 10.5.135.*. Is there some way I can connect to my application remotely? Preferably without having to change the ip of the VM as there is a lot of configuration i will need to change. I am willing to create a hot-spot with some static ip if that's what it takes.

Appreciate any help I can get in this regard.

1 Answer 1

2

You can configure port forwarding on the host for certain TCP and UDP ports. It seems to be impossible to do DNAT with ICMP, though.

Alternatives are to set up an encrypted (SSH, OpenVPN, IPsec) or cleartext tunnel from the remote system to the host and set up routing for the VM address(es) through this tunnel on the remote system.

SSH port forwarding

host_ip='10.5.135.42'
vm_ip='192.168.122.122'
vm_port=42
# on the remote system
ssh -L "127.0.0.1:1234:${vm_ip}:${vm_port}" user@$host_ip

When this SSH connection is active then you can connect on the remote system to port 1234 and reach the desired port on the VM:

telnet localhost 1234

DNAT with iptables

On the host:

vm_ip='192.168.122.122'
vm_port=42
iptables -t nat -A PREROUTING -p tcp --dport "$vm_port" -j DNAT --to-destination "$vm_ip"
iptables -t nat -A POSTROUTING -p tcp -d "$vm_ip" --dport "$vm_port" -j MASQUERADE

Then you can connect to the port on the host and the packet is redirected to the VM. Because the VM sees the packet coming from the host (and not from the remote system due to masquerading) this works.

5
  • I just need to make 1 port accessible. Can you please recommend some guide that I can follow? I am like totally noob at this stuff.
    – usamazf
    Dec 30, 2017 at 20:39
  • 1
    @UsamaZafar See my edit. Dec 30, 2017 at 21:03
  • Thanks mate. I will try this out and will let you know how it goes.
    – usamazf
    Dec 30, 2017 at 21:54
  • Is one method preferred over the other? Also in the second method, do we need to execute this command once or each time the host is powered up?
    – usamazf
    Dec 30, 2017 at 21:54
  • @UsamaZafar Depends on your requirements. SSH makes sure that you are the only one who can connect to the VM. iptables lets everyone through. Dec 30, 2017 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.