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
hosts
file

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – 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 longusername@someserver.alongdomainname.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/config
file 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
Port
setting 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
Much easier!
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
~/.bashrc
file 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
myserver
for hostnamesomeserver.alongdomainname.com
.export myserver='someserver.alongdomainname.com'
- 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 192.168.1.100
.
- Let’s start by editing the
/etc/hosts
file 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:
192.168.1.100 myserver
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
myserver
alias 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.
Closing Thoughts
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
hosts
file should save you some repetitive keystrokes and time on the command line.