How to set a sudo alias |Video upload date:  · Duration: PT1M37S  · Language: EN

Quick guide to add a sudo alias for faster command use with elevated privileges on Linux and macOS shells.

Want to run your beloved shortcuts with sudo without feeling like a password punching monkey? You can make sudo expand user aliases so typed shortcuts like ll still work when prefixed with sudo. This is an interactive shell trick that works in bash and zsh on Linux and macOS. It is great for the command line but not a replacement for careful scripting or automation.

Edit your shell config file

Open the configuration file for the shell you use. The usual suspects are:

  • ~/.bashrc for bash
  • ~/.zshrc for zsh

Use your favorite terminal editor like nano or vim. If you must, code or another GUI editor works too. Just do not edit system files at random unless you enjoy mystery breakage.

Add the sudo alias line

Insert this exact alias into the config file to allow alias expansion after the sudo token:

alias sudo='sudo '

Yes the trailing space inside the quotes is the secret sauce. That space tells the shell to expand an alias that follows the sudo token. Without the trailing space the shell treats the next word as a bare command and skips user aliases.

Reload your shell configuration

Apply the change right away by sourcing the file or starting a new terminal session. Examples:

  • source ~/.bashrc for bash
  • source ~/.zshrc for zsh
  • Or just close and reopen your terminal window

Test the alias and be cautious

Create a simple alias first to prove that expansion works. For example:

alias ll='ls -la'

Then run sudo ll. You should see your ll alias expand under sudo and then be prompted for your password as usual. If that makes you feel powerful, congratulations. If it makes you nervous, remember this is only for interactive shells.

Why you should not abuse this in scripts

Aliases are for humans typing at a terminal. In scripts and non interactive environments alias expansion is unpredictable across shells and remote sessions. For reliable automation prefer explicit commands or shell functions. Functions are portable and behave consistently under sudo when you export them or wrap the command properly.

Quick function alternative for automation

If you need a safe wrapper that behaves more predictably than aliases here is a tiny example that avoids ambiguity:

my_ls() {
  ls -la "$@"
}

You can call sudo my_ls /some/path if you export the function appropriately or put it in scripts that run in the right shell. Still read the manual and do not assume your environment is the same everywhere.

Final notes and warnings

This trick works in interactive bash and zsh sessions on Linux and macOS and is handy in the terminal for power users. It will not save you from bad habits or dangerous commands typed with sudo. Use alias expansion to save keystrokes, not to hide what commands actually do. Always prefer clear scripts or functions for automation and remote tasks.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.