Code in VScode¶
Overview¶
This guide will walk you through how to connect to a remote server using Visual Studio Code (VScode). This is useful if you want to edit files on a remote server without having to use a terminal or a text editor like vim or nano. You can also setup keybindings to run code on the remote server, which is useful to document your code while you are creating and testing it.
Steps¶
1. Install the Remote - SSH extension¶
- Open VScode
- Click on the Extensions icon on the left-hand side of the window
- Search for
Remote - SSHin the search bar - Click on the green
Installbutton
2. Connect to the remote server¶
- Click on the
Remote Explorericon on the left-hand side of the window (it looks like a little monitor) - Click on the
+icon at the top of the window - Enter the username and IP address/server id of the server you want to connect to (e.g.
username@ip_address, e.g.<user_id>@login8.hpc.binf.unibe.ch-IBU or<user_id>@biolpc045604- p910) - Select the SSH configuration file you want to use and update (e.g.
~/.ssh/config) - Once you receive Host added message, click
Connect, you will be prompted to enter your password - Once connected you need to open a folder on the remote server. Click on the
Open Folderbutton and select the folder you want to open. You will be prompted to enter your password again. - You can now Open a terminal on the remote server by clicking on the
Terminalmenu and selectingNew Terminal. Now you can run commands on the remote server, while saving and editing files (e.g. README.md) in VScode.
3. Set keybindings to run code on the remote server¶
Unix/Linux:
This step is useful if you want to document your code while you are creating and testing it.
1. Click on the File menu and select Preferences -> Keyboard Shortcuts
2. Search for Terminal: Run Selected Text in Active Terminal and click on the pen icon to add a keybinding
3. Enter the keybinding you want to use (e.g. Ctrl+Enter)
R:
- Create a dedicated R environment on the remote server using
condato install packages and run R code. - install.packages("languageserver") in the above R to enable code completion and syntax highlighting in VScode.
- Install the
RextensionREditorSupport.rin VScode to run R code on the remote server. - Click on the
Filemenu and selectPreferences->Settingsand search for@ext:REditorSupport.r R: Always use Active Terminaland check the box. - Follow the steps here: to make vscode mimic RStudio.
4. Passwordless SSH Access in VS Code¶
If you want to avoid entering your password every time you connect to the remote server, you can set up passwordless SSH access. This involves generating SSH keys and configuring your local and remote machines to use them for authentication.
1. Configure Your Local SSH Client
-
Open your SSH configuration file, from vscode. Go to Yiew -> Command Palette -> type
Remote-SSH: Open SSH Configuration Fileand select the file you want to edit.
If the file doesn't exist, create it. -
Add the following configuration for your remote server:
plaintext Host <host> HostName <host> User <username> IdentityFile "path/to/.ssh/keys/<host>_rsa"Replace the placeholders: -<host>: The remote server's hostname or IP address. -<username>: Your username on the remote server.
2. Generate SSH Keys
-
Generate a public/private key pair:
bash ssh-keygen -q -b 2048 -P "" -f path/to/.ssh/keys/<host>_rsa -t rsaThis creates: -path/to/.ssh/keys/<host>_rsa(private key) -path/to/.ssh/keys/<host>_rsa.pub(public key) -
Ensure the keys are stored securely:
bash chmod 700 path/to/.ssh chmod 600 path/to/.ssh/keys/<host>_rsa
3. Copy the Public Key to the Remote Server
You can use any method to transfer the public key to the remote server:
Using scp
bash
scp path/to/.ssh/keys/<host>_rsa.pub <username>@<host>:~/
4. Configure the Remote Server
-
Log in to the remote server:
bash ssh <username>@<host> -
Set up the public key for SSH authentication:
bash mkdir -p ~/.ssh cat ~/host_rsa.pub >> ~/.ssh/authorized_keys chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
If authorized_keys doesn’t exist:
bash
mv ~/host_rsa.pub ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
5. Test SSH Connection
- On your local machine, test the connection:
bash ssh <username>@<host>If configured correctly, you should log in without entering a password.
6. Set Up VS Code
-
Open VS Code and install the Remote - SSH extension from the Extensions Marketplace.
-
Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P) and select:Remote-SSH: Connect to Host... -
Choose the host configured in
path/to/.ssh/config. You should connect to your server without entering a password.
Troubleshooting
- Permission Issues: Ensure that
.sshand key files have the correct permissions: .ssh:700- Private key:
600 authorized_keys:600