My setup is made of (on the same host):
- a Network Interface Card (NIC) eth0 with static IP 192.168.1.10
- a label to the NIC eth0:0 with static IP 192.168.1.11
- a virtual guest machine (using KVM) at 192.168.122.11 on virbr0
I can SSH the guest machine from the host using its IP 192.168.122.11, and I can SSH the host machine from a remote machine on the LAN network using its IP 192.168.1.10.
I would like to make the virtual guest accessible from the LAN through the labelled NIC so that I can SSH into 192.168.122.11 by connecting to 192.168.1.11 from a remote machine.
+--------------+
| Machine Ext2 |
+--------------+
| ssh to 192.168.1.11:2222
|
+--------+
| Router |
+--------+
|
| (192.168.1.11:2222)
+----------------------------------------------+
| Machine Ext1 (Host) |
| <--> iptables <--> Guest (192.168.122.11:22) |
+----------------------------------------------+
I got inspired by many things from different tutorials over the web (all look more or less the same) but nothing works and I can't figure out the proper setup..
The idea is to create PREROUTING, FORWARD, and POSTROUTING rules with iptables so that incoming and outgoing traffics are redirected rightfully.
I edited /etc/sysctl.conf so that ip_fordward=1 and ran the following iptables commands:
$ sudo iptables -t nat -A PREROUTING -i virbr0 -p tcp -d 192.168.1.11 --dport 2222 -j DNAT --to-destination 192.168.122.11:22
$ sudo iptables -A FORWARD -p tcp -d 192.168.122.11 --dport 22 -j ACCEPT
$ sudo iptables -t nat -A POSTROUTING -o virbr0 -p tcp -d 192.168.122.11 -j ACCEPT
Pinging 192.168.1.11 still work ok. But connecting to 192.168.1.11:2222 fails:
$ telnet 192.168.1.11 2222
$ telnet: Unable to connect to remote host: Connection refused
I feel like I'm close to get it work but miss out on something.. Can you give me a little help?
Best regards,