How to Connect Virtual box with Visual Studio Code to Edit Code?

What is Virtual Box?

Oracle VM virtual box is cross-platform virtualization application and it is free to download. It can be installed on your existing PC no matter what OS (Window, Mac OS, Linux or Solaris) it is running. Besides, your PC can run multiple OS at the same time.

Table Of Contents

Steps to install virtual box:

1. Go to https://www.virtualbox.org
2. Click Download button
download virtual box
3. Choose platform packages that you wish to download (Window, Macintosh, Linux or Solaris)
virtual box platform package

Set up SSH key

SSH (secure socket shell) is a protocol that use encryption to secure connection between client and server. You can use SSH to connect to remote server and exchange data.

If you don’t have SSH key pair on local machine, open terminal and input command below:
ssh-keygen -t rsa

Press ENTER to accept default location to store the key

Type in passphrase or hit ENTER to accept default (no passphrase)
– If you input passphrase, you need to input second time to continue

Your private key is saved in id_rsa file while your public key is saved in id_rsa.pub file

Add SSH key

Now it’s time to add public key to your remote host

Method 1 :-
Run command below in local machine:
ssh-copy-id remote_username@server_ip_address

Method 2 :-
If ssh-copy-id not available in your local machine, run command below:
cat ~/.ssh/id_rsa.pub | ssh remote_username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Login to server using SSH key

Try connect to the server via SSH:
ssh remote_username@server_ip_address

Configure firewall in Linux

To secure network connection, you can configure a firewall on your server. Uncomplicated Firewall (UFW) is an interface to iptables that is geared towards simplifying the process of configuring a firewall.

To install UFW, type in following command:
apt-get install ufw

Enable firewall:
ufw enable

Allow ssh connection:
ufw allow ssh

Ensure firewall is saved:
ufw status verbose

Check firewall status:
ufw status

Install OpenSSH server on remote host

OpenSSH is the premier connectivity tool for remote login with the SSH protocol. SSH server listen for incoming connection requests.

1. First, you need to update package list by running command below:
sudo apt update

2. Then, install open ssh server package:
sudo apt-get install openssh-server

To check version of openSSH, execute:
ssh -V

To check SSH service is up and running, execute:
sudo systemctl status ssh
SSH connection running

To find your ip address, run command below:
ip a

What is Visual Studio Code?

Visual Studio Code is a free source code editor for Window, Linux and macOS. It include features of debugging, syntax highlighting, intelligent code completion, embedded Git and etc.

How to connect remote host in Visual Studio Code?

Open SSH compatible SSH client installed
SSH client usually your own PC
Visual Studio Code (VSC) installed
Remote Development extension pack installed

1. Select View > Command Palette from top menu bar in VSC
2. Select Remote-SSH: Connect to Host… from command palette
Connect to host

3. Input remote host user@host
– user is username you used in remote host; host is your host IP address
configure SSH host
4. You will be prompted to enter host password
5. A new window is shown after you are connected. Check status bar at bottom left in VSC for host you are connected to. It should be showing IP address of your virtual machine (VM).
status bar
6. You can now open folder on remote machine using File > Open… / File > Open Workspace and edit the code in VM using VSC.

How to close connection in Visual Studio Code?

Go to File > Close Remote Connection in VSC to close the connection to remote machine
close remote connection

Scroll to Top