How to Create an Alias in Linux Using the alias Command

If you use the Linux terminal frequently, you likely find yourself typing the exact same long, complex commands over and over again. For example, typing sudo apt update && sudo apt upgrade -y every time you want to update your system is tedious and prone to typos.

To save time and keystrokes, Linux allows you to create an alias. An alias is simply a custom shortcut word that you define. When you type that short word, the terminal automatically executes the long command it represents.

How to Create a Temporary Alias

You can create an alias directly in your terminal using the alias command.

The syntax is very strict: alias shortcutname='command to run'. (Note: There cannot be any spaces before or after the equals sign!)

For example, if you want to create a shortcut named update that updates your Debian/Ubuntu system, you would type:

alias update='sudo apt update && sudo apt upgrade -y'

Press Enter. Now, simply type update into the terminal and press Enter, and Linux will run the full apt upgrade command.

The Catch: Any alias created this way is temporary. The moment you close the terminal window or end your SSH session, the alias is deleted forever.

How to Create a Permanent Alias

To make an alias permanent so that it works every time you open the terminal, you must save it in your shell’s configuration file. If you are using the default Bash shell, this file is named .bashrc and is located in your home directory.

  1. Open your terminal.
  2. Open the .bashrc file in a text editor like nano:
    nano ~/.bashrc
  3. Use the arrow keys to scroll all the way to the very bottom of the file.
  4. Type your aliases on new lines at the bottom. It is good practice to leave a comment so you remember what they are.

    # My Custom Aliases
    alias update='sudo apt update && sudo apt upgrade -y'
    alias c='clear'
    alias ports='sudo netstat -tulanp'

  5. Save the file in nano by pressing Ctrl+O, hitting Enter to confirm, and then pressing Ctrl+X to exit.

Before the aliases will work, you must tell the terminal to reload the configuration file. Run this command:

source ~/.bashrc

Your custom shortcuts are now permanently saved and ready to use.

How to View and Remove Aliases

If you forget what aliases you have created, simply type the command by itself:

alias

This will print a list of every active alias on your system. You will likely see several aliases that your Linux distribution configured for you automatically (such as alias ls='ls --color=auto').

If you want to temporarily disable an alias during your current session, use the unalias command followed by the shortcut name:

unalias update

To permanently remove an alias, you must open your ~/.bashrc file in a text editor and delete the line of code you wrote.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.