How to Create an SSH Shortcut

If you are constantly needing to SSH into multiple servers, it can real daunting to remember all the different usernames, hostnames, IP addresses, and even sometimes custom private keys to connect to them. It's actually extremely easy to create command line shortcuts to solve this problem. There's two major ways to do it, and we'll discuss the pros and cons of each.

SSH on *NIX machines, such as Linux or Mac, have default shortcut functionality right out of the box. It's very straight forward to setup, too. For those two reasons, this is my preferred way of setting up SSH shortcuts. The first step is to navigate to your .ssh folder:

bash
cd ~/.ssh

Following this, you'll need to create a file called config. Here's how to do it with Vim:

bash
vim config

From here, you can now create shortcuts. You can specify the hostname, username, port, and the private key. For a full list of options, please visit the official docs. Here's an example of how to structure the file:

bash
Host betterdev
  HostName better.dev
  User nick

Host example2
  HostName example.com
  User root

Host example3
  HostName 64.233.160.0
  User userxyz123
  Port 56000

Host amazon1
  HostName ec2.amazon.com
  User ec2-user
  IdentityFile /path/to/special/privatekey/amazon.pem

Now, you can simply SSH into any of these servers with these simple commands:

bash
ssh betterdev
ssh example2
ssh example3
ssh amazon1

If this isn't working for you, trying changing the permissions of the config file like this:

bash
chmod 600 ~/.ssh/config

This method involves creating an alias for your shell (or terminal). You can use this for creating any type of shortcut you want, but a lot of people use them for SSH shortcuts. To set this up, you'll need to navigate to your .bash_aliases file (or some people do this in .bashrc or .bash_profile). The following command will create the .bash_aliases file if it doesn't exist or just edit it if it already does using Vim.

bash
vim ~/.bash_aliases

Here you can add as many shortcuts as you want. Here's how to add the same SSH shortcuts from above:

bash
alias betterdev='ssh nick@better.dev'
alias example2='ssh root@example.com'
alias example3='ssh userxyz123@64.233.160.0 -p 56000'
alias amazon1='ssh ec2-user@ec2.amazon.com -i /path/to/special/privatekey/amazon.pem'

After you add those and save the file, you'll need to "reboot" the aliases file with:

bash
source ~/.bash_aliases

Once that is completed, you can now SSH into all of those same boxes by just typing the following:

bash
betterdev
example2
example3
amazon1

This method provides additional flexibility that the first method might not be able to provide, but it really comes down to a matter of preference for most use cases.

Chris Sev

Chris Sev

Chris Sev is the co-founder of Better Dev. Coding better every day. Previously he created Scotch.io which was acquired.

In this article...

Comments

What did you think of the article? Let us know!
(these comments are powered by GitHub issues and use 0 trackers)