I have a remote CentOS server, lets pretend it's called hostname.com

hostname.com has several user accounts, and I want to ssh into this server as any one of these users (using ssh keys rather than password).

It seems everyone recommends just changing my local ssh config, add an alias the server for each remote user, and then providing a different IdentityFile for each alias.

So then I have to ssh bob@aliased_hostname1 and ssh alice@aliased_hostname2

Are there any other solutions? Id rather not be ssh'ing into aliased hostnames

Ideally I want to just ssh bob@hostname.com or ssh alice@hostname.com and automatically choose the correct ssh keyfile, based on the remote username i'm trying to log in as.

  • 1
    How did you end up with several different ssh keys? Fix that problem. – Michael Hampton Aug 8 at 23:44
  • 2
    Is it a 'problem' though? Why is it a bad idea to want different ssh keys for different remote users? – carpii Aug 9 at 0:05
  • 1
    The facility that ssh-client provides is hostname aliases, and for some inexplicable reason that's exactly the thing you're not willing to do. I suppose you can make a shell alias too: alias ssh1='ssh bob@hostname.com -i /path/to/identity.key' – Wesley Aug 9 at 0:08
  • 3
    The ssh key is meant to identify you to the remote system, so you generally only need one. – Michael Hampton Aug 9 at 0:11
  • 1
    @carpii -- I get it; I was not thinking in terms of scale. Thanks for the explanation. – sitaram Aug 10 at 6:52

Check out the Match keyword in your SSH config on the client machine. Specifically, the user and host criteria. The ssh_config(5) man page states (emphasis mine):

Match conditions are specified using one or more criteria or the single token all which always matches.

So in your case you'd be looking at something like:

Match user bob host "hostname.com"
  IdentityFile /path/to/bob.key

Match user alice host "hostname.com"
  IdentityFile /path/to/alice.key

There's one SuperUser and one Unix & Linux Q/A that helped tie it all together, so you should probably go upvote those participants as well.

| improve this answer | |
  • very cool, thankyou! – carpii Aug 9 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.