Two ways to create SSH server aliases in Linux

Two ways to create SSH server aliases in Linux

Preface

If you frequently access many different remote systems via SSH, this trick will save you some time. You can create SSH aliases for systems you frequently access through SSH so that you don't have to remember all the different usernames, hostnames, SSH port numbers, IP addresses, etc. Moreover, it avoids typing the same username, hostname, IP address, port number repeatedly when SSHing into a Linux server.

Creating SSH Aliases in Linux

Before I knew this trick, I usually used any of the following methods to connect to remote systems via SSH.

Using IP Address:

$ ssh 192.168.225.22

Or using port number, username, and IP address:

$ ssh -p 22 [email protected]

Or using port number, username, and hostname:

$ ssh -p 22 [email protected]

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

I believe that most Linux newbies and/or some administrators connect to remote systems via SSH in this manner. However, if you connect to many different systems via SSH, remembering all the hostnames or IP addresses, as well as usernames, can be difficult unless you write them down on paper or save them in a text file. don’t worry! This can be easily solved by creating an alias (or shortcut) for the SSH connection.

We can create aliases for SSH commands in two ways.

Method 1 – Using SSH Configuration File

This is my preferred 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 it doesn't exist, just create one):

$ vi ~/.ssh/config

Add the details of all remote hosts as shown 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

Creating SSH Aliases in Linux Using SSH Configuration File

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

Now you can SSH into the system using the following command:

$ ssh webserver
$ ssh dns
$ ssh dhcp

It's that simple!

Take a look at the screenshots below.

Accessing remote systems using SSH aliases

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

Note that this only applies to the current user. If you want to provide alias for all users (system wide), add above line in /etc/ssh/ssh_config file.

There are many other things you can add to your SSH configuration file. For example, if you have configured SSH key-based authentication, indicate 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 to replace the hostname, username, and SSH key file path with your own values.

Now connect to the remote server using the following command:

$ ssh ubuntu

This way, you can add as many remote hosts as you want to access via SSH and quickly access them using aliases.

Method 2 – Using Bash Aliases

This is a workaround to create an SSH alias to speed up communications. You can use the alias command to make this task easier.

Open ~/.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 you replace the host, hostname, port number, and IP address with your own values. Save the file and exit.

Then, apply the changes using the command:

$ 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 

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

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to install docker on Linux system and log in to docker container through ssh
  • Detailed explanation of 7 SSH command usages in Linux that you don’t know
  • Detailed explanation of sshd service and service management commands under Linux
  • How to upload files and folders to Linux server via SSH
  • Detailed explanation of Linux SSH login process
  • How to configure ssh to log in to Linux using git bash

<<:  React configuration px conversion rem method

>>:  Window environment configuration Mysql 5.7.21 windowx64.zip free installation version tutorial detailed explanation

Recommend

A brief discussion on the magical uses of CSS pseudo-elements and pseudo-classes

CSS plays a very important role in a web page. Wi...

Mysql 5.7.17 winx64 installation tutorial on win7

Software version and platform: MySQL-5.7.17-winx6...

Methods of adaptive web design (good access experience on mobile phones)

1. Add the viewport tag to the HTML header. At th...

What is a MySQL index? Ask if you don't understand

Table of contents Overview From Binary Tree to B+...

Building command line applications with JavaScript

Table of contents 1. Install node 2. Install Comm...

Teach you how to achieve vertical centering elegantly (recommended)

Preface There are many ways to center horizontall...

Set the input to read-only via disabled and readonly

There are two ways to achieve read-only input: dis...

...

...

MySQL Series 8 MySQL Server Variables

Tutorial Series MySQL series: Basic concepts of M...

How does Vue implement communication between components?

Table of contents 1. Communication between father...

Paragraph layout and line breaks in HTML web pages

The appearance of a web page depends largely on i...