VSCode is a powerful source code editor with numerous advanced features and a rich extensions ecosystem. Lately, remote development have been supported, allowing you to work seamlessly in remote machines via SSH, inside containers or the Windows Subsystem for Linux.
Having successfully set up remote development extension in VSCode, you may want to know how you would save the SSH credentials into the system so that VSCode doesn’t ask for it everytime it tries to connect. In this article, we will show you how to set up SSH password in VSCode as well as fix a few common troubles upon installation.
SSH password in VSCode
Let’s clarify this : VSCode doesn’t store SSH credentials by itself. Instead, it relies on system configuration to figure out whether it would ask for SSH passwords or use the predefined SSH keys.
ssh-agent
is the program in charge of managing user’s identity keys and their passphrases (a.k.a. passwords). Those keys can be used to log into other machines without having the user type in the password manually in every connection.
ssh-agent
is automatically installed and run at login on most Linux systems and macOS-based machines. On Windows-based systems, you would have to either install OpenSSH from Windows 10 optional features or manually install Git for Windows.
Notice for PuTTY users : > PuTTY for Windows is not a supported client, but you can convert your existing PuTTYGen keys to a compatible one.
Set up local SSH key pair
In order to use SSH keys to connect, you have to create a key pair which includes a local "private" key and a "public" key. The public key will then be copied to the remote server.
If you do not have a key, run the following command in a terminal or PowerShell (Windows) to generate an SSH key pair. The command is supposed to be ran on your local machine.
ssh-keygen -t rsa -b 4096
Generated keys are 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
).
Copy public key to remote server
on Linux/macOS systems
If you’re running Linux or macOS in your local machine, run the following commands to copy the generated public key to the remote machine.
- If you’re connecting to a Linux-based or macOS-based remote server :
ssh-copy-id -i "$HOME/.ssh/id_ed25519.pub" [email protected]
Code language: JavaScript (javascript)
- If you’re connecting to a Windows-based remote server :
export USER_AT_HOST="[email protected]"
export PUBKEYPATH="$HOME/.ssh/id_ed25519.pub"
ssh $USER_AT_HOST "powershell New-Item -Force -ItemType Directory -Path \"\$HOME\\.ssh\"; Add-Content -Force -Path \"\$HOME\\.ssh\\authorized_keys\" -Value '$(tr -d '\n\r' < "$PUBKEYPATH")'"
Code language: JavaScript (javascript)
Remember to replace username
, remote-machine-IP
with the appropriate value in your scenario.
on Windows systems
If you’re running Windows in your local machine, run one of the following commands depending on your set up.
- If you’re trying to connect to a Linux-based or macOS-based remote machine.
$USER_AT_HOST="[email protected]"
$PUBKEYPATH="$HOME\.ssh\id_ed25519.pub"
$pubKey=(Get-Content "$PUBKEYPATH" | Out-String); ssh "$USER_AT_HOST" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '${pubKey}' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Code language: PHP (php)
- If you’re trying to connect to a Windows-based remote machine.
$USER_AT_HOST="[email protected]"
$PUBKEYPATH="$HOME\.ssh\id_ed25519.pub"
Get-Content "$PUBKEYPATH" | Out-String | ssh $USER_AT_HOST "powershell <code>"New-Item -Force -ItemType Directory -Path </code>"<code>$HOME\.ssh</code>"; Add-Content -Force -Path <code>"</code>$HOME\.ssh\authorized_keys<code>" </code>""
Code language: PHP (php)
Remember to replace username
, remote-machine-IP
with the appropriate value in your scenario.
Troubleshooting
VSCode keep asking for passwords after setting up SSH keys
In this case, there are chances that VSCode cannot recognize your SSH key settings. What we suggest to do is carefully inspecting the .ssh/config
file and look for anything suspicious.
The SSH configuration file is located at ~/.ssh/config
on macOS / Linux, and C:\Users\your-user.ssh\config
on Windows. A typical configuration should look like this
Host 1.1.1.1
Hostname 1.1.1.1
User username
IdentityFile ~/.ssh/id_ed25519.pub
Code language: JavaScript (javascript)
Common issues includes :
- Relative path to
IdentityFile
. VSCode sometimes needsIdentityFile
to be an absolute path so that it can find the key. A few macOS users have reported that adding “/Users/username
” in theIdentityFile
attribute in.ssh/config
(to make it absolute) solved the problem. - On Windows systems, the user can be mistakenly configured with the domain name included, as
DOMAIN\user
instead ofuser
. The problem can be easily solved by configuring the correct user in their.ssh/config
file. - The current user have insufficient permissions and cannot read the configuration file or the key. This problem mostly happens on Windows.
Connection failed after system restarts
If suddenly passwordless SSH authentication stops working after either the remote or the local machine restarts, we can safely assume that there’s a problem with SSH Agent. Usually, this is because SSH Agent haven’t been set up to run automatically at login.
In order to enable SSH Agent automatically on Windows, open up a local elevated PowerShell window and run the following commands:
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
Code language: JavaScript (javascript)
On Linux-based systems with Bash as default shell, you can add these lines to your ~/.bash_profile
to make SSH Agent starts automatically on login.
if [ -z "$SSH_AUTH_SOCK" ]; then
# Check for a currently running instance of the agent
RUNNING_AGENT="<code>ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'</code>"
if [ "$RUNNING_AGENT" = "0" ]; then
# Launch a new instance of the agent
ssh-agent -s &> .ssh/ssh-agent
fi
eval <code>cat .ssh/ssh-agent</code>
fi
Code language: PHP (php)
We hope that the article offer useful information on how to save SSH password in VSCode. You may also want to check out our guides on how to use RegEx in VSCode, Handle merge conflicts and Quickly create HTML Boilerplate in VSCode. All our other VSCode tutorials can be found at VSCode Tutorials page.
If you have any question, don’t hesitate to let us know in the comments section below.