If you frequently access many remote systems via SSH, this trick will save you some time. You can create SSH alias to frequently-accessed systems via SSH. This way you don't have to remember all the different usernames, hostnames, ssh port numbers and IP addresses etc. Additionally, It avoids the need to repetitively type the same username/hostname, ip address, port number whenever you SSH into a Linux server(s).
Create SSH alias in Linux
Before I know this trick, usually, I connect to a remote system over SSH using anyone of the following ways.
Using IP address:
$ ssh 192.168.225.22
Or using port number, username and IP address:
$ ssh -p 22 email@example.com
Or using port number, username and hostname:
$ ssh -p 22 firstname.lastname@example.org
22is the port number,
skis the username of the remote system,
192.168.225.22is the IP of my remote system,
server.example.comis the hostname of remote system.
I believe most of the newbie Linux users and/or admins would SSH into a remote system this way. However, If you SSH into multiple different systems, remembering all hostnames/ip addresses, usernames is bit difficult unless you write them down in a paper or save them in a text file. No worries! This can be easily solved by creating an alias (or shortcut) for SSH connections.
We can create an alias for SSH commands in two methods.
Method 1 - Using SSH config file
This is my preferred way of creating aliases.
We can use SSH default configuration file to create SSH alias. To do so, edit
~/.ssh/config file (If this file doesn't exist, just create one):
$ vi ~/.ssh/config
Add all of your remote hosts details like 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
Replace the values of
Port with your own. Once you added the details of all remote hosts, save and exit the file.
Now you can SSH into the systems with commands:
$ ssh webserver
$ ssh dns
$ ssh dhcp
It is simple as that.
Have a look at the following screenshot.
See? I only used the alias name (i.e webserver) to access my remote system that has IP address 192.168.225.22.
Please note that this applies for current user only. If you want to make the aliases available for all users (system wide), add the above lines in
You can also add plenty of other things in the SSH config file. For example, if you have configured SSH Key-based authentication, mention the SSH keyfile location as below.
Host ubuntu HostName 192.168.225.50 User senthil IdentityFIle ~/.ssh/id_rsa_remotesystem
Make sure you have replaced the hostname, username and SSH keyfile path with your own.
Now connect to the remote server with command:
$ ssh ubuntu
This way you can add as many as remote hosts you want to access over SSH and quickly access them using their alias name.
Method 2 - Using Bash aliases
This is quick and dirty way to create SSH aliases for faster communication. You can use the alias command to make this task much easier.
~/.bash_profile file in your preferred text editor:
$ nano ~/.bashrc
$ sudo nano ~/.bash_profile
Add aliases for each SSH connections one by one like below.
alias webserver='ssh email@example.com' alias dns='ssh firstname.lastname@example.org' alias dhcp='ssh email@example.com -p 2233' alias ubuntu='ssh firstname.lastname@example.org -i ~/.ssh/id_rsa_remotesystem'
Again make sure you have replaced the host, hostname, port number and ip address with your own. Save the file and exit.
Then, apply the changes using command:
$ source ~/.bashrc
$ source ~/.bash_profile
In this method, you don't even need to use
"ssh alias-name" command. Instead, just use alias name only like below.
$ webserver $ dns $ dhcp $ ubuntu
These two methods are very simple, yet useful and much more convenient for those who often SSH into multiple different systems. Use any one of the aforementioned methods that suits for you to quickly access your remote Linux systems over SSH.
- Allow Or Deny SSH Access To A Particular User Or Group In Linux
- How To SSH Into A Particular Directory On Linux
- How To Stop SSH Session From Disconnecting In Linux
- 4 Ways To Keep A Command Running After You Log Out Of The SSH Session
- SSLH – Share A Same Port For HTTPS And SSH
- How To Find If A User Is Using Password-based Or Key-based SSH Authentication In Linux
I use shy https://github.com/xvxx/shy
“mention the SSH keyfile location” is a good point to make.
This is particularly useful if you have multiple SSH keyfiles.
One particular reason for having mulitple SSH key files is because of different key file formats eg ed25519 and rsa since not all hosts support ed25519 (particularly embedded devices) and specifying which key file to use can help to speed up making the connection.