Tools Blog Learn Quizzes Smile API Log In / Sign Up
Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

SSH shortcuts through Bash aliases on Ubuntu

Difficulty: 30 / 50 Tweet
Ubuntu-Trusty-Tahr

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
              fi
            
          
  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 YOUR_REMOTE_USERNAME@THE_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.

comments powered by Disqus