DEV Community

Czar Pino
Czar Pino

Posted on

Manage Multiple SSH Logins

It can be quite cumbersome when you have a lot of remote servers to log into. If they support SSH access or if you can configure them to do so, managing access to multiple servers can be pleasantly manageable.

What most programmers don't seem to know is that SSH can use a configuration file in ~/.ssh/config to create an alias of sorts for SSH hosts. If you use aliases in Bash then you can think of this as an alias for creating an SSH connection to a server.

# Without config
ssh -i ~/.ssh/linode01.id_rsa czar.pino@120.111.122.123

# With config
ssh cp.linode01
Enter fullscreen mode Exit fullscreen mode

By using the ~/.ssh/config file, it no longer becomes necessary to provide all of the connection parameters every time you want to connect. All information needed to connect to a host can just be defined in a host entry inside the config file.

Host cp.linode01
    Hostname 120.111.122.123
    User czar.pino
    IdentityFile ~/.ssh/linode01.id_rsa
Enter fullscreen mode Exit fullscreen mode

Another side benefit to this is that you also automatically have a document of all SSH servers you can connect to. No need to maintain an easily outdated spreadsheet which has to be updated separately.

Host cp.linode01
    Hostname 120.111.122.123
    User czar.pino
    IdentityFile ~/.ssh/linode01.id_rsa

Host cp.linode02
    Hostname 120.111.122.124
    User czar.pino
    IdentityFile ~/.ssh/linode02.id_rsa

Host cp.ec2-01
    Hostname 121.111.122.123
    User ec2-user
    IdentityFile ~/.ssh/ec2-01.id_rsa

Host cp.ec2-02
    Hostname 121.111.122.124
    User ec2-user
    IdentityFile ~/.ssh/ec2-02.id_rsa
Enter fullscreen mode Exit fullscreen mode

Originally published at https://plog.czarpino.com/storing-database-backups/

Top comments (6)

Collapse
 
polyluxus profile image
Martin Schwarzer

Also nice to know for the configuration is that you can specify options that are common for some or most hosts, like X forwarding, agent forwarding, etc..

If you sometimes need to route through a different server, you can also specify these in the config file.

Many times yes to the config, good that you're showing its capabilities.

Collapse
 
vinayhegde1990 profile image
Vinay Hegde

For me, SSH config always sounded tricky but this post made it easy so thanks @Czar

I think it could be even better and standardized for one's team members by having it in version control, e.g: Git which will help have an audit trail, easier to maintain than having multiple files on one's PC & an indirect backup due to being in a VCS

Collapse
 
czarpino profile image
Czar Pino

Happy it helped you Vinay!

Collapse
 
joehobot profile image
Joe Hobot

I agree using config file is best, another one is to sign up with something like userify which works right off your rsa id.

Collapse
 
czarpino profile image
Czar Pino

That's a really interesting alternative Joe. Thanks for sharing!

Collapse
 
cmmata profile image
Carles Mata

You just saved me a lot of weekly time typing users, full hostnames and ports! Thank you!