I am using VirtualBox on Windows 10 (host) to create two virtual machines (Guest DT and Guest S). Can I SSH from Guest DT to the Guest S?
Guest DTis Ubuntu Desktop 18.04.Guest Sis running Ubuntu Server 18.04.- VirtualBox Terminology
I am inexperienced with SSH. I have googled and read answers to the 'similar questions' on this site.
Error
On Guest DT, I do this:
$ ssh test@10.0.2.15
ssh: connect to host 10.0.2.15 port 22: Connection refusedNote:
- I can successfully ping 10.0.2.15
testis my username on theGuest S.
Environment Setup
On Guest DT:
$ ssh-keygen
$ cat ~/.ssh/id_rsa.pub
ssh-rsa <a very long key> neil@neil-VirtualBoxOn Guest S:
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ echo "ssh-rsa <a very long key> neil@neil-VirtualBox" > ~/.ssh/authorized_keys
$ /etc/init.d/ssh restartOn Guest S:
test@server01:~$ service sshd status
ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-06-15 02:05:04 UTC; 1h 16min ago Process: 903 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS) Main PID: 906 (sshd) Tasks: 1 (limit: 2317) CGroup: / └─906 /usr/sbin/sshd -D
Jun 15 02:05:04 server01 systemd[1]: Starting OpenBSD Secure Shell server...
Jun 15 02:05:04 server01 sshd[906]: Server listening on 0.0.0.0 port 22.
Jun 15 02:05:04 server01 sshd[906]: Server listening on :: port 22.
Jun 15 02:05:04 server01 systemd[1]: Started OpenBSD Secure Shell server.
$ sudo ufw status
[sudo] password for test:
Status: inactive
/etc/ssh/sshd_config contains :
#PubkeyAuthentication yes 5 2 Answers
I think the default networking setup for VirtualBox guest is NAT and is not really intended to achieve this type of networking communication; but based on old posts may be possible by forwarding a non-system port to a Guest port and using the default loopback IP address to reach the Guest. 10.0.2.2 is default loopback.
Setup Networking
However, the easiest is to ensure you choose the correct networking for each of the Guests. Shutdown you guest machines, then in VirtualBox edit the Networking mode appropriate for your needs (note grid).
Using Default NAT Network
Using NAT, VirtualBox uses a special IP 10.0.2.2 as a loopback (at least I think this is still true). Therefore ports forwarded from loopback are available to all guests. I think you should be able to take advantage of this. To communicate using a NAT network, will require one of the following:
Using VBoxManage, setup NAT Forwarding of a non-system port (1024–65535) to a lower system port (< 1024).
- In this case forward something like port 2222, to port 22 (default for SSH). This also assumes we'll use
Guest SIP address of10.0.2.15, per the question. Configure port forwarding on the Virtualbox Host, run:
VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"
All TCP traffic arriving on port 2222 on any host interface will be forwarded to port 22 in the guest. The protocol name tcp is a mandatory attribute defining which protocol should be used for forwarding (udp could also be used). The name guestssh is purely descriptive and will be auto-generated if omitted. The number after --natpf denotes the network card, like in other parts of VBoxManage.
Check the added rule:
VBoxManage showvminfo myserver | grep 'Rule'
Connect to
Guest Servervia host loopback mapping and port-forwarding forGuest Sneil@neil-VirtualBox$ ssh -p 2222 test@10.0.2.2
- In this case forward something like port 2222, to port 22 (default for SSH). This also assumes we'll use
Alternative: change (Guest S) /etc/sshd_config to listen on anon-system port (> 1024). Then reload ssh.
Simply remove all ECDSA fingerprint keys (probably SHA256) contained within known-hosts directory.
- Run
vi ~/.ssh/known_hosts - Delete all keys associated with subject IP-address within the known_hosts file.
- Enter "Esc"-key then "Shift"-key + ":" + "x"