Search code examples
sshssh-config

ssh-config by host subnet


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.x network and ssh 11.x to connect to machines on the 11.x network.

I know I can set up 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

Solution

  • 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.