1

I have a bare metal host server with multiple guest VMs made with QEMU/KVM using Virtual network "default":NAT option. There is only one public IP available.

For better illustration:

  • BM host server (Ubuntu), IP 89.185.xx.xx

  • VM guest server (Ubuntu) no.1, IP 192.168.122.101

    VM guest server (Ubuntu) no.2, IP 192.168.122.102

    VM guest server (Ubuntu) no.3, IP 192.168.122.103

    ...

Now I'd like to run the same application using different port on each VM and be able to connect to each instance from outside through the same public IP. E.g.:

  • 89.185.xx.xx:30334

  • 89.185.xx.xx:30335

  • 89.185.xx.xx:30336

    ...

Similar setup like running the application in multiple docker containers. How could I set this up please?

2
  • 2
    nginx as reverse proxy for any kind. this is how i use it
    – djdomi
    Commented Jan 4, 2022 at 18:19
  • 1
    Reverse proxy for HTTP/HTTPS and port forwarding for other purposes. Commented Jan 4, 2022 at 21:33

1 Answer 1

3

Taken from https://www.systutorials.com/port-forwarding-using-iptables/

to port forward from public IP to local one for VM1:

# iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 30334 -j DNAT --to 192.168.122.101:30334
# iptables -I FORWARD 1 -p tcp -d 192.168.122.101 --dport 30334 -j ACCEPT 

This example assumes local and remote port both are 30334 and public interface name is eth0.

3
  • thanks for the answer! but can't make it work though (must apologize in advance as I don't have IT background so maybe some lame mistakes are overlooked) I tried this with ssh first: $ sudo iptables -A PREROUTING -t nat -i eno2 -p tcp --dport 2222 -j DNAT --to 192.168.122.101:2222 $ sudo iptables -A FORWARD -p tcp -d 192.168.122.101 --dport 2222 -j ACCEPT (eno2 - network interface on BM host server connected to the public IP 89.185.xx.xx)
    – Curu
    Commented Jan 8, 2022 at 15:31
  • I changed ssh port on VM guest 192.168.122.101 to 2222, made sure ufw is inactive on both host and guest and tested ssh connection from BM host server which was working: [email protected]:~$ ssh [email protected] -p 2222 Now I tried to ssh from my local pc to the VM: curu@desktop:~$ ssh [email protected] -p 2222 but it says: ssh: connect to host 89.185.xx.xx port 2222: Connection refused
    – Curu
    Commented Jan 8, 2022 at 15:31
  • 1
    the problem was solved with putting the forward rule at the first place in iptables: iptables -I FORWARD 1 -p tcp -d 192.168.122.101 --dport 2222 -j ACCEPT (I edited the original answer)
    – Curu
    Commented Jan 10, 2022 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .