Main Tutorials

Multiple SSH private keys Examples

To allow multiple private keys connect to different servers, edit ~/.ssh/config :

~/.ssh/config

Host 10.10.10.1 
IdentityFile ~/.ssh/linode_rsa

Host 200.20.20.2
IdentityFile ~/.ssh/id_rsa

If you SSH to 10.10.10.1, private key ~/.ssh/linode_rsa will be used.
If you SSH to 200.20.20.2, private key ~/.ssh/id_rsa will be used.

1. Single Private Key for Multiple Servers

1.1 You have following public and private keys.


$ cat ~/.ssh/

linode_rsa.pub
linode_rsa

1.2 Public key linode_rsa.pub is copied to following two servers :

  1. linode-us (10.10.10.1)
  2. linode-uk (10.10.10.2)

To SSH above servers with a single private key, edit .ssh/config :

~/.ssh/config

Host 10.10.10.1 10.10.10.2
IdentityFile ~/.ssh/linode_rsa

Or

/etc/hosts

127.0.0.1   localhost
10.10.10.1  linode-us
10.10.10.2  linode-uk
~/.ssh/config

Host linode-us linode-uk
IdentityFile ~/.ssh/linode_rsa

2. Multiple Private Keys for Multiple Servers

Create another set of public and private keys, and copy the public key new_rsa.pub to another server, for example 200.20.20.1.


$ cat ~/.ssh/

linode_rsa.pub
linode_rsa

new_rsa.pub
new_rsa

To SSH 200.20.20.1 with the new private key, update ~/.ssh/config :

~/.ssh/config

Host linode-us linode-uk
IdentityFile ~/.ssh/linode_rsa

Host 200.20.20.1
IdentityFile ~/.ssh/new_rsa

3. Output

Try SSH to the servers.


# Connect with private key - linode_rsa
$ ssh username@linode-us

# Connect with private key - linode_rsa
$ ssh username@linode-uk

# Connect with private key - new_rsa
$ ssh [email protected]

References

  1. OpenSSH Config File Examples

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments