If you frequently use your Linux system to connect to a specific host, it can be convenient to make an alias for the hostname or IP address. This is especially true if the host has a long name or URL, and you do not want to keep typing the whole thing out every time you need to connect. There are several ways to create a host alias on Linux, depending on how you ordinarily connect to the host.
It is possible to create an SSH alias, a custom environment variable, or map an IP address to a hostname or URL in the
/etc/hosts file. All will accomplish the same goal, which is to save you some keystrokes on the command line when connecting to the host. In this tutorial, you will learn various methods to crate a host alias on Linux.
In this tutorial you will learn:
- How to create host alias via SSH config
- How to create host alias via environment variable
- How to create host alias via
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to create host alias on Linux
The method for creating a host alias will depend on the application. We will run through various methods below – choose whichever one best applies for your situation.
Create host alias for SSH
If you need to create a host alias to faciliate faster SSH connections, this is the method for you. Let’s say you usually use the following command for SSH:
$ ssh email@example.com
Typing this long username and hostname (or IP address) each time would be cumbersome, so instead, we could reduce it to something simple, like:
$ ssh myserver
Follow our step by step instructions to apply this change:
- First, open the
~/.ssh/configfile in nano or your preferred text editor. If this file does not already exist, we will be creating it.
$ nano ~/.ssh/config
- Inside of this file, we would use the following settings to achieve the desired configuration as shown above:
Host myserver User longusername HostName someserver.alongdomainname.com Port 22022
Note that the
Portsetting is optional, but could save you some additional keystrokes if you the remote server does not run SSH on the default port 22.
- After saving your changes to the file, you will be able to SSH by specifying the host alias.
$ ssh myserver
After following these instructions, you may also want to configure SSH to connect without password.
Create host alias via environment variable
Using an environment variable allows you to access the host alias using any program on the command line. For example, instead of typing this command:
$ ping someserver.alongdomainname.com
We could reduce this to something much simpler, such as:
$ ping $myserver
Note: the host gets referenced via the
$ dollar sign in front of our chosen alias.
- First, open the
~/.bashrcfile in nano or your preferred text editor:
$ nano ~/.bashrc
- At the bottom of this file, put your alias and the hostname or IP address. In this example, we will use alias
- After saving your changes and exiting the file, execute this command to make the changes take effect:
$ source ~/.bashrc
- You can now use this environment variable whenever you would ordinarly have to type the entire hostname or IP address.
$ ping $myserver OR $ ssh $myserver etc...
The previous instructions show how to make an environment variable only for the current user. To see how to make a system wide environment variable, see our tutorial on How to set and list environment variables on Linux.
Create host alias in hosts file
The last method for creating a host alias will only work if the host has an IP address that never changes, since this method requires us to tie an alias to an IP address.
As an example, we will make an alias
myserver for the IP address
- Let’s start by editing the
/etc/hostsfile with root permissions. You can use nano or your preferred text editor for this.
$ sudo nano /etc/hosts
- At the end of this file, we will apply the following changes:
Note that you can apply more than one alias if you wish:
192.168.1.100 myserver othername
- Save your changes to the file and close it. Now, using the
myserveralias will reference the IP address we configured.
$ ping myserver OR $ ssh myserver etc...
To learn more about the
/etc/hosts file and how it works, see our other tutorial on Hosts file example on Linux.
In this tutorial, we saw how to create a host alias on a Linux system. There is no simple, universal way to achieve this, but we have a few options, depending on the application, as we saw in this guide. Creating a host alias via SSH config, environment variable, or
hostsfile should save you some repetitive keystrokes and time on the command line.