I have the following setup:
- A QEMU virtual machine (VM1) running on a server (server a), which with it shares a private network and it uses the server's internet connection to access the internet.
- A server (server a) that be easily accessed via the internet.
- A private server (server b) that resides in a private network.
My goal is to be able to connect VM1 to server b in such a way that VM1 can access any services (i.e. a web server) running on server b, as a bonus I would also like to be able to access the private network in which server b resides in.
I created a reverse proxy from server b to server a via the following command: ssh -v -o ServerAliveInterval=15 -i “keychain.pem" -N -R localhost:8888:localhost:443 user@publichost.com
And in the /etc/libvirt/hooks/qemu file I added the following entries to forward VM1's port 443 to server a's port 8888 (the reverse shh tunnel):
/sbin/iptables -D FORWARD -o virbr0 -p tcp -d $VM_IP --dport 443 -j ACCEPT
/sbin/iptables -t nat -D PREROUTING -p tcp --dport 8888 -j DNAT --to $VM_IP:443
Where VM_IP is VM1's internal IP address. However when forwarding from 8888->443 server b seems to refuse the connection:
debug1: connect_next: host localhost ([127.0.0.1]:443) in progress, fd=5
debug1: channel 0: new [127.0.0.1]
debug1: confirm forwarded-tcpip
debug1: channel 0: connection failed: Connection refused
connect_to localhost port 443: failed.
debug1: channel 0: free: 127.0.0.1, nchannels 1
I also tried allowing access to port 443 on server b but it does not seem to help: sudo iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT.
I am not sure why the host is refusing the connections. Interesting if I run a very simple python web on server b at port 8080 and reverse ssh tunnel it: ssh -v -o ServerAliveInterval=15 -i “keychain.pem" -N -R localhost:8888:localhost:8080 user@publichost.com
I can access the webserver from server a via curl -k https://localhost:8080 and I can also share it via GatewayPorts=yes with VM1 via curl https://SERVER_A_IP:8080/. I do not understand why server b is refusing the connection - this there an easier way to do this (server b and VM1 are not easily accessible from the internet).