Connect over SSH with Visual Studio Code (2023)

This tutorial walks you through creating and connecting to a virtual machine (VM) on Azure using the Visual Studio Code Remote - SSH extension. You'll create a Node.js Express web app to show how you can edit and debug on a remote machine with VS Code just like you could if the source code was local.

Note: Your Linux VM can be hosted anywhere - on your local host, on premise, in Azure, or in any other cloud, as long as the chosen Linux distribution meets these prerequisites.

Prerequisites

To get started, you need to have done the following steps:

  1. Install an OpenSSH compatible SSH client (PuTTY is not supported).
  2. Install Visual Studio Code.
  3. Have an Azure subscription (If you don't have an Azure subscription, create a free account before you begin).

Install the extension

The Remote - SSH extension is used to connect to SSH hosts.

Install the Remote - SSH extension

Connect over SSH with Visual Studio Code (1)

Remote - SSH

With the Remote - SSH extension installed, you will see a new Status bar item at the far left.

Connect over SSH with Visual Studio Code (2)

The Remote Status bar item can quickly show you in which context VS Code is running (local or remote) and clicking on the item will bring up the Remote - SSH commands.

Connect over SSH with Visual Studio Code (3)

Create a virtual machine

If you don't have an existing Linux virtual machine, you can create a new VM through the Azure portal. In the Azure portal, search for "Virtual Machines", and choose Add. From there, you can select your Azure subscription and create a new resource group, if you don't already have one.

Note: In this tutorial, we are using Azure, but your Linux VM can be hosted anywhere, as long as the Linux distribution meets these prerequisites.

Connect over SSH with Visual Studio Code (4)

Now you can specify details of your VM, such as the name, the size, and the base image. Choose Ubuntu Server 18.04 LTS for this example, but you can choose recent versions of other Linux distros and look at VS Code's supported SSH servers.

Connect over SSH with Visual Studio Code (5)

(Video) Setup SSH in VS Code to access your server easily

Set up SSH

There are several authentication methods into a VM, including an SSH public/private key pair or a username and password. We recommend using key-based authentication (if you use a username/password, you'll be prompted to enter your credentials more than once by the extension). If you're on Windows and have already created keys using PuttyGen, you can reuse them.

Create an SSH key

If you don't have an SSH key pair, open a bash shell or the command line and type in:

ssh-keygen -t ed25519

This will generate the SSH key. Press Enter at the following prompt to save the key in the default location (under your user directory as a folder named .ssh).

Connect over SSH with Visual Studio Code (6)

You will then be prompted to enter a secure passphrase, but you can leave that blank. You should now have a id_ed25519.pub file which contains your new public SSH key.

Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, you can use rsa instead: ssh-keygen -t rsa -b 4096.

Add SSH key to your VM

In the previous step, you generated an SSH key pair. Select Use existing public key in the dropdown for SSH public key source so that you can use the public key you just generated. Take the public key and paste it into your VM setup, by copying the entire contents of the id_ed25519.pub in the SSH public key. You also want to allow your VM to accept inbound SSH traffic by selecting Allow selected ports and choosing SSH (22) from the Select inbound ports dropdown list.

Connect over SSH with Visual Studio Code (7)

Auto shutdown

A cool feature of using Azure VMs is the ability to enable auto shutdown (because let's face it, we all forget to turn off our VMs…). If you go to the Management tab, you can set the time you want to shut down the VM daily.

Connect over SSH with Visual Studio Code (8)

Select Review and Create, then Create, and Azure will deploy your VM for you!

Once the deployment is finished (it may take several minutes), go to the new resource view for your virtual machine.

Connect using SSH

Now that you've created an SSH host, let's connect to it!

You'll have noticed an indicator on the bottom-left corner of the Status bar. This indicator tells you in which context VS Code is running (local or remote). Click on the indicator to bring up a list of Remote extension commands.

Connect over SSH with Visual Studio Code (9)

Choose the Connect to Host... command in the Remote-SSH section and connect to the host by entering connection information for your VM in the following format: user@hostname.

The user is the username you set when adding the SSH public key to your VM. For the hostname, go back to the Azure portal and in the Overview pane of the VM you created, copy the Public IP address.

(Video) Visual Studio Code Remote Development through SSH

Connect over SSH with Visual Studio Code (10)

Before connecting in Remote - SSH, you can verify you're able to connect to your VM via a command prompt using ssh user@hostname.

Note: If you run into an error ssh: connect to host <host ip> port 22: Connection timed out, you may need to delete NRMS-Rule-106 from the Networking tab of your VM:

Connect over SSH with Visual Studio Code (11)

Set the user and hostname in the connection information text box.

Connect over SSH with Visual Studio Code (12)

VS Code will now open a new window (instance). You'll then see a notification that the "VS Code Server" is initializing on the SSH Host. Once the VS Code Server is installed on the remote host, it can run extensions and talk to your local instance of VS Code.

Connect over SSH with Visual Studio Code (13)

You'll know you're connected to your VM by looking at the indicator in the Status bar. It shows the hostname of your VM.

Connect over SSH with Visual Studio Code (14)

The Remote - SSH extension also contributes a new icon on your Activity bar, and clicking on it will open the Remote explorer. From the dropdown, select SSH Targets, where you can configure your SSH connections. For instance, you can save the hosts you connect to the most and access them from here instead of entering the user and hostname.

Connect over SSH with Visual Studio Code (15)

Once you're connected to your SSH host, you can interact with files and open folders on the remote machine. If you open the integrated terminal (⌃` (Windows, Linux Ctrl+`)), you'll see you're working inside a bash shell while you're on Windows.

Connect over SSH with Visual Studio Code (16)

You can use the bash shell to browse the file system on the VM. You can also browse and open folders on the remote home directory with File > Open Folder.

Connect over SSH with Visual Studio Code (17)

Create your Node.js application

In this step, you will create a simple Node.js application. You will use an application generator to quickly scaffold out the application from a terminal.

(Video) SSH using VSCode to connect to VM or Workstation

Install Node.js and npm

From the integrated terminal (⌃` (Windows, Linux Ctrl+`)), update the packages in your Linux VM, then install Node.js, which includes npm, the Node.js package manager.

sudo apt-get updatecurl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt-get install -y nodejs

You can verify the installations by running:

node --versionnpm --version

Install the Express generator

Express is a popular framework for building and running Node.js applications. You can scaffold (create) a new Express application using the Express Generator tool. The Express Generator is shipped as an npm module and installed by using the npm command-line tool npm.

sudo npm install -g express-generator

The -g switch installs the Express Generator globally on your machine so that you can run it from anywhere.

Create a new application

You can now create a new Express application called myExpressApp by running:

express myExpressApp --view pug

The --view pug parameters tell the generator to use the pug template engine.

To install all of the application's dependencies, go to the new folder and run npm install.

cd myExpressAppnpm install

Run the application

Last, let's ensure that the application runs. From the terminal, start the application using the npm start command to start the server.

npm start

The Express app by default runs on http://localhost:3000. You won't see anything in your local browser on localhost:3000 because the web app is running on your virtual machine.

Port forwarding

To be able to browse to the web app on your local machine, you can leverage another feature called Port forwarding.

To be able to access a port on the remote machine that may not be publicly exposed, you need to establish a connection or a tunnel between a port on your local machine and the server. With the app still running, open the SSH Explorer and find the Forwarded Ports view. Click on the Forward a port link and indicate that you want to forward port 3000:

Connect over SSH with Visual Studio Code (18)

Name the connection "browser":

Connect over SSH with Visual Studio Code (19)

The server will now forward traffic on port 3000 to your local machine. When you browse to http://localhost:3000, you see the running web app.

Connect over SSH with Visual Studio Code (20)

Edit and debug

From the Visual Studio Code File Explorer (⇧⌘E (Windows, Linux Ctrl+Shift+E)), navigate to your new myExpressApp folder and double-click the app.js file to open it in the editor.

(Video) Connect over SSH with Visual Studio Code

IntelliSense

You have syntax highlighting for the JavaScript file as well as IntelliSense with hovers, just like you would see if the source code was on your local machine.

Connect over SSH with Visual Studio Code (21)

When you start typing, you'll get smart completions for the object methods and properties.

Connect over SSH with Visual Studio Code (22)

Debugging

Set a breakpoint on line 10 of app.js by clicking in the gutter to the left of the line number or by putting the cursor on the line and pressing F9. The breakpoint will be displayed as a red circle.

Connect over SSH with Visual Studio Code (23)

Now, press F5 to run your application. If you are asked how to run the application, choose Node.js.

The app will start, and you'll hit the breakpoint. You can inspect variables, create watches, and navigate the call stack.

Press F10 to step or F5 again to finish your debugging session.

Connect over SSH with Visual Studio Code (24)

You get the full development experience of Visual Studio Code connected over SSH.

Ending your SSH connection

You can end your session over SSH and go back to running VS Code locally with File > Close Remote Connection.

Congratulations

Congratulations, you've successfully completed this tutorial!

Next, check out the other Remote Development extensions.

  • WSL
  • Dev Containers

Or get them all by installing theRemote Development Extension Pack.

12/7/2022

FAQs

Can you use VS code over SSH? ›

VS Code Remote SSH

You can connect over SSH into another machine from Visual Studio Code and interact with files and folders anywhere on that remote filesystem. If you have an app located on a different computer, you could use SSH to connect to it and access your app, view its files, and even modify, run, and debug it.

How do I connect to a remote server in Visual Studio? ›

Set up the remote connection
  1. In Visual Studio, choose Tools > Options on the menu bar to open the Options dialog. ...
  2. In the Connection Manager dialog, choose the Add button to add a new connection. ...
  3. Enter the following information: ...
  4. Choose the Connect button to attempt a connection to the remote computer.
Oct 17, 2022

How do I SSH into ec2 VSCode? ›

Connect using SSH
  1. You can find your AWS credentials under AWS Details of your AWS Academy account.
  2. Download the SSH key (labsuser.pem) file to your local computer. ...
  3. Open up Visual Studio Code.
  4. Click on the Open a Remote Window icon at the bottom left-hand corner of the window.
  5. Select Connect to Host.
Mar 13, 2022

What port does Visual Studio code use for SSH? ›

Next time you connect to the remote host, VSCode will use port 5000 , and when you connect via regular ssh from a terminal, you will use port 22 by default, (or specify a custom port via the -p option).

Can you run GUI over SSH? ›

If you know how to use SSH, you've probably employed it for connecting to the command prompt on a remote machine. But did you know that, using a process known as X forwarding, SSH can open remote GUI applications on your desktop?

Can you use GUI over SSH? ›

SSH, the Secure Shell, supports remote login and command-line or GUI access across the network through encrypted tunnels protected by public-key cryptography.

Can you SSH into EC2? ›

To connect from the Amazon EC2 console

Open the Amazon EC2 console. In the left navigation pane, choose Instances and select the instance to which to connect. Choose Connect. On the Connect To Your Instance page, choose EC2 Instance Connect (browser-based SSH connection), Connect.

How do I SSH into an EC2 instance without Keypair? ›

4 Steps To Connect AWS Servers Using SSH Without Key Pairs
  1. Connect to your EC2 instance using the key file. We have to access the server this way again to make changes. ...
  2. Change the configuration of SSH. This is the core step. ...
  3. Restart the service. ...
  4. Change the password of the root user.
Apr 20, 2022

How do I connect VS Code to a remote server? ›

In VS Code, select Remote-SSH: Connect to Host... from the Command Palette (F1, Ctrl+Shift+P) and use the same user@hostname as in step 1. If VS Code cannot automatically detect the type of server you are connecting to, you will be asked to select the type manually.

How to login using SSH? ›

To initiate an SSH connection to a remote system, you need the Internet Protocol (IP) address or hostname of the remote server and a valid username. You can connect using a password or a private and public key pair. Because passwords and usernames can be brute-forced, it's recommended to use SSH keys.

Where does VS Code look for SSH keys? ›

Check to see if you already have an SSH key on your local machine. This is typically located at ~/.ssh/id_ed25519.pub on macOS / Linux, and the .ssh directory in your user profile folder on Windows (for example C:\Users\your-user\.ssh\id_ed25519.pub ).

How do I connect to a remote access server? ›

Remote Desktop to Your Server From a Local Windows Computer
  1. Click the Start button.
  2. Click Run...
  3. Type “mstsc” and press the Enter key.
  4. Next to Computer: type in the IP address of your server.
  5. Click Connect.
  6. If all goes well, you will see the Windows login prompt.
Dec 13, 2019

How to add git SSH key to Visual Studio Code? ›

How to set up Visual Studio Code Remote SSH with GitHub
  1. Step1: Install and configure git.
  2. Step2: Generate SSH key pair.
  3. Step3: Setup SSH access between client and GitHub.
  4. Step4: Create new GitHub repository (Optional)
  5. Step5: Clone GitHub repo to Linux Client.
  6. Step6: Install Visual Studio Code.

How do I access a remote server using IP address? ›

Using Remote Desktop (Windows)
  1. Launch “Remote Desktop Connection” from the Start menu.
  2. Enter the IP address of the computer you obtained earlier and click “Connect”.
  3. At the prompt, select more choices, enter your credentials for the remote computer, and then click “OK”.
Jun 8, 2020

Is SSH obsolete? ›

The ssh-rsa signature scheme has been deprecated since OpenSSH 8.8 which was released in 2021-08-20 (release notes). The reason is as quoted: In the SSH protocol, the "ssh-rsa" signature scheme uses the SHA-1 hash algorithm in conjunction with the RSA public key algorithm.

How do I login as root over SSH? ›

Enable root login over SSH:
  1. As root, edit the sshd_config file in /etc/ssh/sshd_config : Copy. Copied! ...
  2. Add a line in the Authentication section of the file that says PermitRootLogin yes . This line may already exist and be commented out with a "#". ...
  3. Save the updated /etc/ssh/sshd_config file.
  4. Restart the SSH server: Copy.

What can be done over SSH? ›

SSH is often used to "login" and perform operations on remote computers but it may also be used for transferring data.

How do I access Linux server GUI remotely? ›

Connect to Linux Remotely Using SSH in PuTTY
  1. Select Session > Host Name.
  2. Input the Linux computer's network name, or enter the IP address you noted earlier.
  3. Select SSH, then Open.
  4. When prompted to accept the certificate for the connection, do so.
  5. Enter the username and password to sign in to your Linux device.

Can putty connect to GUI? ›

Start putty do the following settings in putty. Select X11 and click on Enable X11 forwarding. Then go into the session tab and take the remote of your machine using the IP address, Check the below steps. Now you will be able to use the GUI tool using Putty.

Can you SSH into localhost? ›

Open the terminal on the server machine. You can either search for “terminal” or press CTRL + ALT + T on your keyboard. Type in ssh localhost and hit enter.

How to connect to VM using SSH in Windows? ›

To enable SSH connections to Windows VMs, install the google-compute-engine-ssh package and set the enable-windows-ssh key to TRUE in project or instance metadata. Enabling SSH for Windows in project metadata enables SSH for all Windows VMs in your project.

How do I connect to a virtual machine using IP address? ›

Connect to VM

On the Bastion Connect page, for IP address, enter the private IP address of the target VM. Adjust your connection settings to the desired Protocol and Port. Enter your credentials in Username and Password. Select Connect to connect to your virtual machine.

What does SSH into VM mean? ›

Secure Shell access, in short – SSH, is used to safely access VM and to prevent unauthorized access. It is the client's key to the VM. The method of confirming the authorized access to the VM is set up during the VM creation stage.

How do I SSH into my EC2 instance without public IP? ›

If the instance does not have a public IP address, you can connect to the instance over a private network using an SSH client or the EC2 Instance Connect CLI. For example, you can connect from within the same VPC or through a VPN connection, transit gateway, or Amazon Direct Connect.

Can you SSH into a Web server? ›

Using SSH, you can interact with your site's files and server using commands, giving you full access to your server's configuration.

How do I SSH into AWS EC2 PuTTY? ›

Connect to EC2
  1. Make sure: ...
  2. In the Category pane on the left of the PuTTY Configuration window, under Connection, click on the + next to SSH to expand the choices (4), then click on Auth (5).
  3. Under Authentication parameters, click Browse and navigate to the directory where your PuTTY Private Key (. ...
  4. Click Open.

Does SSH require private key? ›

An SSH key relies upon the use of two related keys, a public key and a private key, that together create a key pair that is used as the secure access credential. The private key is secret, known only to the user, and should be encrypted and stored safely.

Does SSH client need public key? ›

To authenticate using SSH keys, a user must have an SSH key pair on their local computer. On the remote server, the public key must be copied to a file within the user's home directory at ~/. ssh/authorized_keys . This file contains a list of public keys, one-per-line, that are authorized to log into this account.

How do I SSH into a remote server without typing the password? ›

Configuring an SSH login without password
  1. Start by generating a key pair. A key pair includes a . ...
  2. Navigate to the directory in which you created the keys and verify that the process succeeded. ...
  3. Copy the public key to the destination system. ...
  4. You should now be able to login into the remote machine without a password.
Oct 17, 2019

How do I access remote code in VS Code? ›

Open VS Code on your remote machine. In the VS Code Account menu, select the option to Turn on Remote Tunnel Access…, as demonstrated in the image below. You may also open the Command Palette (F1) and run the command Remote Tunnels: Turn on Remote Tunnel Access.... In a client of your choice, you can open the vscode.

How to connect Visual Studio Code to localhost? ›

So let's learn how to run HTML on localhost in your browser.
  1. Step 1 - Download And Install Visual Studio Code. Get your VSCode editor ready. ...
  2. Step 2 - Open VSCode And Install Live Server Extension. ...
  3. Step 3 - Configuring Live Server To Run In Chrome (Or Another Browser) ...
  4. Run HTML File On Localhost in VSCode.

How to connect SSH with URL? ›

Connect to your web space via an SSH client:
  1. Open your SSH client.
  2. Type ssh one-example.com@ssh.one-example.com. (replace one-example.com with your own domain)
  3. If this is the first time you are connecting, confirm the authenticity of the host by typing yes.
  4. Enter your password. ...
  5. Press Enter.

How do I specify which SSH key to use? ›

To specify which private key should be used for connections to a particular remote host, use a text editor to create a ~/.ssh/config that includes the Host and IdentityFile keywords. Once you save the file, SSH will use the specified private key for future connections to that host.

How to configure SSH key based authentication VS Code? ›

  1. Step 1: Install Remote-SSH Extension in VS Code. ...
  2. Step 2: Verify SSH Connection in PowerShell. ...
  3. Step 3: Enable the Remote Connection in VS Code. ...
  4. Step 4: Generate SSH Key Pairs. ...
  5. Step 5: Copy the Public Key to the Remote Server. ...
  6. 12 Python Decorators to Take Your Code to the Next Level.
Jun 13, 2020

How do I know if SSH key is installed? ›

Checking for existing SSH keys
  1. Open TerminalTerminalGit Bash.
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present. ...
  3. Check the directory listing to see if you already have a public SSH key. ...
  4. Either generate a new SSH key or upload an existing key.

Can we do shell scripting in VS Code? ›

Shell Script Language Basics

This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled. It provides syntax highlighting and bracket matching in shell script files. You can check it by typing @builtin shell in the extensions tab, as you see in the above image.

How do I use GitHub over SSH? ›

Connecting to GitHub with SSH
  1. About SSH.
  2. Checking for existing SSH keys.
  3. Generating a new SSH key and adding it to the ssh-agent.
  4. Adding a new SSH key to your GitHub account.
  5. Testing your SSH connection.
  6. Working with SSH key passphrases.

How to integrate bash with VS Code? ›

30 Answers
  1. Open Visual Studio Code and press and hold Ctrl + ` to open the terminal.
  2. Open the command palette using Ctrl + Shift + P .
  3. Type - Select Default Profile.
  4. Select Git Bash from the options.
  5. Click on the + icon in the terminal window.
  6. The new terminal now will be a Git Bash terminal.

How do I run a bash command in Visual Studio Code? ›

Run code ~/.bashrc in bash to open the file in VS Code. Add the following to your PowerShell profile. Run code $Profile in pwsh to open the file in VS Code.

Does VS Code support bash? ›

VS Code provides excellent extensions for Bash scripting.

How do I access files over SSH? ›

Access remote file via SSH
  1. Log in to the local host as root.
  2. Generate Apache user authorized_keys file. ...
  3. Copy the generated authorized_keys file to the remote host. ...
  4. Log in to the remote host and move the authorized_keys file to the Apache user root directory (/var/www/.ssh)

How to connect with SSH? ›

To initiate an SSH connection to a remote system, you need the Internet Protocol (IP) address or hostname of the remote server and a valid username. You can connect using a password or a private and public key pair. Because passwords and usernames can be brute-forced, it's recommended to use SSH keys.

How to connect vscode to GitHub SSH? ›

Set up Visual Studio Code Remote SSH with GitHub in four straightforward steps
  1. Step1: Install and configure git. ...
  2. Step2: Generate SSH key pair. ...
  3. Step3: Setup SSH access between client and GitHub. ...
  4. Step4: Create new GitHub repository (Optional) ...
  5. Step5: Clone GitHub repo to Linux Client. ...
  6. Step6: Install Visual Studio Code.

How to connect to server using SSH public key? ›

Upload Your Public Key
  1. To use ssh-copy-id , pass your username and the IP address of the server you would like to access: ssh-copy-id your_username@192.0.2.0.
  2. You'll see output like the following, and a prompt to enter your user's password: ...
  3. Verify that you can log in to the server with your key.
Feb 1, 2023

How does Git over SSH work? ›

Git uses SSH to establish a secure connection through which it can execute commands. You're passing it in your ssh username, git , and the host to connect to, github.com . So far this is normal SSH. You also pass it the path to look for your Git repository, MY_GIT_USERNAME/PROJECT.

Is it better to use SSH or HTTPS GitHub? ›

Use SSH as a more secure option and HTTPS for basic, password-based Git usage. Since SSH is more secure than entering credentials over HTTPS, it is recommended for businesses dealing with sensitive and critical data. Once you generate the SSH keys, only the machines with the key file on disk can access the repository.

Can you send commands through SSH? ›

Running commands on a remote server using the SSH connection

SSH protocol supports both running interactive sessions and regular commands and scripts on a remote server.

Videos

1. Visual Studio Code Remote - SSH
(Visual Studio Code)
2. Remote SSH Configuration in VScode
(More than Certified)
3. VS Code Remote Development using SSH
(Nathan)
4. Using VSCode to SSH into an AWS EC2 instance
(ShaffiCS)
5. How to setup Visual Studio Code to Remotely SSH to an AWS Instance
(Trail Six)
6. How to connect to a remote Docker via SSH with VSCode
(Eyal Betzalel)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated: 05/04/2023

Views: 5624

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.