Operations 3 min read

Why sudo Ignores Your Aliases and How to Fix It

This article explains why the sudo command bypasses user-defined aliases in Bash, demonstrates the resulting error with common shortcuts like ll, and provides a simple solution using a trailing‑space alias so that sudo respects alias expansion.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why sudo Ignores Your Aliases and How to Fix It

Generally, sudo ignores aliases defined in .bashrc, .bash_aliases, or via the alias command.

For example, alias ll='ls -lh' works normally, but sudo ll returns “sudo: ll: command not found”.

Solution

Create an alias for sudo that ends with a space, for example: alias sudo='sudo ' The trailing space tells bash to check the next command word for alias expansion, as described in the bash manual:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

Example .bash_aliases content:

# Shortcuts
alias ll='ls -lh'
alias la='ls -lhA'
alias l='ls'
alias c='clear'
alias x='exit'
alias q='exit'

# Don't run shutdown if rtorrent is running... (as long as there's a screen with "tor" in its name, shutdown won't run unless you call /sbin/shutdown, or unalias it)
alias shutdown='/home/james/scripts/safe.shutdown.sh'

# When using sudo, use alias expansion (otherwise sudo ignores your aliases)
alias sudo='sudo '
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

command-lineBashAliasSudo
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.