DEV Community

Cover image for Passwordless SSH access to server
Ruben Alvarado
Ruben Alvarado

Posted on Edited on

Passwordless SSH access to server

You can also find this article in my blog web-warrior-toolbox

When you implement server for your dev, staging, production, etc. environments it happens that you have to access each of those servers when you need to config something or check the execution logs. Instead of having to input a password each time you access a server, you can skip that step by following this procedre.

In a Nutshell

  1. Generate an SSH key pair on the developer PC if one does not already exist (e.g. ~/.ssh/id_ed25519.pub).

1) Copy the public key to the remote server to ~/.ssh/authorized_keys (each user on remote server has a file with this name). The helper command ssh-copy-id can do this automatically and only needs to be installed on the machine from which it is executed (typically the developer PC).
2) when you want to revoke the access you can delete the public key from ~/.ssh/authorized_keys

In detail

Generate a key pair if needed:

ssh-keygen -t ed25519
Enter fullscreen mode Exit fullscreen mode

Typical files:

~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Install the public key for a remote user:

ssh-copy-id user@server
Enter fullscreen mode Exit fullscreen mode

This adds the public key to:

/home/alice/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode
  • If there is no ssh-copy-id in dev-pc you can copy the contents of .pub file to ~/.ssh/authorized_key on remote server manually

Note that SSH keys grant access to a specific user account, not to the entire server.

For a server with multiple users:

/home/alice/.ssh/authorized_keys
/home/bob/.ssh/authorized_keys
/home/deploy/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

If your public key only exists in Alice's file:

ssh alice@my-server   # works
ssh bob@my-server     # fails
Enter fullscreen mode Exit fullscreen mode

To allow access as multiple users, add the public key to each user's:

~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

To revoke access, remove the corresponding line from:

~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

on the target account.

🤖Keep growing your apss and always be learning, dev-san 🥷

Top comments (0)