3

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 [email protected] or ssh [email protected] and automatically choose the correct ssh keyfile, based on the remote username i'm trying to log in as.

8
  • 1
    How did you end up with several different ssh keys? Fix that problem. Aug 8, 2020 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, 2020 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 [email protected] -i /path/to/identity.key'
    – Wesley
    Aug 9, 2020 at 0:08
  • 3
    The ssh key is meant to identify you to the remote system, so you generally only need one. Aug 9, 2020 at 0:11
  • 1
    @carpii -- I get it; I was not thinking in terms of scale. Thanks for the explanation.
    – user512619
    Aug 10, 2020 at 6:52

1 Answer 1

6

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.

1
  • very cool, thankyou!
    – carpii
    Aug 9, 2020 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .