How to Create an Alias in Linux

Use aliases as shortcuts for Linux commands

HTML code on a black screen

Pixabay

 

Sometimes Linux commands can get a bit unwieldy to type. Either that, or you might have trouble remembering certain options or flags. Fortunately, you can use a Linux alias to get around this.

For example, if you have three servers you regularly secure shell into you may have three separate commands:

ssh bethany@192.168.1.75
ssh bethany@192.168.1.13
ssh bethany@192.168.1.89

You don’t want to have to remember that 192.168.1.75 is your web server, 192.168.1.13 is your Nextcloud server, and 192.168.1.89 is your FTP server. Wouldn’t it be nice if you could just type the commands web, nextcloud, or FTP to automatically secure shell into those servers?

That's where Linux aliases can make things so much easier.

We’ll be testing on Pop!_OS, but aliases have been around for quite some time. Because of the long history of aliases, these instructions will work on nearly every distribution of Linux available.

Creating an Alias

Typically, aliases are created in the ~/.bashrc file and are in the form:

alias ALIASNAME=’COMMAND’

Where ALIASNAME is the nickname for the command and COMMAND is the actual command.

However, it’s often best to create your aliases in a separate file, so as not to render the .bashrc file (one crucial to your ability to log in and use your Linux account) unusable. Because of this, you'll open a terminal window and create a new file with the command nano ~/.bash_aliases. This will open a new file for editing, where you can add your aliases (in the same form you would within .bashrc).

Keeping with our example, we’ll create three aliases to make logging into our remote servers easy. Our aliases will look like:

alias web='ssh bethany@192.168.1.75'
alias nextcloud="bethany@192.168.1.13'
alias FTP='ssh bethany@192.168.1.89'

Remember to change the usernames and IP addresses to match your needs.

Screenshot of an alias.

Save and close that file by typing Ctrl+X on your keyboard. Do not close your terminal yet. Before you do that, you need to test the newly created aliases. Open a new terminal window and type one of the nicknames (either web, nextcloud, or FTP). You should be prompted for the remote user’s password, which indicates the alias is working.

Screenshot of the alias in action.

Modifying Other Commands

Another really handy use for aliases is in the modification of other commands. Say, for instance, you never run the ls command by itself. Instead, you always use the command ls -la (for long form, showing hidden files and folders). Instead of typing that command every time, you could create the alias:

alias lsa=’ls -la’

Add the above alias to your .bash_aliases file and you can then issue the command lsa to see the long form listing of a directory, including hidden files and folders.

Screenshot of the lsa alias in action.

Enjoy Those Shortcuts

And that’s the gist of using aliases to create shortcuts for Linux commands. This is a great way to save time and typing effort on Linux. Get creative with aliases and your experience will be much more efficient.