I'm a php developer who has started a new job. The company has numerous docker instances and the ability to spin multiple versions of this up, for example: to check a particular git branch etc. The system copies all relevant files as well as keys and access.

As these are spun up they end up with different ssh addresses. The usual command: ssh user@someaddress.domain works as expected.

However, I am operating multiple keys (so I can separately rotate and keep personal and private keys unrelated etc). All these spun up instances will use the same key

For this, I can use ssh -i [path to key] [user@domain]. Typing this every time is a hassle.

In bash, and with MySQL you can prompt for the username/password/whatever: mysql -u -p for example. I have aliased or Bash scripted this to good effect many times.

I would like to alias ssh_test to be: ssh -i [path to key] and have it request for the address to save on a (lot) of typing.


How can I use the ssh command in such a way that it prompts for the address

Thanks in advance.

  • 2
    You seem to be overcomplicating things - use ssh-agent and let that figure out which key is correct.
    – Panki
    May 7 '20 at 11:17

Create a ~/.ssh/config file for this:

# everything else matches this
Host *
  IdentityFile ~/.ssh/id_rsa

Host server1
  Hostname server1.fqdn
  IdentityFile ~/.ssh/server1

Host server2
  Hostname server2.fqdn
  IdentityFile ~/.ssh/server2

Host server3
  Hostname server3.fqdn
  IdentityFile ~/.ssh/server3

So that when you, for instance, ssh server2, ssh client automatically picks up and uses the ~/.ssh/server2 key. Because it matches Host server2 in the ssh config file above.

And if you do ssh anyotherserver, ssh client will automatically pick and use ~/.ssh/id_rsa. Because it matches Host * in the ssh config file above.

  • This would be useful, if the instance names didn't change constantly...
    – elb98rm
    May 7 '20 at 11:44
  • You can use regex in the ssh config file as well for the Host, in case there is a match for certain sets of servers. man ssh_config for more
    – GMaster
    May 7 '20 at 12:04

Assuming you’re using bash you need to specify the key file below.

echo “alias ssh_test=‘ssh -i <path/key>’ >> ~/.bashrc

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.