-1

I have a Bridged mode set up in VirtualBox.

I can SSH to my Virtual machine this way (from my Windows machine to VirtualBox):

ssh root@192.168.0.105

However, when I try to SSH to VM via localhost (VM is set up on the same machine), I have this error:

ssh root@localhost
ssh: connect to host localhost port 22: Connection refused

ssh root@127.0.0.1
ssh: connect to host 127.0.0.1 port 22: Connection refused

I also tested NAT + Port forwarding (22 to 2225) and that works fine (I am able to SSH from Windows machine to VM):

ssh root@localhost -p2225

Will that work in any way for the Bridged mode in VirtualBox? (It seems that the Bridged mode separates the localhost and the network it assigns)

t7e
  • 161
  • 1
  • 7
  • Look in `/etc/hosts` and see what `localhost` is defined as. Most likely 127.0.0.1, not 192.168.0.105. – tater Aug 07 '20 at 13:54
  • ssh root@127.0.0.1 does not work either `[root@localhost ~]# cat /etc/hosts` `127.0.0.1 localhost` – t7e Aug 07 '20 at 14:17
  • Then sshd is not listening on 127.0.0.1 or your firewall/config is blocking it. `netstat -an | grep :22` from the same place where you're typing the ssh command. – tater Aug 07 '20 at 14:23
  • From where are you trying to use SSH to connect to the VM? – Tero Kilkanen Aug 07 '20 at 14:25
  • The question is where are you typing the ssh command. If you are typing it in Windows then of course you will not get to the VM. If you are typing it in the VM, then sshd must be listening on 127.0.0.1 and firewall must not block `lo`, see earlier comment. – tater Aug 07 '20 at 14:36
  • I type it in Windows. Then why does NAT + Port forwarding work this way`ssh root@localhost -p2225` and the bridged mode does not? What should I do to be able to SSH to VM via localhost in the bridged mode? – t7e Aug 08 '20 at 01:31

1 Answers1

1

I think you are trying to ssh from your host to your virtual machine and/or the reverse way... Either way, this cannot work (And it is irrelevant if your vm is configured in bridged or in NAT mode).

The Loopback network interface is meant to target your own computer, and therefore is targeting the machine you are executing the ssh command from. It is impossible to pass the vm / host barrier that way! Check here for reference.

[edit] To address the edited question: yes, this should also possible with a bridged network interface. You need full nat (SNAT + DNAT) in this case, but that should work.

Martin
  • 2,194
  • 7
  • 16
  • Thank you. You got my point and I edited my question to be more precise. However, NAT + port forwarding works for me: ssh root@localhost -p2225 – t7e Aug 08 '20 at 01:35
  • now I understand your question... I edited my answer accordingly! – Martin Aug 08 '20 at 13:45