Say I want to configure my ssh options for 30 servers with the same setup in my .ssh config file:
host XXX
HostName XXX.YYY.com
User my_username
Compression yes
Ciphers arcfour,blowfish-cbc
Protocol 2
ControlMaster auto
ControlPath ~/.ssh/%<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a496">[email protected]</a>%h:%p
IdentityFile ~/.ssh/YYY/id_rsa
where the only thing that changes between these 30 machines is XXX.
Instead than repeating the above structure 30 times in my config file, is there another way to define a range of machines?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
From the ssh_config(5) man page:
Host Restricts the following declarations (up to the next Host key‐ word) to be only for those hosts that match one of the patterns given after the keyword. If more than one pattern is provided, they should be separated by whitespace.…
HostName Specifies the real host name to log into. This can be used to specify nicknames or abbreviations for hosts. If the hostname contains the character sequence ‘%h’, then this will be replaced with the host name specified on the commandline (this is useful for manipulating unqualified names).
So:
Host XXX1 XXX2 XXX3 HostName %h.YYY.com
Method 2
To minimize the setup you can have a .ssh/config like this one
Host X01
HostName X01.YYY.com
Host X02
HostName X02.YYY.com
...
Host X01 X02 ...
User my_username
Compression yes
Ciphers arcfour,blowfish-cbc
Protocol 2
ControlMaster auto
ControlPath ~/.ssh/%<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671527">[email protected]</a>%h:%p
IdentityFile ~/.ssh/YYY/id_rsa
Host X01 X02 ... could be replace by Host * if every host have the following configuration
Method 3
Simply use *
See man ssh_config:
PATTERNS
A pattern consists of zero or more non-whitespace characters, ‘*’ (a wildcard that matches zero or more characters), or ‘?’
(a wildcard that matches exactly one character). For example, to specify a set of declarations for any host in the “.co.uk”
set of domains, the following pattern could be used:
Host *.co.uk
The following pattern would match any host in the 192.168.0.[0-9] network range:
Host 192.168.0.?
A pattern-list is a comma-separated list of patterns. Patterns within pattern-lists may be negated by preceding them with an
exclamation mark (‘!’). For example, to allow a key to be used from anywhere within an organisation except from the “dialup”
pool, the following entry (in authorized_keys) could be used:
from="!*.dialup.example.com,*.example.com"
Method 4
From Ignacio Vazquez-Abrams and H.-Dirk Schmitt’s answers, one can add the following to .ssh/config
HOST XXX*
HostName %h.YYY.com
User myname
and then, for example, you can login as [email protected] by
ssh XXX2
Method 5
this works for me:
CanonicalizeHostname yes CanonicalDomains xxx.auckland.ac.nz yyy.auckland.ac.nz host *.xxx.auckland.ac.nz user myuser host *.yyy.auckland.ac.nz user myuser
this allows one to use names within the domain and have the username changed:
bluebottle:~ user_one$ ssh itslogprd05 [email protected]'s password:
Method 6
The following way works.
Host 10.10.* 10.11.*
User vagrant
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0