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

Sample code using scss in uni-app

Pitfalls encountered I spent the whole afternoon ...

Dockerfile text file usage example analysis

Dockerfile is a text file used to build an image....

Mobile terminal adaptation makes px automatically converted to rem

Install postcss-pxtorem first: npm install postcs...

How to limit the number of concurrent connection requests in nginx

Introduction The module that limits the number of...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

Centos6.5 glibc upgrade process introduction

Table of contents Scenario Requirements glibc ver...

Common parameters of IE web page pop-up windows can be set by yourself

The pop-up has nothing to do with whether your cur...

Vue implements seamless scrolling of lists

This article example shares the specific code of ...

Solution to Docker's failure to release ports

Today I encountered a very strange situation. Aft...

HTML meta usage examples

Example Usage Copy code The code is as follows: &l...

Detailed explanation of how to install MariaDB 10.2.4 on CentOS7

CentOS 6 and earlier versions provide MySQL serve...