Connect over SSH with Visual Studio Code (2024)

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)

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.

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.

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.

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

Connect over SSH with Visual Studio Code (2024)

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 using 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

Can I use SSH Git authentication with VS Code? ›

All you need are git, GitHub account, Visual Studio Code, and a remote development extension then set up a server and configure SSH.

How to run remote command via SSH? ›

SSH tip: Send commands remotely
  1. Run the command "ssh username@host" to log in to the system.
  2. At the command prompt, run "top" to view process activity on the remote system.
  3. Exit top and be dropped to the remote command line.
  4. Type "Exit" to close the command.
Oct 2, 2012

How do I connect a device using SSH? ›

Connect
  1. Open a Command prompt window on your technician PC.
  2. Connect to the device: To connect using a username and password: cmd Copy. ssh user@192. 168. ...
  3. Enter the password for your user if you're connecting with a username and password, or if you configured your key to require a password.
Jun 24, 2021

Can you run a script through SSH? ›

SSH scripts can be used in Secret Server to automate specific tasks. An SSH script can be configured as a dependency of a Secret and run after the password is successfully changed on the Secret.

Does Visual Studio have SSH? ›

In the latest version of Visual Studio, users are now able to leverage the integrated terminal to access their remote targets when developing for remote machines from Windows. This updated terminal includes an interactive SSH shell.

How do I SSH into ec2 VS Code? ›

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

How do you connect to a live server in VS Code? ›

Step 1: Click on the extensions option available on the left side in Visual Studio Code. Step 2: Search Live Server in the search box and install it. Step 3: The live server extension has been installed. Step 4: Click on Go live present at the bottom.

How do I connect Visual Studio code to local host? ›

The way to do this, is using VS Code's simple browser. CRTL+SHIFT P, type simple browser, and it will open a window, add your localhost info and Bam!

How do I access GIT from SSH? ›

Connect to GitHub using SSH
  1. Step 1: Generate SSH Key on Local System.
  2. Step 2: Add SSH Key to SSH Agent.
  3. Step 3: Add the SSH Key to your GitHub Account.
  4. Step 4: Test the SSH Connection.
Apr 21, 2020

How do I link Git and Visual Studio code? ›

The first step to being able to use Github with VSCode is to set-up Git on your computer and enable in it VSCode.
...
To enable Git in VS Code on Windows:
  1. Go to File > Preferences.
  2. Go to Settings.
  3. Type Git: Enabled in the search bar.
  4. Make sure that the box is ticked.
May 4, 2022

Where does VS Code look for SSH keys? ›

Create your local SSH key pair

Check to see if you already have an SSH key on your local machine. This is typically located at ~/. ssh/id_ed25519.

Why is my SSH not connecting? ›

Install an SSH tool such as OpenSSH on the server you want to connect to using the sudo apt install openssh-server command. If your firewall is blocking your SSH connection. Disable the firewall rules blocking your SSH connection by changing the destination port's settings to ACCEPT.

How to use SSH command in script? ›

How to use SSHPASS inside the Shell Script ( A Secure Approach )
  1. Gets UserName and Password from the User.
  2. Read the list of server names from a Serverlist.properties file.
  3. Create a Script on the Runtime named TestScript.sh using HereDocument.
  4. Copy the Created TestScript to the remote server using SCP.
Jun 19, 2022

Where do I run SSH commands? ›

You must download and use the PuTTy key generation tool to create keys for the Run SSH Command activity. The key generation tool is available at Download PuTTY - a free SSH and telnet client for Windows. The Run SSH Command activity supports SSH-1.

What is SSH connection command? ›

The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.

What does it mean to connect via SSH? ›

SSH or Secure Shell is a network communication protocol that enables two computers to communicate (c.f http or hypertext transfer protocol, which is the protocol used to transfer hypertext such as web pages) and share data.

How do I connect to an existing SSH session? ›

Resume command running in dropped SSH session.
...
You can:
  1. F2 start a new session.
  2. F3 move to the next session tab on the left.
  3. F4 move to the next session tab on the right.
  4. F8 give a friendly name to the current session tab.
  5. F9 opens a options menu.
  6. CTRL+A+D detaches all sessions from the terminal.

Can I use SSH instead of PuTTY? ›

So, can SSH on Windows PowerShell compete with PuTTY? The possibilities are good! However, it's still not PuTTY. While you can bind an address with OpenSSH on Windows, you're limited by the number of addresses you can save.

How do I run a script remotely? ›

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.

What is a disadvantage to using SSH? ›

The first con of using SSH key authentication is that the private key needs to be stored on the device with which you're logging in. These devices, such as laptops and mobile phones, can be lost or stolen. And if they're not protected properly, hackers can gain access to the private key and eventually the server.

How do I use Visual Studio code in terminal? ›

To open the terminal:
  1. Use the Ctrl+` keyboard shortcut to toggle the terminal panel.
  2. Use the Ctrl+Shift+` keyboard shortcut to create a new terminal.
  3. Use the View > Terminal or Terminal > New Terminal menu commands.
  4. From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command.

How do I enable SSH? ›

SSH Configuration
  1. Open the SSH configuration file /etc/ssh/sshd_config.
  2. Disable non-admin SSH tunneling. ...
  3. Disable agent forwarding (which is enabled by default). ...
  4. Update authentication methods.
Oct 28, 2021

Can I use SSH in terminal? ›

You can start an SSH session in your command prompt by executing ssh user@machine and you will be prompted to enter your password. You can create a Windows Terminal profile that does this on startup by adding the commandline setting to a profile in your settings.

How do I connect to AWS from VS code? ›

Connect to AWS through the Toolkit for VS Code
  1. Open VS Code.
  2. To open the Command Palette, on the menu bar, choose View, Command Palette. ...
  3. Search for AWS and choose AWS Toolkit Connect to AWS.
  4. Choose a profile from the list. ...
  5. Open the AWS Toolkit Explorer Side Bar, which we call the AWS Explorer, to verify the connection.

Does VSCode have a live server? ›

The Live Server extension for Visual Studio code has been installed 25M times and is incredibly useful. It enables you to right-click an HTML document, and it runs a server for you and opens a browser window with the file in it.

How do you open a live server in VSCode Live share? ›

Open VS Code (if it isn't open already), or open a new VS Code window by selecting File > New Window. Open the Live Share pane by clicking the Live Share icon from the navigation bar. Select the option to join a collaboration session: either a button that says Join or a link that says Join collaboration session.

Why is my live server not working in Visual Studio code? ›

Restart VSCode

Sometimes the best you can do is start VSCode from scratch. First, save all of your work. Then close VSCode, which will also stop all of the extensions you've installed. Then, reopen VSCode and try again – go to the HTML file you want to view, right click, and select "Open with Live Server".

How to add SSH key in VS Code? ›

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.

How do I SSH to local host? ›

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 do I push local branch to remote in Visual Studio Code? ›

To push a local branch to the remote, right click on that branch in Team Explorer. From the context menu, that pops up on the screen, select Push Branch.

How do I connect to a SSH server in terminal? ›

How to connect via SSH:
  1. Open the list of your servers. Click the one you need and click the button "Instructions". ...
  2. Open a terminal (for Linux) or a command line (for Windows) on your computer. Enter the command: ssh [username]@[server IP] ...
  3. The connection will ask for a password.

What is SSH command in terminal? ›

The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.

How to access SSH on Linux? ›

Using SSH in Linux
  1. Open the terminal if you're not already at the command line. ...
  2. Use SSH to log in to the remote machine using your username, and the IP address / hostname. ...
  3. Type yes if you are given a warning that the SSH connection's key is not known. ...
  4. Enter your password if prompted.
Jul 4, 2022

How do you connect to a live server in VS code? ›

Step 1: Click on the extensions option available on the left side in Visual Studio Code. Step 2: Search Live Server in the search box and install it. Step 3: The live server extension has been installed. Step 4: Click on Go live present at the bottom.

How do I connect VS code to my virtual machine? ›

Connect to the Linux VM from VS Code

Inside VS Code, launch the command palette with ctrl + shift + p , enter remote-ssh , and select Remote-SSH: Connect to Host... . Select Linux . VS Code will now connect to the Linux VM and start the installation process.

How do I verify my SSH connection? ›

To Test the SSH Setup on a Host
  1. From another host, use SSH to log in into the host that you are testing as the SSH user. $ ssh -l user-name host-name user-name. The user name for the SSH user's account on the host. ...
  2. In response to the prompt, type your password. If this step succeeds, your setup of SSH is complete.

How do I know if SSH is running? ›

To check if SSH is enabled on your system, open a command prompt and end the command ssh . If it provides you with help for using SSH, it is already enabled! You should be able to follow the Linux instructions using the ssh-keygen command from the command prompt.

How do I fix SSH port 22 connection refused? ›

How to Fix the SSH “Connection Refused” Error
  1. Check Your Network Connection. While this may seem obvious, it doesn't hurt to check! ...
  2. Double-Check Your Credentials. Connection working fine? ...
  3. Reboot Your Server. ...
  4. Make Sure SSH Is Installed. ...
  5. Make Sure SSH Is Working Properly. ...
  6. Ports and Firewalls. ...
  7. Contact Your Host.

Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6468

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.