MacOS – SSH to VirtualBox Mac Host Linux Guest using NAT

macosNetworksshvirtualbox

How to configure ssh OS X host with Linux guest, so I can access my guest OS through SSH. I've tried:

virtualbox guest

But when I tried to ssh from host, it failed:

> ssh localhost
ssh: connect to host localhost port 22: Connection refused

I've tried to set the ip (host 10.0.0.10, guest 10.0.2.15) on the port forwarding, it gives the same output.
I've tried to bind the host to higher address (2222), but got no luck, the host still doesn't listen on port forwarding port.
It doesn't work like this answer: VirtualBox Host ssh to Guest

I could not even ping the guest from the host:

PING 10.0.2.15 (10.0.2.15): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
^C
--- 10.0.2.15 ping statistics ---
4 packets transmitted, 0 packets received, 100.0% packet loss

But ping works fine from guest to host, ssh from guest to localhost is also ok, the PermitRootLogin is yes, the netstat result is ok:

netstat guest

EDIT after switching to bridged mode, it works fine.

Best Answer

After adding a port-forwarding rule (e.g. SSH TCP 10.0.0.10 2222 10.0.2.15 22) enter in the host's Terminal.app:

 ssh user_name@host-ip -p forwarded_port

Example with the rule above:

 ssh user_name@10.0.0.10 -p 2222

Don't use identical ports in the rule (host's port ≠ guest's port) like in your screenshot. user_name has to be the name of a remote access enabled account on the guest OS.

You may even skip the IP addresses (10.0.0.10 and 10.0.2.15) in the rule and either use:

 ssh user_name@10.0.0.10 -p 2222

or

 ssh user_name@127.0.0.1 -p 2222

or even

 ssh user_name@localhost -p 2222