Skip to main content

How To Create SSH Alias In Linux

If you frequently access a lot of different remote systems via SSH, this trick will save you some time. You can create SSH alias to frequently-accessed systems via SSH. This way you need not to remember all the different usernames, hostnames, ssh port numbers and IP addresses etc. Additionally, It avoids the need to repetitively type the same username/hostname, ip address, port no whenever you SSH into a Linux server(s).

Create SSH Alias In Linux

Before I know this trick, usually, I connect to a remote system over SSH using anyone of the following ways.

Using IP address:

$ ssh 192.168.225.22

Or using port number, username and IP address:

$ ssh -p 22 sk@192.168.225.22

Or using port number, username and hostname:

$ ssh -p 22 sk@server.example.com

Here,

  • 22 is the port number,
  • sk is the username of the remote system,
  • 192.168.225.22 is the IP of my remote system,
  • server.example.com is the hostname of remote system.

I believe most of the newbie Linux users and/or admins would SSH into a remote system this way. However, If you SSH into multiple different systems, remembering all hostnames/ip addresses, usernames is bit difficult unless you write them down in a paper or save them in a text file. No worries! This can be easily solved by creating an alias(or shortcut) for SSH connections.


We can create an alias for SSH commands in two methods.

Method 1 – Using SSH Config File

This is my preferred way of creating aliases.

We can use SSH default configuration file to create SSH alias. To do so, edit ~/.ssh/configfile (If this file doesn’t exist, just create one):

$ vi ~/.ssh/config

Add all of your remote hosts details like below:

Host webserver
    HostName 192.168.225.22
    User sk

Host dns
    HostName server.example.com
    User root

Host dhcp
    HostName 192.168.225.25
    User ostechnix
    Port 2233
Create SSH Alias In Linux

Create SSH Alias In Linux Using SSH Config File

Replace the values of HostHostnameUserand Port with your own. Once you added the details of all remote hosts, save and exit the file.

Now you can SSH into the systems with commands:

$ ssh webserver
$ ssh dns
$ ssh dhcp

It is simple as that.

Have a look at the following screenshot.

create ssh alias

Access remote system using SSH alias

See? I only used the alias name (i.e webserver) to access my remote system that has IP address 192.168.225.22.

Please note that this applies for current user only. If you want to make the aliases available for all users (system wide), add the above lines in /etc/ssh/ssh_config file.


You can also add plenty of other things in the SSH config file. For example, if you have configured SSH Key-based authentication, mention the SSH keyfile location as below.

Host ubuntu
    HostName 192.168.225.50
    User senthil
    IdentityFIle ~/.ssh/id_rsa_remotesystem

Make sure you have replace the hostname, username and SSH keyfile path with your own.

Now connect to the remote server with command:

$ ssh ubuntu

This way you can add as many as remote hosts you want to access over SSH and quickly access them using their alias name.

Method 2 – Using Bash aliases

This is quick and dirty way to create SSH aliases for faster communication. You can use the alias command to make this task much easier.

Open ~/.bashrc or ~/.bash_profile file:

Add aliases for each SSH connections one by one like below.

alias webserver='ssh sk@192.168.225.22'
alias dns='ssh root@server.example.com'
alias dhcp='ssh ostechnix@192.168.225.25 -p 2233'
alias ubuntu='ssh senthil@192.168.225.50 -i ~/.ssh/id_rsa_remotesystem'

Again make sure you have replaced the host, hostname, port number and ip address with your own. Save the file and exit.

Then, apply the changes using command:

$ source ~/.bashrc

Or,

$ source ~/.bash_profile

In this method, you don’t even need to use “ssh alias-name” command. Instead, just use alias name only like below.

$ webserver
$ dns
$ dhcp
$ ubuntu

create ssh alias 1

These two methods are very simple, yet useful and much more convenient for those who often SSH into multiple different systems. Use any one of the aforementioned methods that suits for you to quickly access your remote Linux systems over SSH.

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...