If you frequently access many different remote systems via SSH, this technique will save you some time. You can create SSH aliases for frequently accessed systems via SSH, so you don’t have to remember all the different usernames, hostnames, SSH port numbers, and IP addresses. In addition, it avoids repeatedly entering the same username, hostname, IP address, and port number when SSHing to a Linux server.
Create an SSH alias in Linux
Before I know this trick, I usually use one of the following methods to connect to a remote system via SSH.
Use IP address:
$ ssh 192.168.225.22
Or use the port number, username, and IP address:
$ ssh -p 22 firstname.lastname@example.org
Or use the port number, username, and hostname:
$ ssh -p 22 email@example.com
22 is the port number,
ec2-user is the username 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, remembering all hostnames or IP addresses, as well as usernames, is difficult unless you write them on paper or save them in a text file. do not 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 – Use an SSH Profile
This is my preferred method of creating an alias.
We can use the SSH default configuration file to create an SSH alias. To do this, edit the ~/.ssh/config file (if this file doesn’t exist, just create one):
$ vi ~/.ssh/config
Add details for all remote hosts as follows:
Create an SSH alias in Linux using an SSH configuration file
Replace the values ??of the Host, Hostname, User, and Port configuration 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 command :
$ ssh webserver
$ ssh dns
$ ssh dhcp
It’s that simple!
Access remote systems using SSH aliases
see it? I only use an alias (such as webserver) to access a remote system with an IP address of 192.168.225.22.
Please note that this is only for the current user. If you want to provide an alias for all users (system-wide), add the above line to the /etc/ssh/ssh_config file.
You can also add a lot of other content to your SSH configuration file. For example, if you have configured SSH key-based authentication, the location of the SSH key file is as follows:
Make sure you have replaced your 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 you want to access via SSH and quickly access them using aliases.
Method 2 – Use a Bash Alias
This is an emergency workaround for creating SSH aliases that speed up communication. You can make this taec2-user easier with the alias command.
Open the ~/.bashrc or ~/.bash_profile file:
Alias ??webserver=’ssh firstname.lastname@example.org’
Alias ??dns=’ssh email@example.com’
Alias ??dhcp=’ssh firstname.lastname@example.org -p 2233′
Alias ??ubuntu=’ssh email@example.com -i ~/.ssh/id_rsa_remotesystem’
Again, make sure 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
$ 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.