HOW TO: SSH Aliases

When I think back on 2010, I’d have to say I SSH’d into well over 100 different machines. Late last year I found out about the SSH config file, and coming across it was one of those “Why didn’t I find out earlier?” moments.

The SSH config file is located in ~/.ssh/config or can be added if it doesn’t already exist. There is a slew of things you can add to this file (which you can find from man ssh\_config) but the neatest thing I’ve found is setting server aliases.

Let’s assume you SSH frequently with this command: ssh exampleuser@example.com

By adding this to ~/.ssh/config:

Host example
  HostName example.com
  User exampleuser

You can reduce that to previous command to ssh example

Nice huh?

Do you have a multi-server infrastructure that you log into consistently?

Host project.web1
  HostName web1.project.com
  User webadmin

Host project.web2
  HostName web2.project.com
  User webadmin

Host project.db1
  HostName db1.project.com
  User webadmin

Host project.util1
  HostName util1.project.com
  User webadmin

Host project.stage
  HostName stage.project.com
  User webadmin

Which boils down to command like

ssh project.web1
ssh project.db1
....

ssh a_different_user@project.db1 # you always have the option of overriding at the command line.

You can also specify the host by IP directly:

Host example
  HostName 127.0.0.1
  User exampleuser

And you can use different SSH keys:

Host example2
  Hostname example.com
  User exampleuser
  IdentityFile ~/.ssh/another_ssh.identity

If you’re a heavy remote user, this saves a lot of typing and lookups from READMEs and notes. It’s nice to be able to refer to my aliases rather than scattered URL and IP addresses across projects.

Another great perk: the config also affects (most) other tools that depend on SSH like SCP, Rsync, and even Transmit!

zach@collectiveidea.com

Comments

  1. February 04, 2011 at 14:31 PM

    Another good tip, if you’re a mac user who likes iTerm: the bookmarks are a total timesaver.  I also use those to set the color of my terminal, so when I’m on a production shell, the background is always red.  Its kept me from doing some stupid stuff on more than one occasion.

  2. February 04, 2011 at 14:37 PM

    If you’re using an ssh key, do you know if the pubkey authentication and forward agent option need to be added?

      PubkeyAuthentication yes  ForwardAgent yes
    
  3. February 04, 2011 at 14:41 PM

    @Brian,

    I don’t know if they have to be added. I don’t have them in my config, but I also took a look at /etc/ssh_config and didn’t see any declarations. It’s possible that those are default. The couple declarations that I use a different public.key worked without them.

    But it’s good to point out in case someone else has trouble getting it working.

  4. February 04, 2011 at 14:55 PM

    I’ve loved SSH aliases for 6 months or so, too. They’re great!

    One thing is they stopped me from sharing my dotfiles. I finally got the sense to put ssh/config in Dropbox and then in my dotfiles project I symlink over to the Dropbox version.

  5. February 04, 2011 at 15:14 PM

    I also add “Compression yes” to each of my aliases, though I can’t say I’ve carefully evaluated the tradeoff between CPU and I/O involved when doing small tasks in a shell. Maybe if you’re on a slow connection you’ll notice a difference.

    man ssh_config for more ssh config fun.

  6. February 04, 2011 at 15:21 PM

    I been doing simple bash aliasing for a number of years now, like:

    alias login_stage=’ssh user@staging.server.com -p 2301’

    May have to switch to get more structure. Great post.

  7. July 17, 2012 at 5:37 AM

    i edited the config file and it worked great for 3 days but after then it sarted giving error like
    ssh: Could not resolve hostname hostname: Name or service not known
    but with ssh hostame@SameIpAsInConfigFile it workes fine.
    The config file is intact and untouched. do you have any idea what is wrong ?

  8. stuart.layton@gmail.com
    Stuart Layton
    September 20, 2012 at 14:55 PM

    Wow! thanks for posting this, this is one of those features I find myself wanting but never taking the time to learn about.

  9. September 24, 2012 at 20:05 PM

    Really useful thanks

  10. fero@fero.sk
    fero
    December 17, 2012 at 8:47 AM

    Very useful. Thanks!

  11. collectiveidea.20.phoneysony@antichef.com
    Max
    March 21, 2013 at 9:44 AM

    You are missing the best feature yet. With new enough openssh versions you can write

    host foo bar
    hostname %h.domain.name
    username webadmin
    and then ssh foo will turn into webadmin@foo.domain.name while ssh bar will turn into webadmin@bar.domain.name

  12. timucinbahsi@gmail.com
    toramanlis
    May 05, 2013 at 12:58 PM

    you can also specify the port by simply adding this line under a certain host:

    Port xxxx

  13. conebone69er@gmail.com
    Mr. Brock Peters
    March 11, 2014 at 22:35 PM

    Exactly what I needed. Wunderbar!

  14. December 17, 2015 at 0:45 AM

    You ca also use SSHez which makes using the config file easier https://rubygems.org/gems/sshez

  15. Iktiar
    March 15, 2018 at 1:04 AM

    thanks, it was very helpful :)

  16. Fredrik
    February 19, 2019 at 20:31 PM

    Awesome, thank you!!

  17. Sandeep
    April 15, 2019 at 12:09 PM

    Thanks, Great port