Set default user and key for SSH connection

I work with several different deployment environments and multiple servers at the moment and I log at each one of them as different user than the one on my laptop. Every time I want to connect to the server I have to specify the user.

ssh [email protected]
# or
ssh myserver.com -l bestuser

To make it even more interesting, I have to use a different SSH key for some of the connections. I need to type something like

ssh myserver.com -l bestuser -i ~/.ssh/myserveridentity

Typing all those characters is not much fun, but don’t despair as there’s a simple solution. You can use ssh config to specify various options for your SSH settings and apply it to your server.

Moreover, you can use wildcards to match just parts of the server name, and that is super useful on private network where you can use *-test-* to match all your test servers.

To start configuring, edit or create ~/.ssh/config file and add

Host *myserver.com
  User bestuser
  IdentityFile ~/.ssh/myserveridentity

And from now on you can type only ssh myserver.com and it automatically uses this SSH key and user name for login.


Would you like to get the most interesting content about programming every Monday?
Sign up to Programming Digest and stay up to date!