Recently, we set up an Ubuntu Server in a Gnome Boxes virtual machine. We wanted to perform an ssh connection into it to make administration easier. In the properties of the VM that are visible from the GUI, there was no option to edit the network cards and set up a virtual network between the host and the virtual machine.
To allow ourselves to perform the ssh connection, we decided to go with the option of reverse ssh tunneling. To do so, we needed to install and start the ssh server.
After that, we got the IP of the host machine.
Then, we used the terminal of the virtual machine to execute the following ssh command:
ssh -N -T -R 22222:localhost:22 host_machine_user@host_machine_ip;
That created a connection to the host machine and blocked the terminal as expected since it was an active application.
Finally, from the host, we executed the following to ssh into the virtual machine:
ssh -p 22222 virtual_machine_user@localhost;
The biggest disadvantage of this method is that you need to enable ssh on your host machine.
The biggest advantage is the ease with which anyone can set it up.
Notes on the ssh parameters:
-N Do not execute a remote command. This is useful for just forwarding ports.
-T Disable pseudo-terminal allocation.
-R remote_socket:host:hostport Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.