Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to create SSH aliases in Linux

Shulou Source: shulou.com Published: 2022-06-01 16:53:46 10月03日 Update

In this issue, the editor will bring you about how to create SSH aliases in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Create a SSH alias in Linux

Before I knew this trick, I usually used any of the following ways to connect to a remote system through SSH.

Use the IP address:

$ssh 192.168.225.22

Or use the port number, user name, and IP address:

$ssh-p 22 sk@192.168.225.22

Or use the port number, user name, and hostname:

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

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 hostname of the remote system.

I believe that most Linux newcomers and (or some) administrators will connect to remote systems through SSH in this way. However, if you connect to multiple different systems through SSH, it is difficult to remember all hostnames or IP addresses, and 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 the SSH command in two ways.

Method 1-use SSH profile

This is my method of creating aliases.

We can use the SSH default configuration file to create SSH aliases. To do this, edit the ~ / .ssh/config file (if this file 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

Use the SSH configuration file to create a SSH alias in Linux

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

Now you can enter the system through SSH using the following command:

$ssh webserver$ ssh dns$ ssh dhcp

It's that simple!

Look at the screenshot below.

Access remote systems using SSH aliases

You see that? I only use aliases (such as webserver) to access the remote system with the IP address 192.168.225.22.

Please note that this is only used by the current user. If you want to provide aliases for all users (system-wide), add the above line to the / etc/ssh/ssh_config file.

There are many other things you can add to the SSH configuration file. For example, if you have configured authentication based on SSH keys, explain 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 that you have replaced the hostname, user name, and SSH key file path with your own values.

Now connect to the remote server using the following command:

$ssh ubuntu

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

Method 2-use Bash alias

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

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

Alias webserver='ssh sk@server.example.com'alias dns='ssh sk@server.example.com'alias dhcp='ssh sk@server.example.com-p 2233'alias ubuntu='ssh sk@server.example.com-I ~ / .ssh/id_rsa_remotesystem'

Make sure again that you have replaced the host, hostname, port number, and IP address with your own values. Save the file and exit.

Then use the command to apply the changes:

$source ~ / .bashrc

Or

$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

This is how to create SSH aliases in the Linux shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Aliases files systems hosts users commands configurations addresses methods user names slogans content keys methods information just that is this is Zhongchuang analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Linux Docker macOS