How to SSH into a VirtualBox VM
Last update: 06-29-2024
SSH (Secure Shell) is a powerful tool that allows you to access your virtual machines (VMs) remotely and securely. This guide will walk you through the steps to SSH into a VirtualBox VM using two different network configurations: Bridged Connection and NAT Network. Understanding these options and how they affect your SSH setup can help you choose the best configuration for your needs.
Starting Your VM
- Start your VM.
- Update your package list:
sudo apt update
- Install the OpenSSH server:
sudo apt install openssh-server
Option 1 - Bridged Connection
A bridged connection places your VM on the same network as your host machine, allowing it to have its own IP address within your local network. This makes it easy to SSH into your VM from any device on the same network.
- In your VM's menu, go to Machine > Settings.
- From the options on the left, choose Network.
- Under Adapter 1, change the Attached to value to Bridged Adapter.
- A second box appears named Name. Choose the option according to how your host is connected to the internet:
- If connected through WiFi, choose the option ending with "Wireless Adapter".
- If using a cable, choose the option ending with "Ethernet Adapter".
- Click OK and wait for a minute.
- Run the following command to see the IP address of the VM:
ip a
- Open the terminal on your host machine and check its IP address:
- For Linux, run:
ip a
- For Windows, run:
ipconfig
- For Linux, run:
- The host and guest should be on the same subnet.
- SSH into your VM (replace username with your Linux user and VMIP with the VM's IP address):
ssh username@VMIP
- You might get a message saying "The authenticity of host can't be established. ECDSA key fingerprint is...". Type yes and press Enter.
- Provide the user password, and you will be connected.
Option 2 - NAT Network
Using a NAT (Network Address Translation) network keeps your VM isolated from your local network, making it more secure by preventing direct access to your VM from other devices on the network. However, you can still SSH into your VM by setting up port forwarding.
- In your VM's menu, go to Machine > Settings.
- From the options on the left, choose Network.
- Under Adapter 1, change the Attached to value to NAT.
- Click on Advanced to show additional options and choose Port Forwarding.
- Add a new entry by clicking on the green button and set both the Host Port and Guest Port to 22.
- Port 22 is the port listening for incoming SSH requests. Once forwarded, you can SSH into your localhost, and the request will be forwarded to the VM.
- Click OK.
- Back in the host's terminal, run (replace username with your Linux user):
ssh username@localhost
- If it's a VM you've never connected to before, confirm its authenticity by typing yes.
- Type in your password, and you will be connected.
Conclusion
Both Bridged and NAT network configurations have their own advantages. A bridged connection is straightforward and allows easy access to your VM from anywhere on your local network, making it ideal for development and testing environments. On the other hand, a NAT network offers an additional layer of security by isolating your VM from the local network, which can be beneficial in production environments where security is a higher priority. By understanding these configurations and their implications for SSH, you can choose the best setup for your specific needs.