How to Set Up VSCode for Remote Development on Ubuntu via SSH
This guide explains how to set up Visual Studio Code (VSCode) for remote development on an Ubuntu machine using SSH.
1. Install VSCode and Remote Development Extensions
- Ensure you have Visual Studio Code installed on your local machine. If not, you can download it from here.
- Install the Remote - SSH extension in VSCode. You can do this by:
- Opening VSCode.
- Clicking on the Extensions view icon on the sidebar.
- Searching for Remote - SSH and installing it.
2. Enable SSH on Ubuntu
- Open a terminal on your Ubuntu machine.
- Install the SSH server by running the following commands:
1 2
sudo apt update sudo apt install openssh-server
- Verify that the SSH service is running:
1
sudo systemctl status ssh
3. Find Your Ubuntu machine’s IP Address
- On Ubuntu, run the following command to get the IP address:
ip a
- Note the IP address shown (e.g.,
192.168.x.x
).
4. Configure SSH on Your Local Machine
A. Generate an SSH Key Pair (Local Machine)
- Open a terminal or command prompt on your local machine.
- Run the following command to generate an SSH key pair:
1
ssh-keygen
- Follow the prompts:
- Press Enter:
Enter file in which to save the key:
Enter(default)Enter passphrase (empty for no passphrase):
EnterEnter same passphrase again:
Enter - The key will be saved in:- Linux:
~/.ssh/<id_name>
- Windows:
C:\Users\<username>\.ssh\<id_name>
B. Copy the SSH Public Key to Ubuntu
- Linux:
- Run the following command to copy your public key to the Ubuntu machine:
1
ssh-copy-id <ubuntu_user_name>@<ubuntu_ip>
- Replace
<ubuntu_username>
and<ubuntu_ip>
with your Ubuntu username and IP address. - Enter the password when prompted.
- Run the following command to copy your public key to the Ubuntu machine:
- Windows:
- Display your public key:
1
type C:\Users\<Username>\.ssh\<id_name>.pub
- Connect to the Ubuntu machine via SSH:
1
ssh <ubuntu_username>@<ubuntu_ip>
- Create the
.ssh
directory and add the public key:1 2
mkdir -p ~/.ssh nano ~/.ssh/authorized_keys
Paste the public key into the
authorized_keys
file. Save and exit.
- Display your public key:
authorized_keys
is a fixed filename. If you have many keys, please save all of them in this file.
C. Test SSH Access:
- Verify that you can SSH into the Ubuntu machine without a password:
1
ssh <ubuntu_username>@<ubuntu_ip>
5. Configure VSCode to Connect via SSH
- Open VSCode.
- Press
Ctrl + Shift + P
to open the Command Palette. - Type and select Remote-SSH: Connect to Host….
- Enter the following:
1
<ubuntu_username>@<ubuntu_ip>
- Replace
<ubuntu_username>
and<ubuntu_ip>
with your Ubuntu username and IP address.
Optional A: Save Connection
- You can save this SSH connection for easier access in the future:
- Go to File > Preferences > Settings.
- Search for Remote.SSH: Config File.
- Linux:
~/.ssh/config
- Windows:
C:\Users\<username>\.ssh\config
1 2 3 4 5
# For Linux Host Ubuntu HostName <ubuntu_ip> User <ubuntu_username> IdentityFile ~/.ssh/<id_name>`
1 2 3 4 5
# For Windows Host Ubuntu HostName <ubuntu_ip> User <ubuntu_username> IdentityFile C:\Users\<username>\.ssh\config
This way, you can simply type Ubuntu
in the Remote-SSH: Connect to Host command.
Optional B: Fix “Connection Refused” Errors
The “Connection Refused” error maybe show up when trying to SSH into the server. When the connecting is cut down while, the SSH service will down automatically.
- Edit the SHH service setting
/etc/ssh/sshd_config
:1
sudo nano /etc/ssh/sshd_config
- Update the following settings:
1 2
ClientAliveInterval 60 ClientAliveCountMax 3
- Restart the SSH service:
1
sudo systemctl restart sshd
6. Start Editing on Ubuntu
You are now ready to use Visual Studio Code for remote development on your Ubuntu machine!
This post is licensed under CC BY 4.0 by the author.