How to Create an Alias in Linux

Working in the Linux terminal often involves typing the same long, complex commands dozens of times a day. For example, fully updating a Debian-based system requires typing sudo apt update && sudo apt upgrade -y. Navigating to a deeply buried web directory might require cd /var/www/html/public_html/assets/css. Typing these out repeatedly is inefficient and prone to typographical errors. Linux solves this problem through aliases. An alias is essentially a custom keyboard shortcut—a short, memorable word that you define, which the terminal automatically expands into a much longer, complex command.

How Aliases Work

The concept is simple text replacement. If you create an alias called update, the moment you type update and hit Enter, the terminal intercepts the word, checks its internal list of aliases, sees what it stands for, and silently executes the full command (e.g., the apt update process) instead. Aliases can include multiple chained commands, specific flags, and even complex piped outputs.

Method 1: Creating a Temporary Alias

If you are working on a quick project and only need a shortcut for the current session, you can create a temporary alias directly in the terminal.

  1. Open your terminal or connect via SSH.
  2. The syntax is: alias short_name='long command'
  3. For example, to create a shortcut to list files with maximum detail, type:
    alias ll='ls -alF'
  4. Press Enter.

Now, whenever you type ll, it will execute ls -alF. However, this is temporary. The moment you close the terminal window or log out of your SSH session, the alias is permanently deleted.

Method 2: Creating a Permanent Alias

To make an alias permanent, you must save it inside your shell’s configuration file. When you open a terminal, it reads this configuration file and loads all the aliases into memory. For the vast majority of Linux users, the default shell is Bash, and the configuration file is .bashrc (located in your home directory).

  1. Open your terminal.
  2. Open your Bash configuration 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. (It is best practice to keep all your custom aliases grouped together at the end so they are easy to find).
  4. Add a comment (starting with #) to organize your file, then define your aliases exactly as you did in the temporary method:
    # My Custom Aliases
    alias update='sudo apt update && sudo apt upgrade -y'
    alias web='cd /var/www/html'
    alias ports='sudo ss -tulnp'
  5. Save the file and exit the text editor (in Nano, press Ctrl+O, Enter, then Ctrl+X).
  6. Your current terminal session doesn’t know about the changes yet. You must force it to re-read the configuration file by typing:
    source ~/.bashrc

These shortcuts are now permanently active on your user account. Every time you log in, typing update will trigger the full system update.

Troubleshooting and Best Practices

  • No Spaces Around the Equals Sign: The most common mistake when creating an alias is adding spaces. alias update = 'command' will fail. It must be exactly alias update='command' with no spaces around the = sign.
  • Overwriting System Commands: Be careful what you name your alias. If you create an alias named cd, you will overwrite the system’s actual directory change command, potentially breaking your ability to navigate. Always choose unique names (like up instead of update if you prefer, just ensure it doesn’t conflict with existing tools).
  • Viewing Current Aliases: If you forget what shortcuts you have created, simply type the word alias by itself and press Enter. The terminal will print a list of every active alias on the system.

By defining a robust set of custom aliases, you can drastically reduce your keystrokes and transform the terminal into a highly personalized, efficient workspace.

Get the best tech tips delivered straight to your inbox.

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