Collective Idea

Collective Idea Logo

Zach Moazeni

HOW TO: SSH Aliases

By Zach Moazeni on February 04, 2011 in howto and ssh

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!

By Zach Moazeni on February 04, 2011 in howto and ssh

13 Comments

  1. Keith Gaddis

    Keith Gaddis February 04, 2011 http://www.karmajunkie.com

    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. Brian Ryckbost

    Brian Ryckbost February 04, 2011 http://ryckbost.com

    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. Zach Moazeni

    Zach Moazeni February 04, 2011 http://collectiveidea.com

    @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. Barry Hess

    Barry Hess February 04, 2011 http://bjhess.com

    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. Andy Keller

    Andy Keller February 04, 2011 http://tractionsoftware.com

    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. Mike Krisher

    Mike Krisher February 04, 2011 http://www.mikekrisher.com

    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. Hussain Nagri

    Hussain Nagri July 17, 2012 http://www.codewoes.com

    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

    Stuart Layton September 20, 2012

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

  9. Peter Fisher

    Peter Fisher September 24, 2012 http://blog.peterfisher.me.uk

    Really useful thanks

  10. fero

    fero December 17, 2012

    Very useful. Thanks!

  11. Max

    Max March 21, 2013 http://max-klinger.org

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

    toramanlis May 05, 2013

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

    Port xxxx

  13. Mr. Brock Peters

    Mr. Brock Peters March 11, 2014

    Exactly what I needed. Wunderbar!

Post a Comment

Contact Us

Find us on Google Maps
Collective Idea
44 East 8th Street, Suite 410
Holland, Michigan 49423 USA 42.790334-86.105251

Follow us on the Interwebs

We are currently available for medium and long term projects. Please get in touch if we can be of service.