How can I use multiple SSH keys for the same host?

后端 未结 1 1504
别跟我提以往
别跟我提以往 2021-01-01 06:11

Pretty much I want to be able to use multiple SSH keys on the same server for different users. I have a server that I use for both webhosting and as an SSH tunnel. I have se

相关标签:
1条回答
  • 2021-01-01 06:41

    If you have one key set up for your root user, the other one for your tunnel user (via file authorized_keys on the server/remote machine), the right key shall be picked automatically.

    This is based on the assumption that you loaded the keys in ssh-agent and they are available to the ssh utility.

    Otherwise, you can manually specify the key with ssh -i <identity file>.

    Besides that, you can set up aliases in your ssh_config file (~/.ssh/config or /etc/ssh/ssh_config):

    Host server-root
    User root
    IdentityFile <path to your key>
    Hostname <real hostname>
    
    Host server-tunnel
    User tunnel-user
    IdentityFile <path to your key>
    Hostname <real hostname>
    

    Then you use either ssh server-root or ssh server-tunnel.

    But I would say working with ssh-agent might be the easiest setup.

    If you want auto-selection of the right key without ssh-agent, you could specify both keys via -i.

    To quote from the OpenSSH man page:

     -i identity_file
         Selects a file from which the identity (private key) for public
         key authentication is read.  The default is ~/.ssh/identity for
         protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and
         ~/.ssh/id_rsa for protocol version 2.  Identity files may also be
         specified on a per-host basis in the configuration file.  It is
         possible to have multiple -i options (and multiple identities
         specified in configuration files).  ssh will also try to load
         certificate information from the filename obtained by appending
         -cert.pub to identity filenames.
    
    0 讨论(0)
提交回复
热议问题