Using SSH Keys with Multiple Identities and Agents for Simplified Remote Access

Introduction to SSH Keys and Agents

I’ve seen many Linux users struggle with managing multiple remote systems, but SSH keys and agents can make a huge difference. By using SSH keys, you can authenticate to remote servers without entering a password, making it easier to manage multiple systems. In practice, this means you can quickly switch between systems without having to remember a bunch of passwords.

Generating SSH Keys

To start using SSH keys, you need to generate a key pair on your local system. I usually start with the ssh-keygen command to generate a key pair:

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519

This command generates a 256-bit ED25519 key pair, which is considered secure and is the default key type for OpenSSH. The real trick is to make sure you use a strong passphrase for your private key.

Configuring SSH Agents

An SSH agent is a program that runs in the background and manages your SSH keys. When you add a key to the agent, it will automatically use that key for authentication. To start the SSH agent, you can use the following command:

eval $(ssh-agent -s)

This command starts the SSH agent and sets the necessary environment variables. Don’t bother with trying to manage the agent manually - just let the command do its thing.

Adding Keys to the Agent

To add a key to the agent, you can use the ssh-add command:

ssh-add ~/.ssh/id_ed25519

This command adds the private key to the agent, which will then use it for authentication. I’ve seen this go wrong when the key is not in the correct location, so make sure to check the path.

Using Multiple Identities

If you need to access multiple remote systems with different SSH keys, you can use the IdentityFile directive in your SSH configuration file (~/.ssh/config). For example:

Host server1
  IdentityFile ~/.ssh/id_ed25519_server1

Host server2
  IdentityFile ~/.ssh/id_ed25519_server2

This configuration tells SSH to use the id_ed25519_server1 key when connecting to server1 and the id_ed25519_server2 key when connecting to server2. This is where people usually get burned - forgetting to update the config file.

Managing Multiple Agents

If you need to use multiple SSH agents, you can use the SSH_AUTH_SOCK environment variable to specify the socket for the agent. For example:

SSH_AUTH_SOCK=/tmp/ssh-agent-1.sock ssh-add ~/.ssh/id_ed25519_server1

This command adds the id_ed25519_server1 key to the agent running on the /tmp/ssh-agent-1.sock socket. In practice, this is rarely needed, but it’s good to know how to do it.

Security Considerations

When using SSH keys and agents, it’s essential to keep your private keys secure. Make sure to set a strong passphrase for your private key and store it in a secure location. You should also regularly review your SSH configuration and agent activity to ensure that no unauthorized keys have been added.

Troubleshooting

If you encounter issues with SSH keys or agents, you can use the ssh command with the -v option to enable verbose mode:

ssh -v user@server

This command will display detailed information about the SSH connection, including the key exchange and authentication process.

Best Practices

To get the most out of SSH keys and agents, follow these best practices:

  • Use strong passphrases for your private keys
  • Store your private keys in a secure location
  • Regularly review your SSH configuration and agent activity
  • Use multiple identities and agents to manage access to different remote systems
  • Keep your SSH client and server software up to date

For more information on SSH and its configuration, you can refer to the OpenSSH documentation or the SSH protocol specification on the IETF website.


See also