Unable to ssh from host to guest (both linux)

Discussions related to using VirtualBox on Linux hosts.
Post Reply
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Unable to ssh from host to guest (both linux)

Post by mn »

I have defined a port forwarding rule for SSH connection (from host to guest) where both are Ubuntu based machines.
On the host, the sshd service is running on port 22 as below

Code: Select all

mahmood@host:~$ sudo netstat -tulpn | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      812/sshd: /usr/sbin
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      973710/sshd: mahmood
tcp6       0      0 :::22                   :::*                    LISTEN      812/sshd: /usr/sbin
tcp6       0      0 ::1:6010                :::*                    LISTEN      973710/sshd: mahmood
and I have defined the following rule in the guest settings:
11.jpg
11.jpg (31.21 KiB) Viewed 6483 times
On the guest I see the following ip address and sshd port number

Code: Select all

mahmood@guest:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:12:63:f3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
       valid_lft 86358sec preferred_lft 86358sec
    inet6 fe80::741:9434:60a8:98bd/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
mahmood@guest:~$ sudo netstat -tulpn | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      569/sshd: /usr/sbin 
tcp6       0      0 :::22                   :::*                    LISTEN      569/sshd: /usr/sbin 

Now, when I want to ssh from host to guest, I get the following time out error.

Code: Select all

mahmood@host:~$ ssh mahmood@10.0.2.15
ssh: connect to host 10.0.2.15 port 22: Connection timed out
How can I fix that?
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Unable to ssh from host to guest (both linux)

Post by mpack »

I think it's unwise to forward well known host TCP/IP ports to the VM. Doing so can break the host, or simply be rejected (e.g. if you already attached a host service to port 22). Allocate a dedicated unique port, typically in the 8000 range. E.g. 8022.

I assume there's a reason why you are using NAT instead of bridged or host-only. Port forwarding is only needed for NAT. Ideally you'd use bridged, have the router assign an IP to the VM, and then any capable device on your network can reach the ssh capability in the VM.
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Re: Unable to ssh from host to guest (both linux)

Post by mn »

I think it's unwise to forward well known host TCP/IP ports to the VM. Doing so can break the host, or simply be rejected (e.g. if you already attached a host service to port 22). Allocate a dedicated unique port, typically in the 8000 range. E.g. 8022.
You mean I should configure guest to use a different port for sshd? e.g. 8022?
I assume there's a reason why you are using NAT instead of bridged or host-only. Port forwarding is only needed for NAT. Ideally you'd use bridged, have the router assign an IP to the VM, and then any capable device on your network can reach the ssh capability in the VM.
I used the default setting which is NAT. Also I need the guest to access the Internet.
fth0
Volunteer
Posts: 5661
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Unable to ssh from host to guest (both linux)

Post by fth0 »

In the VM configuration, change the host port to 8022, then use ssh -p 8022 mahmood@127.0.0.1.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Unable to ssh from host to guest (both linux)

Post by mpack »

mn wrote:
mpack wrote:I assume there's a reason why you are using NAT instead of bridged or host-only.
I used the default setting which is NAT. Also I need the guest to access the Internet.
The default setting is NAT because it works for the majority of users, who are not using ssh.

Bridged Networking would give you Internet access and eliminate the need for port forwarding. However it usually works best with a host wired connection. A host WiFi connection may work, but is outside the spec.
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Re: Unable to ssh from host to guest (both linux)

Post by mn »

OK thanks I got it.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Unable to ssh from host to guest (both linux)

Post by scottgus1 »

Glad you're up and running!

FWIW when using NAT (not "NAT Netowrk") only the host and guest port numbers are needed, due to the way Virtualbox NAT works. See Virtualbox Networks: In Pictures: NAT
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Re: Unable to ssh from host to guest (both linux)

Post by mn »

Hi again,
I would like to try port forwarding + NAT for an SSH connection from host to guest (and vice versa). the port forwarding table looks like
Screenshot from 2021-08-19 12-05-18.png
Screenshot from 2021-08-19 12-05-18.png (8.74 KiB) Viewed 5649 times
The IP address of host

Code: Select all

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp39s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 2c:f0:5d:a3:8b:57 brd ff:ff:ff:ff:ff:ff
    inet 172.18.20.175/22 brd 172.18.23.255 scope global dynamic noprefixroute enp39s0
       valid_lft 54027sec preferred_lft 54027sec
    inet6 fe80::9a4c:a21f:3a00:d98a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
and the ip address of the guest
Screenshot from 2021-08-19 12-06-54.png
Screenshot from 2021-08-19 12-06-54.png (9.7 KiB) Viewed 5649 times
However, when I want to ssh from host to guest I don't see any prompt for password.

Code: Select all

$ sudo netstat -tulpn | grep 8900
tcp        0      0 127.0.0.1:8900          0.0.0.0:*               LISTEN      694840/VirtualBoxVM 
$ ssh slurmadmin@10.0.2.15
^C
$ ssh slurmadmin@10.0.2.15 -p 8900
^C
$ ping 10.0.2.15
PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
^C
--- 10.0.2.15 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2042ms


I have to say that I can ssh from guest to host.
Any idea about that?
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Re: Unable to ssh from host to guest (both linux)

Post by mn »

OK. the answer is that I shouldn't specify 10.0.2.15 as the target machine. So, the correct command from host is

ssh slurmadmin@localhost -p 8900
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Unable to ssh from host to guest (both linux)

Post by scottgus1 »

mn wrote:I shouldn't specify 10.0.2.15 as the target machine.
This is correct. That's because there's a router between your host and the VM. Did you read the Pictures link I posted above your last post? It also said that two of the items in your port-forwarding rule are not necessary.
mn
Posts: 99
Joined: 17. Feb 2013, 21:33

Re: Unable to ssh from host to guest (both linux)

Post by mn »

Yes but I read from another tutorial and followed that.
Thanks.
Post Reply