Tutorial

How to Create an SSH Shortcut

Updated on September 15, 2020
    Default avatar

    By Nicholas Cerminara

    How to Create an SSH Shortcut

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    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.

    Method 1: Using SSH Config

    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:

    cd ~/.ssh
    

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

    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:

    Host scotch
        HostName scotch.io
        User nick
    
    Host example2
        HostName example.com
        User root
    
    Host example3
        HostName 64.233.160.0
        User userxyz123
        Port 56000
    
    Host droplet1
        HostName droplet1.digitalocean.com
        User ec2-user
        IdentityFile /path/to/special/privatekey/droplet1.pem
    

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

    ssh scotch
    ssh example2
    ssh example3
    ssh droplet1
    

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

    chmod 600 ~/.ssh/config
    

    Method 2: Create aliases for your shell

    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.

    vim ~/.bash_aliases
    

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

    alias scotch='ssh nick@scotch.io'
    alias example2='ssh root@example.com'
    alias example3='ssh userxyz123@64.233.160.0 -p 56000'
    alias droplet1='ssh ec2-user@droplet1.digitalocean.com -i /path/to/special/privatekey/droplet1.pem'
    

    After you add those and save the file, you’ll need to “reboot” the aliases file with:

    source ~/.bash_aliases
    

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

    scotch
    example2
    example3
    droplet1
    

    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.

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Nicholas Cerminara

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    1 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    pls update: u need restart bash session (logout)

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel