Two Ways to Create SSH Server Aliases in Linux System

Time:2019-9-16

Preface

If you often access many different remote systems through SSH, this technique will save you some time. You can create SSH aliases for frequently accessed systems through SSH, so you don’t have to remember different user names, host names, SSH port numbers, IP addresses, etc. In addition, it avoids re-entering the same user name, host name, IP address and port number when SSH goes to the Linux server.

Create SSH aliases in Linux

Before I know this technique, I usually connect to a remote system via SSH in any of the following ways.

Use IP address:


$ ssh 192.168.225.22

Or use port number, username and IP address:


$ ssh -p 22 [email protected]

Or use port number, user name and host name:


$ ssh -p 22 [email protected]

Here

  • 22 is the port number.
  • Sk is the user name of the remote system.
  • 192.168.225.22 is the IP of my remote system.
  • Server.example.com is the host name of the remote system.

I believe that most Linux novices and (or some) administrators will connect to remote systems via SSH in this way. However, if you connect to multiple different systems via SSH, it is difficult to remember all hostnames or IP addresses, as well as user names, unless you write them on paper or save them in a text file. Don’t worry! This can be easily solved by creating aliases (or shortcuts) for SSH connections.

We can create aliases for SSH commands in two ways.

Method 1 – Use SSH configuration file

This is my preferred way to create aliases.

We can use SSH default configuration files to create SSH aliases. To do this, edit the ~/.ssh/config file (if it does not exist, just create one):


$ vi ~/.ssh/config

Add details for all remote hosts as follows:

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 aliases in Linux using SSH configuration files

Replace the values of Host, Hostname, User, and Port configurations with your own values. After adding the details of all remote hosts, save and exit the file.

Now you can access the system via SSH using the following commands:


$ ssh webserver
$ ssh dns
$ ssh dhcp

It’s that simple!

Look at the screenshot below.

Accessing remote systems using SSH aliases

Did you see? I only use aliases (such as webserver) to access remote systems with IP addresses 192.168.225.22.

Note that this is only for current users. To provide aliases for all users (system-wide), add the above lines to the / etc/ssh/ssh_config file.

You can also add many other things to the SSH configuration file. For example, if you have configured SSH key based authentication, specify the location of the SSH key file as follows:

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

Make sure you have replaced the hostname, username, and SSH key file path with your own values.

Now connect to the remote server using the following commands:


$ ssh ubuntu

In this way, you can add any number of remote hosts you want to access through SSH and use aliases to quickly access them.

Method 2 – Use Bash aliases

This is an emergency alternative to creating SSH aliases, which can speed up communication. You can use the alias command to make this task easier.

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


alias webserver='ssh [email protected]'
alias dns='ssh [email protected]'
alias dhcp='ssh [email protected] -p 2233'
alias ubuntu='ssh [email protected] -i ~/.ssh/id_rsa_remotesystem'

Again, make sure that you have replaced the host, host name, port number and IP address with your own values. Save the file and exit.

Then, use the command to apply the changes:


$ source ~/.bashrc

perhaps


$ source ~/.bash_profile

In this method, you don’t even need to use the SSH alias command. Instead, just use an alias, as shown below.


$ webserver
$ dns
$ dhcp
$ ubuntu

These two methods are very simple, but they are very useful and convenient for people who often connect to multiple different systems through SSH. Use any of the above methods for you to quickly access remote Linux systems via SSH.

summary

Above is the whole content of this article. I hope the content of this article has some reference value for your study or work. Thank you for your support to developpaer.

Recommended Today

Sharing 10 useful methods of laravel 5.8 sets

This article is forwarded from the professional laravel developer community, original link: https://learnku.com/laravel/t… In laravel, there is a very useful class for manipulating arrays, called collections. I believe that every developer of laravel has used the collection more or less, especially when operating eloquent. In this article, I will list 10 common methods. 1. Weight […]