Connect to a Remote Server via SSH directly from Visual Studio Code

How to connect to remote server via SSH directly from the Visual Studio Code and write code in the remote server or view log file using VSCode plugin Remote - SSH on windows.

Introduction

Now most of the developers using Visual Studio Code for development. And it provides various plugin to make developers code comfortably. I am writing this blog about one of my favorite plugin Remote - SSH which I am using everytime to update code in remote server and check logs by running grep command in large log files and export the required contents in separate file and view easily. All these things can be done directly from the Visual Studio Code instead of connecting through a Putty terminal.

Install Remote - SSH from the Microsoft marketplace. After successful installation, You can find the "Remote Explorer" icon in the left navigation.

After install

Install an (OpenSSH compatible SSH client)[https://code.visualstudio.com/docs/remote/troubleshooting#_installing-a-supported-ssh-client].

After installing the plugin, we need to create a config file with the list of remote server details which you wish to connect.Create a file in the below mentioned path with name sshConfig (without any extension)

Note : If you didn't have .ssh folder create new folder and enter name as .ssh. and save it

C:\Users\<your-username>\.ssh\sshConfig

Provide the remote server details inside the sshConfig file in the below mentioned format

Host dev
    HostName XXX.XX.XX.XXX
    User ubuntu
    IdentityFile "C:/Users/boopathikumar/.ssh/ubuntu.pem"

Host prod
    HostName XXX.XX.XX.XXX
    User ubuntu
    IdentityFile "C:/Users/boopathikumar/.ssh/ubuntu.pem"

Meaning of the terms used in the above format is explained below

  • Host - Provide any name for your remote server for further reference.
  • Hostname - Remote server Ip address
  • User - Username for login into the server
  • IdentityFile - Path of you pem key

Then select the settings icon near SSH TARGETS and add the sshConfig file path which we had created previously.

Add configuration file

After that you can see the list of remote servers configured in the sshConfig file inside the Visual Studio Code as shown below

List servers

To connect to any particular server, Click on the folder icon next to the remote server name as highlighted below.

Connect servers

Then, Select the Operating system of the remote server.

Connect servers select OS

Once the server is connected, You can see the name of the host which you provided on sshConfig file on the bottom left corner.

Click on Open folder button to view the folders in the remote server.Select the folder that you need to view as shown below

Connect servers select OS

After that you can view files under selected remote folder on the explorer as shown below.

Connected

Also you can use the integrated terminal to run the commands on connected remote server directly.

Setup "Remote - SSH" and enjoy programming in a remote server directly from Visual Studio Code !!!.