0

So, my local address keeps on changing on my Lan. Can't change the dhcp settings. Addresses are all like: 192.168.A.x, 192.168.A.y, 192.168.A.z. When I switch off the dhcp server, next time it gives 192.168.B.x, 192.168.B.y, 192.168.B.z. It keeps x, y and z constant.

So, i have tried to edit ~/. ssh/config like this

Host pc1
           HostName pc.local
           User xx
           Port 22

Host pc2
           HostName "192.168.???.n1"
           User xx
           Port 22

Host pc3
           HostName "192.168.*.n2"
           User xx
           Port 22

Calling ssh pc1 works fine. Also using ssh 192.168.A.x with the full adress works. But, calling ssh pc2 gives the error could not resolve hostname 192.168.???.n1

nyxee
  • 113
  • 5

2 Answers2

3

No, you can't. Especially not in this case. How is the ssh client supposed to "guess" what IP you want to connect to?

Set up a proper DHCP server or configure static IPs on your servers.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • Got some instructions from here: https://serverfault.com/questions/803902/how-can-i-match-a-cidr-range-for-an-ssh-config-host-entry/803906 – nyxee Jan 12 '22 at 15:19
  • 3
    wildcard will work on `Host` line, e.g. `host 192.168.*.125user xx` would allow selecting user `xx` when using `ssh 192.168.33.125` – Archemar Jan 12 '22 at 15:52
  • @Archemar, how would I then access that via ssh command?? Do I use `Host 192.168.*.n1 pc2` then `ssh pc2`?? – nyxee Jan 13 '22 at 05:25
  • by using `ssh 192.168.33.125` ssh can't do "retro aliasing" that DNS's work. – Archemar Jan 13 '22 at 11:15
1

it's not possible to use regex for hostname in ssh_config, but you can use the local domain name as you had done with PC1 thus

Host pc1
           HostName pc.local
           User xx
           Port 22

Host pc2
           HostName pc2.local
           User xx
           Port 22

Host pc3
           HostName pc3.local
           User xx
           Port 22

using the hostname you would never need to know the IP. Then you can ssh to the hostname

ssh pc.local
byaruhaf
  • 126
  • 2