1

I have a KVM guest machine on a virtual network. It has an IP address like 192.168.122.65. The host machine is connected to the public internet. If I open a terminal on the host machine, I can SSH into the guest by typing ssh [email protected], so I know it works fine.

Now I'd like to ssh into the guest from any computer via the Internet. How can I configure the host machine to make it possible? I've tried a lot of rules with iptables but none has helped so far.

Update: so far this solution seems to work, but I haven't tried to restart the server: https://serverfault.com/a/170641/264678

2 Answers 2

1

If the host machine is running SSH server software as well and is accessible from the Internet, you could use the command ssh -L 22065:127.0.0.1:22 [email protected]. The -L option is used in the following way:

 -L [bind_address:]port:host:hostport
             Specifies that the given port on the local (client) host is to be
             forwarded to the given host and port on the remote side.  This
             works by allocating a socket to listen to port on the local side,
             optionally bound to the specified bind_address.  Whenever a con‐
             nection is made to this port, the connection is forwarded over
             the secure channel, and a connection is made to host port
             hostport from the remote machine.  Port forwardings can also be
             specified in the configuration file.  IPv6 addresses can be spec‐
             ified by enclosing the address in square brackets.  Only the
             superuser can forward privileged ports.  By default, the local
             port is bound in accordance with the GatewayPorts setting.  How‐
             ever, an explicit bind_address may be used to bind the connection
             to a specific address.  The bind_address of “localhost” indicates
             that the listening port be bound for local use only, while an
             empty address or ‘*’ indicates that the port should be available
             from all interfaces.

Let's say the host's IP address is 10.0.0.5. From a system on the Internet, you could establish a connection to port 22065 on the host with the command ssh -p 22065 [email protected]. Note: you need to provide a valid userid and password for the guest system even though you are using the host's public IP address (I'm using an address from the private IP address range 10.x.x.x just for purposes of the example), since the connectivty will actually be tunneled to the guest.

When the connection to port 22065 on the host is made, it is forwarded through the SSH tunnel established by your first SSH connection from the host to the guest system at 192.168.122.65. As far as an SSH client on the Internet is concerned, it is going directly to the guest system on port 22065, though it is actually connecting to port 22 on that system via the SSH tunnel from the 10.0.0.5 host. Note: for this to work, the SSH connection from the host to the guest system must be up at the time.

Where I placed 127.0.0.1 you could have also put 192.168.122.65, but since the connection is to the localhost address, i.e., the guest itself, rather than some system external to it, I would use 127.0.0.1

For the firewall rules that are needed, you need to have a rule on the host and any firewalls between it and the Internet that allows incoming TCP connections to port 22065, or whatever port you select, instead. You won't need any additional firewall rules on the guest system.

2
  • Thanks for the nice answer! I can set up an SSH tunnel, but I really wanted to forward a port, because later I'll want to do the same for a HTTP server. Aug 18, 2015 at 11:09
  • I've added my observation to the question Aug 18, 2015 at 11:11
0

First off, maybe don't ssh into the VM?
Instead get tmux on your KVM host, and connect to that, where you can have as many ssh sessions open as you like.

Tell me if any of this sounds closer to what you want, and I'll elaborate.

Quick and dirty:

  • Set up a reverse tunnel with ssh -R, run it on the KVM host, configure your gateway (the Internet-facing router) to forward or DMZ :22 to the KVM host at the port you set up in your reverse tunnel.

Permanent and self-sufficient:

  • Bridge the two networks with a virtual network device like tun, set up the gateway similarly.

Easy:

  • There are SaaS services that do that for you. Many have free tiers that might satisfy your use case.
1
  • Thanks for the answer, but I really want to forward the port, as later I might have to do it with other services (e.g. HTTP). Aug 18, 2015 at 11:12

You must log in to answer this question.

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