Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - SFTP Extension Using SSH Authentication

Tags:

I'm using the vscode-sftp extension in my Visual Studio Code workflow to upload files to my remote server, whenever I hit save. I am trying to achieve password-less authentication as I used to do with Sublime Text's SFTP plugin. However, the catch is that for vscode-sftp, in the sftp.json file, I have to enter my Password as plain text for this to work:

{
    "host": "mysamplehost.com",
    "port": 22,
    "username": "username",
    "password": "password",
    "protocol": "sftp",
    "agent": null,
    "privateKeyPath": null,
    "passphrase": null,
    "passive": false,
    "interactiveAuth": false,
    "remotePath": "/",
    "uploadOnSave": true,
    ...
}

However, if I SSH into my server, I do not get asked for a password - since I have the SSH key in my MacBook. How do I configure vscode-sftp to use this authentication method?

I see the following comment in the documentation, however I don't know how to set this up:

  /**
   * string - Path to ssh-agent's UNIX socket for ssh-agent-based user authentication.
   * Windows users: set to 'pageant' for authenticating with Pageant or (actual) path to a cygwin "UNIX socket.
   */
  agent: null, 
  privateKeyPath: null, // absolute path to user private key
  passphrase: null,
  passive: false, // ftp passive mode
like image 626
Amit Avatar asked Oct 20 '17 15:10

Amit


People also ask

How do I use VS Code SFTP extension?

You can do this from File->Open Folder and select your project directory. Press the Ctrl+Shift+P if you are on Windows/Linux or Cmd+Shift+P on Mac which opens a command palette. Type SFTP and select the SFTP:config option.

Can VS Code connect to SSH?

VS Code Remote SSHYou 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 does Visual Studio Code connect to SSH 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.

Where do I put 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.


2 Answers

I figured it out after stumbling upon this. Here is how I did it. First, run:

$ echo $SSH_AUTH_SOCK

Enter the path to agent in your sftp.json file. Also enter the path to your private key (id_rsa file). The following config did the trick for me:

"agent": "/private/tmp/com.apple.launchd.nPw17MhOqq/Listeners",
"privateKeyPath": "/Users/amitsn/.ssh/id_rsa",
"passphrase": null,

Note that I do not have a passphrase, so I left it null. Do not forget to fill this one if you have one.

like image 188
Amit Avatar answered Sep 19 '22 15:09

Amit


For me, it was enough to just use the privateKeyPath option:

...
    "host": "my.site",
    "protocol": "sftp",
    "port": 22,
    "username": "username",
    "privateKeyPath": "/Users/username/.ssh/id_rsa",
...

Note: in order to login with the key, you need to install your key to the remote server first with ssh-copy-id [email protected] command.

like image 38
pilat Avatar answered Sep 21 '22 15:09

pilat