Setting Up VS Code for Remote Development on an Ubuntu Linux Server with SSH Key Authentication

Follow these steps to configure Visual Studio Code (VS Code) for remote development on an Ubuntu Linux server, including setting up SSH key-based authentication for a passwordless login.


1. Prerequisites

Before you start:

  • VS Code installed on your local machine (macOS or Windows).
  • An Ubuntu server with SSH access (e.g., cloud server or local VM).
  • Basic understanding of SSH and server management.
  • OpenSSH installed on both your local machine and the Ubuntu server (pre-installed on most systems).

2. Set Up SSH Key-Based Authentication

Step 1: Generate an SSH Key Pair on Your Local Machine

  1. Open a terminal (on macOS/Linux) or Command Prompt/PowerShell (on Windows).
  2. Run the following command to generate a new SSH key pair:bashCopyEditssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  3. When prompted:
    • Press Enter to accept the default file location (~/.ssh/id_rsa).
    • Enter a passphrase (optional but recommended) or leave it blank for no passphrase.

Step 2: Copy the Public Key to the Server

  1. Use the following command to copy the public key to the server:bashCopyEditssh-copy-id user@your-server-ip Replace:
    • user with your server username.
    • your-server-ip with your server’s IP address.
  2. If ssh-copy-id is unavailable, manually copy the key:
    • Display the public key:bashCopyEditcat ~/.ssh/id_rsa.pub
    • Copy the output.
    • Log in to the server using your password:bashCopyEditssh user@your-server-ip
    • Add the key to the ~/.ssh/authorized_keys file on the server:bashCopyEditmkdir -p ~/.ssh echo "paste_your_public_key_here" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh

Step 3: Test the Connection

  1. Log out of the server.
  2. Reconnect using:bashCopyEditssh user@your-server-ip
  3. If successful, no password will be required.

3. Install the Remote Development Extension

  1. Open VS Code on your local machine.
  2. Go to the Extensions view (click the Extensions icon on the left sidebar or press Ctrl + Shift + X / Command + Shift + X).
  3. Search for and install the Remote – SSH extension by Microsoft.

4. Configure the SSH Connection in VS Code

  1. Open the command palette (Ctrl + Shift + P / Command + Shift + P).
  2. Type Remote-SSH: Add New SSH Host and select it.
  3. Enter the SSH connection string:plaintextCopyEdituser@your-server-ip
  4. Select the SSH configuration file to update (e.g., ~/.ssh/config).
  5. Add the following configuration to the file for passwordless login:plaintextCopyEditHost your-server-alias HostName your-server-ip User user IdentityFile ~/.ssh/id_rsa

5. Connect to the Remote Server

  1. Open the command palette and type Remote-SSH: Connect to Host.
  2. Select your server from the list.
  3. VS Code will establish a connection and install the VS Code Server on the remote machine.

6. Install Essential Tools on the Remote Server

  1. Use the integrated terminal in VS Code (or SSH manually) to install tools:
    • Update the package list:bashCopyEditsudo apt update
    • Install development tools, e.g.:bashCopyEditsudo apt install git -y sudo apt install python3 python3-pip -y sudo apt install nodejs npm -y

7. Sync Extensions

  1. Many VS Code extensions need to be installed on the remote server.
  2. When prompted in VS Code, click Install on Remote to enable extensions for your project’s language (e.g., Python, Node.js).

8. Debug and Port Forward

  • Debugging:
    1. Install language-specific debuggers like the Python or Node.js Debugger.
    2. Configure the debugger using .vscode/launch.json.
  • Port Forwarding:
    1. Use the command palette and search for Remote-SSH: Forward Port.
    2. Forward ports (e.g., 8080 for a web app):plaintextCopyEditLocalForward 8080 localhost:8080

9. Disconnect and Reconnect

  1. To disconnect, click the green Remote indicator in the bottom-left corner and select Close Remote Connection.
  2. To reconnect later, use the Remote-SSH: Connect to Host command.

10. Optimize Workflow

  • Use Settings Sync in VS Code to synchronize your settings, extensions, and themes across devices.
  • Use VS Code’s built-in Git tools to manage version control directly on the remote server.

By following these instructions, you’ll set up a seamless environment for developing on a remote Ubuntu Linux server, ensuring security and ease of use with SSH key-based authentication.

Scroll to Top