Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh-config by host subnet

Tags:

ssh

So I have a whole bunch of machines on my 10.10.10.x subnet, all of them are essentially configured in the same way. I differentiate these from machines on my 10.10.11.x subnet which serves a different purpose.

I'd like to be able to type 'ssh 10.x' to connect to machines on the 10. network and 'ssh 11.x' to connect to machines on the 11 network.

I know I can setup individual machines to allow access to the full ip, or the shorthand version like this in my ~/.ssh/config:

Host 10.10.10.11 10.11
HostName 10.10.10.11
User root

This can get pretty repetitive for lots of hosts on my network, so my question is, is there a way to specify this as a pattern, for the entire subnet, something like:

Host 10.10.10.x
User root

Host 10.x
HostName 10.10.10.x
User root

Thanks

like image 401
jdeuce Avatar asked Jul 17 '12 21:07

jdeuce


People also ask

How do I find my SSH config?

The ssh program on a host receives its configuration from either the command line or from configuration files ~/. ssh/config and /etc/ssh/ssh_config .

What is HostName in SSH config?

In essence: Host is the string the user gives as input on the CLI when invoking SSH; HostName is the string that the SSH client will output over the network when attempting to connect to the server.


2 Answers

This line will provide the desired functionality:

Host 192.168.1.*
IdentityFile KeyFile

If you attempt to connect a server whose ip is in this subnet, you will be able to establish an ssh connection.

like image 147
Shnkc Avatar answered Oct 07 '22 08:10

Shnkc


From the ssh_config(5) Manpage:

 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 decla‐
 rations for any host in the “.co.uk” set of domains, the following pat‐
 tern 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"

So you can just use host 10.*

like image 15
Tron Avatar answered Oct 07 '22 06:10

Tron