Introduction
Enabling SSH in Oracle VirtualBox allows secure remote command line access to your virtual machines from the host or other devices on your network. This guide covers the most common methods: NAT with port forwarding, Host-only adapter, and Bridged networking. You will also find step-by-step commands to enable OpenSSH in Linux and Windows guests, VirtualBox GUI and VBoxManage commands, troubleshooting tips, and security best practices.
Prerequisites
- Oracle VirtualBox installed on your host machine.
- A virtual machine with a supported guest operating system.
- Administrative or root access inside the guest to install and enable SSH server software.
- Basic familiarity with networking concepts and the host firewall.
Method 1: NAT with Port Forwarding (Recommended for simple host access)
NAT is the default VirtualBox network type. To access the guest SSH server from the host via port forwarding, map a host port to guest port 22.
GUI steps
- Open VirtualBox, select the VM and click Settings.
- Go to Network and ensure Adapter 1 is set to NAT.
- Click Advanced then Port Forwarding. Add a rule mapping Host Port 2222 to Guest Port 22. Example: Host IP empty, Host Port 2222, Guest IP empty, Guest Port 22.
VBoxManage command
From your host terminal you can add the rule with:
VBoxManage modifyvm “Your VM Name” –natpf1 “guestssh,tcp,,2222,,22”
To connect from the host: ssh username@localhost -p 2222. On Windows use PuTTY and set the port to 2222 and host to 127.0.0.1.
Method 2: Host-Only Adapter (Host to VM on a private network)
Host-only networking places the VM on a private network between host and VM. This is useful for development and testing without exposing the VM to your LAN.
- Create a Host-Only Network in VirtualBox Global Tools if not already present.
- Set the VM network adapter to Host-only Adapter and start the VM.
- Assign a static IP inside the guest or use VirtualBox DHCP. Example guest IP: 192.168.56.101.
- Connect from the host: ssh [email protected].
Method 3: Bridged Networking (Guest gets LAN IP)
Bridged mode connects the VM directly to the same network as the host. The VM receives an IP from your network DHCP and becomes reachable from other devices on the LAN.
- Set Adapter to Bridged Adapter in VM Settings and choose the correct host network interface.
- Start the VM and determine its IP via ip addr or ifconfig in Linux.
- Connect using ssh username@guest-ip.
Enable OpenSSH in Linux Guests
Common commands for Debian/Ubuntu:
sudo apt update and sudo apt install openssh-server
Start and enable the service: sudo systemctl enable –now ssh
Verify: ss -tlnp | grep :22 or sudo systemctl status ssh.
Enable OpenSSH in Windows Guests
Install using Optional Features or PowerShell:
Open an elevated PowerShell and run: Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Then start and enable: Start-Service sshd and Set-Service -Name sshd -StartupType ‘Automatic’
Firewall and Security Notes
- In Linux guests, allow SSH through the firewall: sudo ufw allow 22 or if using NAT host port 2222: sudo ufw allow 22/tcp and configure host firewall to allow the chosen host port.
- For security, prefer SSH keys over passwords. Disable password authentication and root login in /etc/ssh/sshd_config.
- Use nonstandard host ports for NAT port forwarding to reduce noise, and consider fail2ban to block repeated attempts.
- If using Bridged mode, ensure your network policies allow the VM to be on the LAN.
Troubleshooting
- Connection refused: Confirm OpenSSH service is running inside the guest.
- Timed out: Verify port forwarding rule or host firewall and that the VM network adapter is attached and active.
- Wrong IP: Check the guest IP with ip addr or ipconfig for Windows.
- Persistent NAT rules: Use VBoxManage commands to ensure rules persist across reboots.
Best Practices
- Use SSH key authentication and disable password login.
- Restrict SSH access by IP on the host firewall when exposing ports.
- Keep OpenSSH up to date in the guest and use strong credentials.
- Document port mappings for team environments and use consistent host ports per VM.
Conclusion
Enabling SSH in Oracle VirtualBox can be done quickly via NAT port forwarding, Host-only networking, or Bridged networking depending on your use case. Follow the steps above to install and enable the SSH server in the guest, configure VirtualBox networking using the GUI or VBoxManage, and apply firewall and security best practices to keep remote access secure.

Leave a Reply