Hacker News new | ask | show | jobs
by malikNF 680 days ago
>No need to remember server IPs

On your local machine under ~/.ssh/config you can add something like

#PERSONAL

Host vpn-us

    HostName 1.2.3.4

    User my_fun_username

    Port 1212

now you can ssh using

ssh vpn-us

(above is the same as the following command --> ssh my_fun_username@1.2.3.4 -p1212)

2 comments

Also you can organize servers into different config files and Include them your base config,.

  $ cat .ssh/config
  # Fictitious example
  Include work.config
  Include personal.config
  $ 
https://man7.org/linux/man-pages/man5/ssh_config.5.html
This looks very useful. I did not know this. Thank you!
Hope it helps. I use the configs as a source of truth and have a dozen or so different included files. This way the servers are grouped logically similar to ansible enventories (which I generate using the different configs). Running a command against all servers in one or more configs is simple this way:

  $ grep '^Host' .ssh/<config file> | awk '{print $2}' | while read hst; do ssh $hst '<remote command>' 2> /dev/null < /dev/null; done
One thing I wish ~/.ssh/config had was more slightly powerful matching. I think all you get for dynamic matches is * and ? instead of a regex syntax. Works probably 99% of the time.