SSH timeouts in Oracle VirtualBox with Ubuntu typically stem from NAT network configuration limits, firewall restrictions, or host-guest connectivity issues. This guide details how to verify network adapters, configure port forwarding, and adjust SSH daemon settings to restore reliable remote access to your virtual machine.
Understanding the Network Adapter Configuration
The most common cause of SSH timeouts is an incorrect or missing network adapter type in the VirtualBox settings. By default, new virtual machines often use the "NAT" network adapter, which allows the guest OS to access the internet but prevents incoming connections from the host machine unless explicitly configured. To resolve this, you must ensure that the network interface is correctly set up to allow communication between your host computer and the Ubuntu VM.
Open the Oracle VirtualBox Manager and select your Ubuntu virtual machine. Navigate to
Settings and then click on the
Network tab. Here, you will see the
Adapter 1 section. If you are using NAT, you need to set up port forwarding rules. Alternatively, switching to a "Bridged Adapter" can make the VM appear as a distinct device on your local network, which often simplifies connectivity. However, for many users, keeping NAT and configuring port forwarding is more stable because it avoids potential IP conflicts with other devices on the LAN.
If you choose to stick with NAT, click the
Advanced dropdown menu and then the
Port Forwarding button. You need to add a rule that maps a port on your host machine (usually 2222) to port 22 on the guest VM. The protocol should be TCP. Once saved, restart the virtual machine to apply these changes. This step is crucial because without it, attempts to connect via SSH from the host will time out as the traffic has no defined path into the virtual environment.
For those who prefer a more direct connection, switching to a Bridged Adapter involves selecting "Bridged Adapter" from the
Attached to dropdown. This requires that your physical network interface supports bridging. After applying this change, the Ubuntu VM will request an IP address from your router just like any other physical device. You can then use this IP address directly for SSH connections. Note that if your router assigns dynamic IPs, you may need to configure a static DHCP reservation or check the IP address frequently.
Verifying SSH Service and Firewall Rules
Even with correct network settings, SSH timeouts can occur if the SSH service itself is not running or if internal firewall rules block the connection. Inside your Ubuntu VM, open a terminal and check the status of the OpenSSH server by running
sudo systemctl status ssh. If the service is inactive or failed, start it immediately using
sudo systemctl start ssh and enable it to start on boot with
sudo systemctl enable ssh.
Ubuntu systems come with UFW (Uncomplicated Firewall) enabled by default. If UFW is active, it might be blocking incoming SSH traffic. Check the firewall status with
sudo ufw status. If the status is
active, ensure that port 22 is allowed by running
sudo ufw allow ssh. In some cases, users accidentally lock themselves out by enabling the firewall before allowing SSH; if this happens, you may need to access the VM locally through the VirtualBox console to fix the rules.
It is also worth checking if the SSH daemon is listening on the correct interface. Run
netstat -tulpn | grep :22 or
ss -tulpn | grep :22 to see if sshd is bound to all interfaces (0.0.0.0) or just localhost (127.0.0.1). If it is only bound to localhost, external connections will fail. Modify
/etc/ssh/sshd_config and ensure the
ListenAddress directive is commented out or set to
0.0.0.0 to accept connections on all network interfaces.
Adjusting Host System Settings
Sometimes the issue lies not within the VM but on the host operating system. Windows hosts, for example, have their own firewall that might block outbound connections to specific ports. Ensure that your host's firewall allows outgoing TCP connections on the port you are using for SSH. Additionally, check if any antivirus software is interfering with the network stack. Temporarily disabling such software can help isolate whether it is the cause of the timeout.
Another critical factor is the MTU (Maximum Transmission Unit) size. If there is a mismatch between the host, the VM, and the network hardware, packets may be dropped silently, leading to timeouts. You can test this by pinging the VM from the host with varying packet sizes. Use
ping -s 1472 and reduce the size until it succeeds. If you find a limit, adjust the MTU settings in the VirtualBox network adapter advanced options or within the Ubuntu networking configuration.
For persistent issues, consider reviewing the SSH logs inside the VM. Running
sudo tail -f /var/log/auth.log while attempting a connection from the host can provide real-time feedback on why the connection is failing. Look for messages indicating authentication failures, network unreachable errors, or connection resets. This log data is invaluable for diagnosing subtle configuration errors that standard troubleshooting steps might miss.
If you encounter permission denied errors during file transfers after establishing SSH, refer to our guide on
Fixing Permission Denied Error in Vagrant with Ubuntu Jammy64 for related insights on user permissions. Similarly, if you are setting up multiple Ubuntu machines, ensuring proper SSH key exchange is vital, as detailed in
Setup SSH Connection with 3 Ubuntu Machines to Bypass a Firewall.
Frequently Asked Questions
Why does SSH work locally but not from the host?
This usually indicates that the SSH daemon is bound only to localhost or that the network adapter is not configured for host-guest communication. Ensure
ListenAddress is set correctly in
sshd_config and that Port Forwarding is defined in VirtualBox NAT settings.
How do I reset the SSH port forwarding in VirtualBox?
Go to
Settings >
Network >
Advanced >
Port Forwarding. Delete existing rules and add a new one mapping host port 2222 to guest port 22 with TCP protocol. Restart the VM to apply changes.
Can I use Bridged Adapter instead of NAT?
Yes, Bridged Adapter makes the VM visible on your local network. Select it in the
Attached to dropdown. This eliminates the need for port forwarding but requires the VM to obtain an IP via DHCP from your router.
What if UFW blocks SSH after enabling it?
Access the VM via the VirtualBox graphical console. Run
sudo ufw allow ssh to permit incoming connections. If locked out, you may need to disable UFW temporarily with
sudo ufw disable to regain access.
How do I check if SSH is listening on all interfaces?
Use the command
ss -tulpn | grep :22. If the output shows
127.0.0.1:22, it is restricted. Change
ListenAddress in
/etc/ssh/sshd_config to
0.0.0.0 and restart the SSH service.