3v-Hosting Blog

How to Set Up Bash Aliases for Linux Administration

Administration

9 min read


The day-to-day work of a system administrator rarely involves complex operations. More often than not, you have to run the same simple commands over and over again - for example, checking free disk space, viewing server load, opening a service log, navigating to the desired directory, updating packages, or restarting a service. These actions can be repeated dozens of times a day.

Of course, you can type out long commands manually each time, and at first, this is a good way to memorize them. Later, you can look them up in the terminal history when you’ve already performed a particular task. But it’s much easier to set up Bash aliases just once - these are short commands that replace long constructs and save time when working in the console. An alias literally means "alias" :)

Even just a few well-chosen aliases significantly reduce the amount of manual typing, drastically cut down on typos, and make routine actions almost automatic. Once you’ve tried working this way, you’ll never want to go back to the old method.

 

 

 

 

What Are Bash Aliases

An alias is a short name that the Bash shell substitutes for a predefined command or an entire sequence of commands.

For example, instead of the long command:

ls -lah --color=auto

you can simply type:

ll

 

To do this, you just need to create the corresponding alias:

alias ll='ls -lah --color=auto'

After that, when you enter the command ll the shell will automatically replace it with ls -lah --color=auto and execute it immediately.

But there’s one caveat. Aliases only work in the interactive Bash shell. And since they aren’t separate programs or auxiliary scripts, they place almost no additional load on the system, which is very efficient.

 

 

 

 

 

Where Aliases Are Stored. Creating and Managing Aliases.

User aliases are often located in one of three files:

  • ~/.bash_aliases
  • ~/.bashrc
  • ~/.profile

In most modern distributions, it is recommended to use ~/.bash_aliases; if this file does not exist, you can create it yourself.

Creating your own alias is very simple. To do this, open or create the file:

nano ~/.bash_aliases

Add the following line to it:

alias ll='ls -lah --color=auto'

and save the file.

 

After changing the configuration and adding a new alias, you need to reload the shell settings so that the changes take effect and you can use the new short command within your current session. This is done with the command:

source ~/.bashrc

Or you can simply open a new terminal window, where these changes will already be applied.

 

You can view all active aliases on the system using the command:

alias

 

To remove an alias before closing the terminal:

unalias alias_name

Note: This does not completely remove the alias; rather, it temporarily disables it for the current session. However, in all other sessions - including new ones - the alias will continue to work until you remove it from the file and save the changes.

 

After you’ve added the alias line to the file, saved the file, and applied the changes, simply enter the following command in the console:

ll

After that, Bash will execute the full command ls -lah --color=auto.

 

 

 

 

Why Aliases Sometimes Don’t Work

If a new alias doesn’t work, the reason is usually quite simple.

  • After changing the configuration, the command source ~/.bashrcwas not executed;
  • The terminal was opened before the changes were made, so Bash continues to use the old configuration;
  • There is a syntax error in the file or the quotes are misplaced;
  • A different shell is being used instead of Bash, such as Zsh or Fish;
  • The ~/.bash_aliases file is not included in ~/.bashrc.

Remember that you can check which aliases are correctly defined and loaded in the current session using the command:

alias

If the desired entry is not in the list, it means Bash did not read it, so you should check the configuration files and reload them if necessary.

 

 

 

 

Useful Bash Aliases for Everyday Work

Over time, every administrator develops their own set of aliases. First and foremost, this set depends on the tasks the administrator performs, but generally speaking, the purpose is to reduce the number of repetitive actions.

Aliases are most often created for working with files and the system, since these actions form the foundation of work on Unix systems. Below are examples of the most popular, basic aliases that greatly simplify an administrator’s life:

Alias Full command Purpose
ll ls -lah --color=auto Detailed file listing
la ls -A Show hidden files
grep grep --color=auto Highlight matching results
df df -h Display disk usage
du du -sh Show directory size
ports ss -tulnp View open ports
cls clear Clear the terminal

 

But it doesn’t stop there - aliases are also great for viewing logs, managing services, working with Docker and other third-party programs, and quickly navigating directories.

For example:

# Logs
alias nginxlog='tail -f /var/log/nginx/access.log'
alias syslog='journalctl -f'
# Managing services
alias nginxrestart='systemctl restart nginx'
alias nginxstatus='systemctl status nginx'
# Docker
alias dps='docker ps -a'
alias dlogs='docker logs -f'
alias dc='docker compose'
alias dcu='docker compose up -d'
# Navigation
alias www='cd /var/www'
alias logs='cd /var/log'
alias projects='cd ~/projects'

 

Essentially, if a service is used daily, it makes sense to create a separate alias for it. After a while, short commands become more natural than full ones, and working in the terminal speeds up significantly.

 

 

 

 

Aliases can combine multiple commands

Although this may seem obvious, we wanted to emphasize that an alias can run several commands in a row. This is especially convenient when the same sequence of actions is repeated every day. For example, updating packages can be reduced to a single short command:

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

 

As you’ve noticed, the built-in logical "AND" operator (&&) is used here. You can use the full range of bash command-line capabilities to write command sequences or entire scripts. For example, using the same principle, you can easily put together a small server health check:

alias health='uptime && free -h && df -h'

After running health in the terminal, information about the system’s uptime, RAM usage, and free disk space will appear immediately. This is usually sufficient for a quick check of your VPS server’s status.

 

 

 

When Aliases Are No Longer Enough

Aliases work well for short, frequently used commands. But there are limits beyond which aliases start to get in the way rather than help with your work. For example, you shouldn’t change the behavior of standard commands like `rm` unnecessarily:

alias rm='rm -i'

On your own computer, such an alias might be convenient, but when working on another server or as part of a team, it can easily cause confusion - and in the worst cases, it can even lead to catastrophic consequences for the system.

 

There’s also a technical limitation: aliases don’t handle arguments properly. Therefore, if you want to run something like:

logs nginx

, it’s better to use a Bash function:

logs() {
    journalctl -u "$1" -f
}

 

Now the same command will work with any service, such as:

logs nginx
logs ssh
logs docker

 

Aliases remain a good choice for simple shortcuts, but since functions support parameters, conditions, loops, and variables, more complex logic is usually implemented in Bash functions.

 

 

 

 

How to Maintain a Collection of Aliases

If you’re working with multiple servers at once, it’s best to use the same set of aliases. The easiest way is to simply copy the ~/.bash_aliases file to all your servers:

scp ~/.bash_aliases user@server:~/

 

Many administrators go a step further and store their configuration files (dotfiles) in a Git repository. This way, you can set up your familiar environment on a new VPS in just a few minutes.

To prevent your alias list from turning into a chaotic jumble of commands over time, we recommend following a few simple rules:

  • use short and clear names;
  • store all user aliases in a single file;
  • periodically remove commands you haven’t used in a long time;
  • don’t override standard utilities unless necessary.

After just a couple of months, this file becomes your personal library. It’s easy to transfer between servers, add new commands to, and use on virtually any Linux system.

 

 

 

 

 

FAQ

Where is the best place to store user aliases?

In most modern Linux distributions, the ~/.bash_aliasesfile is used for this purpose, which is loaded from ~/.bashrc.

 

Do aliases work when connecting via SSH?

Yes. If Bash starts after login and the user configuration is loaded, all aliases will be available just as they are in a local terminal.

 

Can aliases be used in shell scripts?

Usually not. By default, aliases are intended for the interactive shell. In scripts, it’s better to use full-fledged commands or functions.

 

How does a Bash function differ from an alias?

An alias simply replaces one string of text with another. A function is a small script that can accept arguments, use conditions, loops, and variables.

 

Which should you choose - Bash or Zsh?

If you administer Linux servers, Bash remains the most versatile choice to this day, since it’s installed virtually everywhere. Zsh, of course, offers more features for interactive work, but the principles for creating aliases in both shells are practically the same.

 

 

 

 

Conclusion

Bash aliases are one of those tools you quickly get used to. Setting them up takes just a few minutes, and they’ll save you time every day.

Every administrator gradually develops their own set of commands. Some work with logs more often, some constantly manage services, and some hardly ever leave Docker or SSH sessions. Therefore, there’s not much point in looking for some "perfect" list of aliases - it will be different for every administrator anyway.

So start with a few commands that you use most often, and after a while, you’ll notice that the list starts to grow on its own. That’s usually how a convenient working environment develops - one where the terminal does what you need it to do, rather than the other way around.

3v-Hosting Team

Author

3v-Hosting Team

The 3v-Hosting Team is made up of a dedicated group of engineers and operators who are all about building and maintaining the backbone of our services. Every day, we dive into the world of virtual and dedicated servers, handling everything from deployment and monitoring to troubleshooting real-world issues that pop up in production environments. Most of our articles stem from hands-on experience rather than just theory. We share insights on the challenges we face: performance hiccups, configuration missteps, networking intricacies, and architectural choices that impact stability and reliability. Our mission is straightforward – we want to share knowledge that empowers you to manage your projects with fewer surprises and a lot more predictability.

Using tmux for Long-Running Tasks on a VPS
Using tmux for Long-Running Tasks on a VPS

A Detailed Guide to tmux for Linux and VPS: Installation, Creating Sessions, Working with Windows and Panels, Hotkeys, and Protecting Long-Running Processes fro...

9 min