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
- Open a terminal (on macOS/Linux) or Command Prompt/PowerShell (on Windows).
- Run the following command to generate a new SSH key pair:bashCopyEdit
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" - 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.
- Press Enter to accept the default file location (
Step 2: Copy the Public Key to the Server
- Use the following command to copy the public key to the server:bashCopyEdit
ssh-copy-id user@your-server-ipReplace:userwith your server username.your-server-ipwith your server’s IP address.
- If
ssh-copy-idis unavailable, manually copy the key:- Display the public key:bashCopyEdit
cat ~/.ssh/id_rsa.pub - Copy the output.
- Log in to the server using your password:bashCopyEdit
ssh user@your-server-ip - Add the key to the
~/.ssh/authorized_keysfile on the server:bashCopyEditmkdir -p ~/.ssh echo "paste_your_public_key_here" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh
- Display the public key:bashCopyEdit
Step 3: Test the Connection
- Log out of the server.
- Reconnect using:bashCopyEdit
ssh user@your-server-ip - If successful, no password will be required.
3. Install the Remote Development Extension
- Open VS Code on your local machine.
- Go to the Extensions view (click the Extensions icon on the left sidebar or press
Ctrl+Shift+X/Command+Shift+X). - Search for and install the Remote – SSH extension by Microsoft.
4. Configure the SSH Connection in VS Code
- Open the command palette (
Ctrl+Shift+P/Command+Shift+P). - Type
Remote-SSH: Add New SSH Hostand select it. - Enter the SSH connection string:plaintextCopyEdit
user@your-server-ip - Select the SSH configuration file to update (e.g.,
~/.ssh/config). - Add the following configuration to the file for passwordless login:plaintextCopyEdit
Host your-server-alias HostName your-server-ip User user IdentityFile ~/.ssh/id_rsa
5. Connect to the Remote Server
- Open the command palette and type
Remote-SSH: Connect to Host. - Select your server from the list.
- VS Code will establish a connection and install the VS Code Server on the remote machine.
6. Install Essential Tools on the Remote Server
- Use the integrated terminal in VS Code (or SSH manually) to install tools:
- Update the package list:bashCopyEdit
sudo apt update - Install development tools, e.g.:bashCopyEdit
sudo apt install git -y sudo apt install python3 python3-pip -y sudo apt install nodejs npm -y
- Update the package list:bashCopyEdit
7. Sync Extensions
- Many VS Code extensions need to be installed on the remote server.
- 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:
- Install language-specific debuggers like the Python or Node.js Debugger.
- Configure the debugger using
.vscode/launch.json.
- Port Forwarding:
- Use the command palette and search for
Remote-SSH: Forward Port. - Forward ports (e.g.,
8080for a web app):plaintextCopyEditLocalForward 8080 localhost:8080
- Use the command palette and search for
9. Disconnect and Reconnect
- To disconnect, click the green Remote indicator in the bottom-left corner and select Close Remote Connection.
- To reconnect later, use the
Remote-SSH: Connect to Hostcommand.
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.
