Quickly list all hosts in your ssh config

April 4, 2017

I try to be a good citizen and create unique ssh keys for each service I ssh into. I setup all sorts of fun config items and one of my favorites is host aliases. I have accumulated dozens of hosts in my config file and remembering the names of each can be problematic. I tend to open the config file a couple times a day to find a host. This morning I whipped up a quick command that will dump a list of all my hosts into the terminal with one command. No more opening the config file with an editor and scrolling through it. Hopefully this will be something you can enjoy also!

Update: A commenter provided a smoother version that gets rid of the grep call. Here it is.

sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config

But wait! That is a lot to type and this was supposed to make life easier. Add this as a shell alias and simply type sshhosts to get the list!

alias sshhosts="sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config"

11 thoughts on “Quickly list all hosts in your ssh config

  1. Anant (April 10, 2018)

    This will save me a fair amount of effort as well!

  2. a (June 24, 2018)

    Try harder;)
    sed -n ‘s/Host//p’ ~/.ssh/config

    1. Ben Lobaugh (blobaugh) (July 6, 2018)

      That does indeed work to pull out every line with a “Host” on it, however it is not well formatted and will include any other directive in the config file with “Host” on the line. My config file is complex enough that this will not work for me. Try harder 😉

  3. Zouppen (September 18, 2018)

    Host keyword in ssh_config is case-insensitive so you should do a case insensitive replacement, too. Also, one should clean-up the spaces and tabs. And of course play golf with a sed one-liner:

    sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config

    1. Ben Lobaugh (blobaugh) (September 18, 2018)

      Good feedback. My grep command is indeed case sensitive, however the sed is not. The removal of spaces and tabs in your example is handy.

      1. Zouppen (September 19, 2018)

        Yeah 🙂

        Still there’s a small issue: your blog platform mangles single quotes to Unicode LEFT and RIGHT SINGLE QUOTATION MARK and makes copy-pastes broken. That’s annoying feature for a tech blog. Hope you can fix it. 🙂

  4. wwek (July 29, 2019)

    thinks very match

  5. dirdi (November 19, 2019)

    Here is the command used by BASH completion:

    sed -ne ‘s/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]]\{1,\}\([^#*?%]*\)\(#.*\)\{0,1\}$/\1/p’ ~/,ssh/config

    It handles comments appended at the end as well.

  6. Yosu (July 24, 2020)

    I use unique SSH ports and unique keys for each server I manage, that’s well over 40 VPSs and growing, I couldn’t memorize more than a handful…

    I’ve been using your method for a couple of months, this is fantastic, I’m saving so much time, not just typing, but looking for the right IP, the right port, the right key, the right directory!

    Now I’m even starting to add some fancy configuration for some hosts.
    Best time saver ever, thank you all for the post and the additions from everyone else.

    1. Ben Lobaugh (blobaugh) (July 30, 2020)

      Fantastic! Glad it is working well for you.

  7. Yosu Cadilla (July 24, 2020)

    For some reason, in my Xubuntu 20.04 Desktop, your exact syntax didn’t work, I had to modify it to:
    alias sshlist=”sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config”
    (only change is ‘ instead of ‘ )

    If you want to make the alias permanent:
    nano ~/.bashrc
    Add the command where it makes sense (I just added to the end of the file)
    alias sshlist=”sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config”
    alias lh=”sed -rn ‘s/^\s*Host\s+(.*)\s*/\1/ip’ ~/.ssh/config”

Leave a Reply

Your email address will not be published. Required fields are marked *