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

  1. Start your VM.
  2. Update your package list:
    sudo apt update
  3. 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.

  1. In your VM's menu, go to Machine > Settings.
  2. From the options on the left, choose Network.
  3. Under Adapter 1, change the Attached to value to Bridged Adapter.
  4. 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".
  5. Click OK and wait for a minute.
  6. Run the following command to see the IP address of the VM:
    ip a
  7. Open the terminal on your host machine and check its IP address:
    • For Linux, run:
      ip a
    • For Windows, run:
      ipconfig
  8. The host and guest should be on the same subnet.
  9. SSH into your VM (replace username with your Linux user and VMIP with the VM's IP address):
    ssh username@VMIP
  10. You might get a message saying "The authenticity of host can't be established. ECDSA key fingerprint is...". Type yes and press Enter.
  11. 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.

  1. In your VM's menu, go to Machine > Settings.
  2. From the options on the left, choose Network.
  3. Under Adapter 1, change the Attached to value to NAT.
  4. Click on Advanced to show additional options and choose Port Forwarding.
  5. Add a new entry by clicking on the green button and set both the Host Port and Guest Port to 22.
  6. 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.
  7. Click OK.
  8. Back in the host's terminal, run (replace username with your Linux user):
    ssh username@localhost
  9. If it's a VM you've never connected to before, confirm its authenticity by typing yes.
  10. 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.

0 Comments

Add a new comment: