Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

SSH shortcuts through Bash aliases on Ubuntu

Daniel Gheorghe Difficulty: 30 / 50 Tweet

Update: There is a better way of doing this. Click here to see how!

In today's tutorial I will show you how to use Bash aliases to create 'shortcuts' to your ssh connections on Ubuntu. This will save you a lot of typing and remembering which private key is used for which server.

All you need to do that is to type a few simple commands, so open up your terminal and follow the steps below. I also made a video so you can just watch the screen-cast at the bottom of the article.

  1. First check if you have the snippet listed below inside your '.bashrc' file. It is used to include the aliases file, so if you don't have it, please paste it in.
              if [ -f ~/.bash_aliases ]; then
                  . ~/.bash_aliases
  2. Now you can create .bash_aliases in your home folder and define the alias for an SSH connection using the template below:
              alias a_name_for_the_alias='sudo ssh -p PORT_NUMBER -o IdentityFile=~/.ssh/YOUR_PRIVATE_KEY_FILE [email protected]_REMOTE_SERVER'
  3. Finally you need to refresh the configuration by executing .bashrc.
              source ~/.bashrc

Update ... achieve the same by using your SSH configuration file

Thanks to mooism2 who pointed out a better way of doing this, I am updating this article with a snippet that should be added to your SSH configuration file.

  # contents of $HOME/.ssh/config
Host to_dev
    HostName dev.yourdomain.com
    Port 22000
    IdentityFile ~/.ssh/some.key
    User username

Then, typing `ssh to_dev` will take you to your remote server.