2

I connect to many servers daily. my ssh config default username is "admin"

but servers comes from different environnements and username can changes. sometimes it will be "ec2-user" or "exploitation" or "something-else" ...

Is there a way SSH remember that last connection to 192.0.2.1/server.example.org was with "ec2-user" ?

So I don't have to specify "-l ec2-user" next time.

exeral
  • 1,787
  • 11
  • 21

2 Answers2

4

You can set these up yourself in your configuration file $HOME/.ssh/config.

For example:

Host *.amazonaws.com
        User ec2-user

Host 192.0.2.1
        User centos
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • sadly, there is no rule/pattern that can group servers. I can have ec2-user or admin or centos inside amazon servers – exeral Nov 18 '20 at 22:15
  • @exeral You're going to have a lengthy config file then. – Michael Hampton Nov 19 '20 at 09:10
  • You can put defaults at the end of the file, and override them earlier in the file. So if you want to use the user `ec2-user` on *most* \*.amazonaws.com servers, but `admin` or `centos` on only a few, you can put an entry for `Host *.amazonaws.com`, `User ec2-user` at the end of the file and entries for the others earlier. Or override it on the command line for those anomalous hosts. – Gordon Davisson Nov 19 '20 at 16:55
0

self answer, I am finally using this script as a wrapper for "ssh"

#!/bin/bash
SSH_OPTIONS="-o PasswordAuthentication=no"
ssh $SSH_OPTIONS $*

if [ $? -eq 255 ]
then
  ssh -l admin $SSH_OPTIONS $*
fi

if [ $? -eq 255 ]
then
  ssh -l exploit $SSH_OPTIONS $*
fi

if [ $? -eq 255 ]
then
  ssh -l ec2-user $SSH_OPTIONS $*
fi
exeral
  • 1,787
  • 11
  • 21