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

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...