Introduction to SSH Keys with Multiple Accounts
I’ve seen this go wrong when people try to manage multiple services or projects on a single remote server - they end up with a mess of passwords and login issues. Using SSH keys with multiple accounts is a much better approach. It allows for secure, passwordless login to different user accounts on the same server, which is particularly useful for sysadmins, developers, and self-hosters.
Generating and Managing SSH Keys
To get started, you’ll need to generate a pair of SSH keys on your local machine. Don’t bother with the default RSA keys - I usually start with ED25519 keys, which are more secure and efficient. You can use the ssh-keygen command to create a new key pair:
ssh-keygen -t ed25519 -f ~/.ssh/my_key
This command generates a new ED25519 key pair, with the private key saved in ~/.ssh/my_key and the public key in ~/.ssh/my_key.pub.
Copying Public Keys to the Remote Server
Once you have your key pair, you’ll need to copy the public key to the remote server for each user account you want to access. The ssh-copy-id command makes this process a lot easier:
ssh-copy-id -i ~/.ssh/my_key.pub user1@remote_server
ssh-copy-id -i ~/.ssh/my_key.pub user2@remote_server
Just replace user1 and user2 with the actual usernames on the remote server. This is where people usually get burned - they forget to update the public key for each user account.
Configuring SSH to Use Multiple Keys
To use multiple SSH keys with different user accounts on the same server, you’ll need to configure your SSH client to use a specific key for each user. In practice, this means adding an IdentityFile directive to your ~/.ssh/config file:
Host remote_server
User user1
IdentityFile ~/.ssh/my_key
Host remote_server_user2
HostName remote_server
User user2
IdentityFile ~/.ssh/my_key
This configuration tells SSH to use the my_key key for both user1 and user2 on the remote_server. The real trick is to make sure you’re using the correct key for each user account.
Security Considerations
When using SSH keys with multiple accounts, security is paramount. I’ve seen this go wrong when people neglect to set a strong passphrase for their private key. Make sure to set a strong passphrase and store it securely. You can also use a tool like ssh-agent to manage your SSH keys and passphrases.
For more information on SSH key management, you can refer to the OpenSSH documentation.
Troubleshooting
If you encounter issues with SSH key authentication, enable verbose mode by adding the -v flag to your SSH command:
ssh -v user1@remote_server
This will help you diagnose any problems with your SSH key configuration.
See also
- Taming Duplicate Entries in Your Bash History with a Simple Script
- Taming systemd Restart Policies to Prevent Service Deluge
- Using rsync and SSH to Automate Offsite Backups of Important Configuration Files
- Recovering from a Failed Boot: Using systemd's Emergency Mode and Rescue Shell to Troubleshoot Initramfs Issues
- Taming Log Noise with journalctl: Filtering Out the Unimportant Stuff