Skip to Content Collective Idea Home

HOW TO: SSH Aliases

by Zach Moazeni

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 [email protected]

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 [email protected] # 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!

Comments

Keith Gaddis
::

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.


Brian Ryckbost
::

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

Zach Moazeni
::

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


Barry Hess
::

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.


Andy Keller
::

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.


Mike Krisher
::

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

alias login_stage=‘ssh [email protected] -p 2301’

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


Hussain Nagri
::

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 ?


Stuart Layton
::

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


Peter Fisher
::

Really useful thanks


fero
::

Very useful. Thanks!


Max
::

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 [email protected] while ssh bar will turn into [email protected]


toramanlis
::

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

Port xxxx


Mr. Brock Peters
::

Exactly what I needed. Wunderbar!


Mohamed
::

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


Iktiar
::

thanks, it was very helpful :)


Fredrik
::

Awesome, thank you!!


Sandeep
::

Thanks, Great port