Ssh'ing in and out of a virtualbox guest

0

I've set up port forwarding and allowed a firewall exception so I can ssh from my Linux Mint host into a Windows7/Cygwin guest.

#from linux host
hostuser@host$ ssh -p 2222 guestuser@localhost

What do I have to do to make it work in reverse as well?

#from cygwin guest (not working yet)
guestuser@guest$ ssh -p 2222 hostuser@localhost

PSkocik

Posted 2018-08-18T11:50:59.790

Reputation: 1 182

Do you think this is a good question for superuser.com or would I stand a better chance of getting a good answer if I moved it elsewhere? – PSkocik – 2018-08-18T12:29:06.027

You should always elaborate on "not working yet". There is no reason why it shouldn't work, unless you have firewall rules that prevent it. – RalfFriedl – 2018-08-18T12:51:51.317

@RalfFriedl I set up port mapping in Virtual box, from Host/2222 to Guest/22 and from Guest/2222 to Host/22 and unblocked incoming 22 and outgoing 2222 in the Windows firewall, but only incoming host-to-guest connections work. Can't call out. – PSkocik – 2018-08-18T13:09:40.007

@RalfFriedl I get connection refused even if I turn the firewall off completely. It's not a firewall problem, I guess I just need to get Win7 to somehow forward to the the Host at TPC/22 if I connect to 2222 in WIn7. – PSkocik – 2018-08-18T13:13:37.350

Why do you try to connect to localhost instead of the host you want to connect to? – RalfFriedl – 2018-08-18T13:15:28.893

@RalfFriedl So I could rsync from inside and bundle it with my build command. I could avoid the rsyncing part completely if I got the shared directories working right, but https://superuser.com/questions/1350225/virtual-box-shared-directories-not-showing-up-in-cygwin-when-sshing-into-it but I'd like to be able to do this just for the sake of being able to do it too.

– PSkocik – 2018-08-18T13:33:56.217

Answers

0

Virtualbox has multiple networking mode that you can choose for guest VMs. You did not specify what mode you chose, but if you chose 'NAT', (it's the default and most popular), you cannot ssh from the guest to the host. Because there is no route from the guest to the host.

Additionally, your guest OS does not know it is a VM. When you guestuser@guest$ ssh -p 2222 hostuser@localhost you are saying: ssh into localhost, the guest OS, port 2222, and connect to the sshd service running on the guest OS.

If you wish to have the guest OS be able to connect to the host OS you should change the VB networking mode. You will probably want 'Bridged' mode. Here the VM acts as a separate (physical) computer on your home network. It will receive a IP address via DHCP. Then you may ssh from one into the other, assuming there is no firewall rules or other 'barriers' between them. There is no port forwarding in this mode; all ssh connections will be through port 22 by default.

Example

hostuser@host$ ssh guestuser@192.168.0.2

guestuse@guest$ ssh hostuser@192.168.0.1

Assuming host IP=192.168.0.1 and guest IP=192.168.0.2

Opinion: Unless you are setting up a specific service on your guest, there is often little need for the guest OS to be able to connect its host in the way you are asking about. I would not go through the trouble of setting this up.

ender.qa

Posted 2018-08-18T11:50:59.790

Reputation: 126