SSH into a VirtualBox VM on NAT
A VirtualBox VM on NAT reaches the internet, but nothing reaches it, including your host. SSH fails because the guest has no address your host can dial, not because of guest keys or firewalls.
Port forwarding makes the host listen and hand the connection in.
Step 1 — Get the IP address of the VM
Log into the VM through the VirtualBox window:
ip addr # or: hostname -I
Look for 10.0.2.15 on the NAT interface: the guest address the rule points at. Write it down.
Step 2 — Make sure the SSH server is running
On Ubuntu-based hosts:
sudo systemctl status sshd
You want active (running). If not, install and start it:
sudo apt install openssh-server
sudo systemctl enable --now ssh
If SSH is not running, nothing else helps.
Step 3 — Set the virtual network interface to NAT
In VirtualBox, select the VM: Settings -> Network -> Adapter 1, and set Attached to: NAT.
NAT is the default, so this is often already set.
Step 4 — Add the port forwarding rule
Still in Settings -> Network -> Adapter 1, click Advanced (expand it), then Port Forwarding.
Click the green plus to add a rule. Six fields:
| Field | Value | Why |
|---|---|---|
| Name | anything, e.g. ssh |
labels the rule; not used by SSH itself |
| Protocol | TCP |
SSH is TCP-based, so leave it alone |
| Host IP | 127.0.0.1 |
listen only on your machine’s loopback; see the warning below |
| Host Port | 2222 |
the port you will dial from your laptop |
| Guest IP | 10.0.2.15 |
the VM’s address from Step 1 |
| Guest Port | 22 |
the standard SSH port inside the VM |
Confirm with OK, then OK again to close Settings.
Add the rule before starting the VM. You can add it while it runs, but the VM must run for it to work.
Step 5 — Connect
From your host’s terminal, not the VM:
ssh -p 2222 student@127.0.0.1
- student — the account inside the VM.
-p 2222— the host port.ssh hostalone goes to port 22, where nothing on the host is listening, and you getConnection refused.
Use a host alias:
Host vm-nat
HostName 127.0.0.1
Port 2222
User student
IdentityFile ~/.ssh/id_ed25519_vm
Then ssh vm-nat is the whole command.
Nothing outside your host sees it: the listener bound to 127.0.0.1.
Passwordless login into the VM
Once the port works, set up passwordless SSH as usual. Forwarding does not change the key mechanism.
# private key lives on your host, as usual
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_vm -C "you@example.vnce.eu"
# install your PUBLIC key inside the VM
cat ~/.ssh/id_ed25519_vm.pub | ssh -p 2222 student@127.0.0.1 \
"mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
# from now on, no password
ssh -p 2222 student@127.0.0.1
Inside the VM the key sits as on any server: ~/.ssh/authorized_keys, permissions 600, directory 700. A forward changes nothing the VM requires.
Scp and rsync also work through the forward
The forward is a plain TCP listener, so anything over SSH uses it:
scp -P 2222 ~/.ssh/id_ed25519_vm.pub student@127.0.0.1:/tmp/
rsync -e 'ssh -p 2222' ./dir student@127.0.0.1:~/
Note the capital -P for scp, where ssh uses lowercase -p.
VirtualBox network modes
- NAT — outbound only; the guest is hidden behind the host.
- Host-Only — a private address only your host can reach; no forwarding, but no guest internet either.
- Bridged — the guest joins the network as a peer; it needs a router address and is reachable like any other machine.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
Connection refused on port 2222 |
Rule missing, or host port in use, or the VM is not running | check the rule, ss -ltnp | grep 2222, start the VM |
| Prompted for a password you never set | sshd not running in the guest, or the key is not installed |
sudo systemctl status sshd on the VM |
| Dials 22 instead | forgot -p 2222 |
add it, or use the config alias |
| Works once, then refused | host port 2222 was transiently occupied | restart the rule or change the host port |
| Guest IP missing | VM uses DHCP differently than 10.0.2.15 |
re-read hostname -I inside the VM and update the rule |
| Fingerprint warning every connection | the guest keeps reissuing its host key, as in a fresh boot | expected on some NAT interfaces; or accept the key once and check |
The guest IP is fragile. If the VM re-acquires another address, the old rule forwards to the wrong place. Check ~/.ssh/authorized_keys and the guest IP together when it breaks.
Check yourself — 3 questions
1. ssh student@127.0.0.1 gives Connection refused. What is missing from the command?
The port: ssh -p 2222 student@127.0.0.1. Without -p it dials port 22, where nothing on the host is listening — the forward listens on 2222.
2. Why is the rule's Host IP set to 127.0.0.1 rather than left empty?
An empty Host IP binds the listener to every interface, so other machines on your network could dial straight into your VM. Loopback keeps the hole local-only.
3. Inside the VM, does passwordless SSH differ from a physical server?
No. The forward only carries bytes: same key pair, same authorized_keys, same permissions. The forward only carries bytes; the key setup is unchanged.