1

Is it somehow possible to use a default ssh config without a hostname? of course I know how to use ~/.ssh/config, but I want to do something like:

Host myservers
  User root
  IdentitiesOnly yes
  IdentityFile ~/.ssh/specialkey
  • ssh myservers server1.de
  • ssh myservers server2.de

but that wont work, so I cannot use a ssh config and define the server/hostname afterwards. That means for now I always have to do ssh -i ~/.ssh/specialkey -O IdentitiesOnly=yes [email protected] or what other possibilities would I have here to use?

5
  • 2
    What's wrong with Host server1.de server2.de? Commented Sep 23, 2020 at 18:56
  • @KamilMaciorowski its about 100-200 servers ...
    – Jeno
    Commented Sep 24, 2020 at 8:40
  • Host server*.de? Commented Sep 24, 2020 at 8:42
  • I still dont understand how that should work, maybe you can post it more verbose as an answer?
    – Jeno
    Commented Sep 24, 2020 at 18:50
  • My answer now includes the idea. Commented Sep 24, 2020 at 19:28

1 Answer 1

0

Modify the snippet so it applies to the hosts in question:

Host server1.de server2.de
  User root
  IdentitiesOnly yes
  IdentityFile ~/.ssh/specialkey

Then you just ssh server1.de or ssh server2.de.

You can add more entries to the Host line. If the settings should apply to a group of servers with a common naming scheme, consider using wildcards. E.g. Host server*.de.

Make sure you put the snippet early enough in your config (because the first obtained value for each parameter is used). Definitely before Host *.


If you want to use some special config on demand, put the config in another file and use ssh -F.

-F configfile
Specifies an alternative per-user configuration file. If a configuration file is given on the command line, the system-wide configuration file (/etc/ssh/ssh_config) will be ignored. The default for the per-user configuration file is ~/.ssh/config. […]

(source)

Example:

ssh -F ~/.ssh/myservers server1.de

Notes:

  • The config may contain Host * as the only section, so it will work for any server you want.

  • You can create two or more configs, each in a separate file, and pick whichever you want when you connect.

  • An alias

    alias ssh-myservers='ssh -F ~/.ssh/myservers'
    

    will allow you to connect almost as you wanted: ssh-myservers server1.de.

  • A wrapper script or a shell function can allow you to connect exactly as you wanted. Example function:

    ssh() {
       configfile=~/".ssh/$1"
       if [ -f "$configfile" ]; then
          shift; command ssh -F "$configfile" "$@"
       else
          command ssh "$@"
       fi }
    

    To connect with ssh myservers server1.de you need the config to be in ~/.ssh/myservers.

You must log in to answer this question.

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