Improve Your Dev Env with VirtualBox + VSCode! A Guide to SSH Connection and One-Click Launch Automation.
Posted on
Updated on
Read time
min(3982 words)
Introduction
Are you using VirtualBox on Windows for Linux development and experiencing these common frustrations?
- "Starting the VM, opening VSCode, establishing SSH connection... the preparation is tedious every time!"
- "I created a development profile in VSCode, but it opens with default settings when SSH connecting..."
- "I don't use the VM's GUI, but it feels like a waste of resources and windows..."
This article introduces methods to solve these issues and significantly improve your development workflow. We'll guide you through smoothly connecting from your host OS's VSCode-based editor to your guest OS (we'll use Linux Mint as an example, but these steps can be applied to other Linux distributions), ultimately automating the entire process from VM startup (headless), SSH connection, to VSCode launch with just one desktop shortcut.
📌 Prerequisites
- Host OS: Windows 11
- Guest OS: Linux on VirtualBox (we'll use Linux Mint 22 as an example, but similar steps apply to other Debian-based distributions like Ubuntu)
- Editor (Host OS): VSCode or its derivatives (referred to as VSCode in this article)
- VirtualBox Guest Additions must be installed on the guest OS.
⚠️ Disclaimer
- The content of this article is based on a specific verification environment (laboratory development environment) and may not work similarly in your environment.
- When implementing these steps, please take appropriate safety measures such as creating VM snapshots and backing up files according to your environment.
- The information in this article is current as of the writing date. Content may become outdated or procedures may change due to version updates of various tools and software.
- Please refer to the official documentation of VirtualBox, VSCode, SSH, and your Linux distribution as needed.
☁️ 1. Preparing SSH Connection from VSCode to Guest OS
First, let's set up SSH connection from your host OS's VSCode to your Linux environment on the guest OS.
1.1. Changing VirtualBox Network Settings
To accept SSH connections, we'll set up port forwarding in VirtualBox's network settings.
- Open VirtualBox Manager, select your target virtual machine (e.g., Linux Mint), and shut it down.
- Open the virtual machine's "Settings" and navigate to the "Network" section.
- Configure "Adapter 1" (or your chosen adapter) as follows:
- Attached to:
NAT(By selecting NAT, the guest OS can share the host OS's IP address for external network access while allowing specific communications from host to guest through port forwarding.) - Expand "Advanced" and click the "Port Forwarding" button.
- Add a new rule (click the
+button on the right) with the following settings (guest IP can be left blank):
- Attached to:
Port forwarding settings are as follows:
| Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
|---|---|---|---|---|---|
| SSH | TCP | 2222 | 22 |
- Host Port
2222: The port that will listen for SSH connections on the host OS. You can change this to any number that doesn't conflict with other services. - Guest Port
22: The standard port that the guest OS's SSH server listens on.
1.2. Setting up SSH Server and Auto-start on Guest OS
Next, let's install the SSH server on the guest OS (Linux Mint) and configure it to start automatically.
- Start the target virtual machine.
- Open the guest OS's terminal and run the following commands to install the OpenSSH server:
- Start the SSH service and configure it to start automatically on boot:
- Check the SSH service status. If it shows
active (running), it's working correctly:
- Verify that auto-start is enabled. If it shows
enabled, you're good to go:
1.3. Guest OS Firewall Settings (UFW)
If the firewall (UFW: Uncomplicated Firewall) is enabled on the guest OS, you need to allow SSH connections.
- Allow connections to the SSH port (default is 22):
- Reload the settings:
- Check the firewall status and rules. If
22/tcp(or22) showsALLOW IN Anywhere, you're set:
If ufw is not installed, install it with sudo apt install ufw and enable it with sudo ufw enable.
1.4. Verifying SSH Connection (from Host OS)
Check if you can establish an SSH connection to the guest OS from your host OS (Windows) terminal (Command Prompt or PowerShell).
- Replace
your_guest_usernamewith your guest OS username. -p 22222is the host port we set in VirtualBox's port forwarding.
On first connection, you'll be asked to verify the fingerprint - type yes, then enter your guest OS user password. If you can log in, it's successful.
1.5. VSCode SSH Connection Setup
Let's configure VSCode for easy SSH connection.
-
Start VSCode on your host OS.
-
Install Remote - SSH Extension:
If not already installed, search for and install "Remote - SSH" (ms-vscode-remote.remote-ssh) from VSCode's extension marketplace:
-
Configure SSH Config File:
Add SSH connection settings to your SSH Config file. This file is typically located in the
.sshfolder under your host OS user directory (e.g.,C:\Users\[username]\.ssh\config). Create it if it doesn't exist. Add or create the following content:
your_ssh_alias: Any connection name (e.g.,vbox-mint). This will appear in VSCode's connection list.your_guest_username: Your guest OS username.Port 22222: The host port we set in VirtualBox's port forwarding.
- Connect via SSH from VSCode:
- Click the green "Open Remote Window" icon (usually shaped like
><) in the bottom-left corner of VSCode. - Select "Connect to Host..." from the dropdown menu.
- Select the
your_ssh_aliaswe configured in the SSH Config file. - On first connection, you might be asked about the OS type. Select "Linux".
- A new VSCode window will open, asking for your guest OS user password. If you can connect after entering the password, it's successful. You'll see
SSH: your_ssh_aliasin the bottom-left corner.
- Click the green "Open Remote Window" icon (usually shaped like
Now you can access the guest OS's file system from your host OS's VSCode and use the guest OS's terminal directly.
Current Challenges:
- Password input required for each SSH connection (this can be improved with SSH key authentication, but we won't cover that in this article).
- Extra steps needed when you want to use specific VSCode development profiles (extension sets, etc.) for the connection.
- Guest OS needs to be started manually from VirtualBox Manager, and the GUI window is displayed.
Let's proceed to the next section to solve these challenges through automation.
🤖 2. Automating Startup and Connection
Our goal is to perform the following processes automatically with just one click on a desktop shortcut:
- Start the VirtualBox guest OS in headless mode (no GUI).
- Wait for the guest OS's SSH server to start.
- Launch VSCode with the specified profile and workspace, connected to the guest OS via SSH.
2.1. Preparing VBoxManage Command Line Tool
To control VirtualBox from the command line, we need to add VBoxManage.exe to the PATH.
- Open Windows "Settings" → "System" → "About" and click "Advanced system settings".
- Click the "Environment Variables" button.
- Select "Path" under "System variables" and click "Edit".
- Click "New" and add the VirtualBox installation directory (typically
C:\Program Files\Oracle\VirtualBox). - Click OK to close all dialogs, and restart your PC or open a new Command Prompt/PowerShell window to apply the settings.
- Run
VBoxManage --versionin Command Prompt or PowerShell to verify the path is set correctly. - Note down the VM name for use in the automation script:
From the displayed list, note the target VM's name (e.g., "Linux Mint 22" or "MyLinuxVM", including the quotes). We'll refer to this VM name as Your_VM_Name in the following steps.
2.2. Setting up Guest OS SSH Ready Notification
Let's build a mechanism for the guest OS to notify the host OS when it's ready for SSH connection. We'll use VirtualBox's Guest Properties feature.
-
Start the target virtual machine.
-
Guest OS: Create SSH Ready Notification Script
Create a script in the guest OS's home directory or similar location to check SSH service startup and set the Guest Property. For example, create
~/bin/set-ssh-ready.shwith the following content. Create the~/bindirectory if it doesn't exist.
Make the script executable:
-
Guest OS: Create systemd Service File
Create a systemd service unit file to run the above script after network and SSH service startup.
Open an editor with
sudo nano /etc/systemd/system/set-ssh-ready.serviceand add the following content. Replaceyour_guest_usernamewith your actual guest OS username.
- Important: Adjust the
ExecStartpath according to your actual script location and username. - Adding
sshd.serviceto theAfterdirective helps handle different SSH daemon service names across distributions.
- Guest OS: Enable and Verify systemd Service
Make systemd recognize the new service file and enable auto-start:
- Reboot the guest OS:
sudo reboot - After reboot, verify that the service ran once (OK if
inactive (dead)withstatus=0/SUCCESS) and the Guest Property was set:
If it's not showing active (running) in green or Value: true, check the script path, permissions, service file content, and Guest Additions installation status.
-
Host OS: Verify Guest Property
From the host OS's Command Prompt or PowerShell, verify that the guest OS's property is set correctly. Replace
Your_VM_Namewith your actual VM name.
2.3. Host OS: Create VSCode Workspace File (Optional)
Creating a workspace file is convenient for opening specific project folders in VSCode. This specifies the folder on the guest OS to open after SSH connection.
- Create a file with
.code-workspaceextension in any location on the host OS (e.g.,C:\Users\[username]\Documents\VSCodeWorkspaces). - Add the following JSON content to the file:
- Make sure to set the
uripath andremoteAuthorityaccurately according to your environment. - The VM shutdown task in
taskscan be executed from VSCode's command palette (Ctrl+Shift+P) by selectingTasks: Run Taskand thenShutdown Guest VM. Note that this runs as a command on the host OS.
2.4. Host OS: Create PowerShell Script for Auto-start
Let's create a PowerShell script to automate VM startup, SSH ready waiting, and VSCode launch.
-
Create a
.ps1extension script file in any location on the host OS (e.g.,C:\Users\[username]\Scripts). -
Add the following script content.
Make sure to replace all
<...>placeholders with your actual environment settings.
- Create Desktop Shortcut:
- Right-click on the desktop, select "New" → "Shortcut".
- In the "Type the location of the item" field, enter the following. Replace
<Path_To_Your_Script_File.ps1>with the full path to the PowerShell script you created above.
Example: powershell.exe -ExecutionPolicy Bypass -File "C:\Users\YourUser\Scripts\launch-linux-dev-env.ps1"
- Click "Next", give the shortcut any name (e.g.,
Launch Linux Dev Environment), and click "Finish".
2.5. Testing and Verification
- Double-click the desktop shortcut you created to run it.
- A PowerShell window will open, showing log messages as the process progresses.
- VM starts (no VirtualBox GUI since it's headless).
- Waits for SSH readiness.
- VSCode launches.
- If VSCode launches with the specified profile, connected to the guest OS via SSH (shown as
SSH: your_ssh_aliasin the bottom-left corner), and opens the specified workspace, it's successful. You might see password input or confirmation dialogs on first connection or with new profiles.
If it doesn't work, check these points:
- Verify all paths in the PowerShell script (VM name, VSCode executable path, workspace path).
- Check PowerShell execution policy (we're temporarily allowing with
-ExecutionPolicy Bypass, but you can check permanent settings withSet-ExecutionPolicy). - Ensure VBoxManage command is in PATH and works correctly.
- Verify guest OS's
set-ssh-ready.shscript andset-ssh-ready.servicesettings, permissions, and paths. - Check if Guest Additions is properly installed and working on the guest OS.
- Verify VirtualBox network settings (port forwarding).
- Check guest OS firewall settings.
- Verify VSCode SSH Config file content.
- Check error messages displayed in the PowerShell window.
💬 Conclusion
With these settings, your development environment is ready with just one desktop shortcut.
Improved Development Workflow Example
- Run the created shortcut.
- VSCode launches and establishes SSH connection to the guest OS (password input required on first connection or without key setup).
- Start coding in VSCode.
- Launch development servers (e.g.,
npm run dev,python manage.py runserver) in VSCode's terminal (this is the guest OS's terminal).
- If the development server listens on a specific port (e.g., 3000, 8000) on the guest OS, VSCode's Remote-SSH feature often automatically forwards it to the same port on the host OS's localhost. You can check and manually configure this in the "Ports" tab.
- Access
http://localhost:3000orhttp://localhost:8000from the host OS's browser to verify operation. - Shut down the VM when done.
- From VSCode's command palette (
Ctrl+Shift+P), runTasks: Run Taskand selectShutdown Guest VM(task defined in the workspace file from 2.3). - Or use the shutdown shortcut described below.
Additional Useful Tips
-
SSH Key Authentication Setup:
Strongly recommended to avoid password input every time. Create an SSH key pair on the host OS and register the public key in the guest OS's
~/.ssh/authorized_keys. -
Customize Development Server Launch Tasks (.vscode/tasks.json):
Create a
.vscode/tasks.jsonfile in your project's root to define tasks for launching multiple development servers from VSCode. For example, here's how to define a task to launch frontend and backend servers simultaneously (modify content according to your project):
Run from command palette with Tasks: Run Task → Start All Dev Servers.
-
VM Shutdown Shortcut:
Create a dedicated shortcut on the desktop for quick VM shutdown.
- "Right-click on desktop" → "New" → "Shortcut"
- In "Type the location of the item", enter the following (replace
Your_VM_Namewith your actual VM name):
- Click "Next", give the shortcut any name (e.g.,
VM Shutdown), and click "Finish".
Q&A
-
Q: Some VSCode extensions don't work after Remote-SSH connection.
A: In Remote-SSH, extensions are categorized as "UI Extensions" (run locally) and "Workspace Extensions" (run on the remote SSH server). Check if required extensions are installed on the remote side (guest OS). In the extensions sidebar, check where each extension is installed and select "Install on SSH:
your_ssh_alias" if needed. -
Q: VSCode doesn't recognize Python interpreter or Node.js on the guest OS.
A: Verify that the VSCode window is connected to the remote server (shown as
SSH: your_ssh_aliasin the bottom-left corner). Then, from VSCode's command palette, check if the correct version is selected withPython: Select Interpreteror for Node.js, verify withnvmornodenvin the guest OS terminal. You might need to explicitly specify paths in VSCode settings (settings.json).
This article presents one solution for automation in my environment. Find the best method for your environment and enjoy a better development life!
