sshssh-keysopenssh

How to Configure Multiple SSH Private Keys for Different Servers Efficiently?


I am working with multiple SSH private keys to connect to different servers or services (e.g., system administration, Git usage) on the same machine. Currently, I am using the -i option like so:

Apparently a straightforward way to do this is to use the command

ssh -i <path_to_private_key> user@server.example.com

However, managing connections this way feels cumbersome, especially when switching between different keys for different servers.

Is there a more efficient way to manage and use multiple SSH private keys without specifying the -i flag each time? I am looking for a configuration or tool-based approach that allows me to streamline this process.

What I've tried: Stacking the keys in the id_rsa file, which didn't work. Manually specifying the private key file with the -i flag, but this approach isn't very scalable.

Desired outcome: I'd like to set up my SSH configuration so that the correct private key is automatically used for the corresponding server or service.


Solution

  • From my .ssh/config:

    Host myshortname realname.example.com
        HostName realname.example.com
        IdentityFile ~/.ssh/realname_rsa # private key for realname
        User remoteusername
    
    Host myother realname2.example.org
        HostName realname2.example.org
        IdentityFile ~/.ssh/realname2_rsa  # different private key for realname2
        User remoteusername
    

    Then you can use the following to connect:

    ssh myshortname

    ssh myother

    And so on.