10

I have a Centos 5.5 server that has my own alias commands for my account. When I do sudo bash I can still use those aliases, I didn't have to add the aliases to the root's .bashrc.

I have another server that is Centos 6.5. But when I execute sudo bash, it will not use my aliases of the user that I'm logged in as. Does anyone know where I configure it to include the aliases of the user I am logged into when I use sudo bash?

  • I think aliases are inherited via the environment, so maybe the answer here will work: unix.stackexchange.com/questions/92998/… – Barmar Jun 25 '14 at 20:57
  • The 5.5 one probably preserved $HOME from the caller, while 6.5 doesn't. See sudo sudo -V output and env_keep in the sudoers man page. – Stéphane Chazelas Jun 25 '14 at 21:38
  • 1
    Thanks Barmar and Stephane. Both your answer pointed me in the right direction. I had to set the following in sudoers: Defaults !always_set_home and Defaults !env_reset ` – Tyler Knotek Jun 26 '14 at 0:54
8

Add the following line to your ~/.bashrc:

alias sudo='sudo '

From the bash manual

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

So Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is checked by bash for an alias, ll is ignored. We can tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.

| improve this answer | |
  • Works for zsh as well just fine. Thank you! – dzhi Jan 19 '19 at 13:18
  • This just allows to use aliases in the sudo command, e.g. sudo some_alias ..., but what OP is asking is how to make aliases defined for his own user work after he switches to another user using sudo. Let's say I have some_alias defined. When I sudo to root using sudo su, a new environment is created and that alias is not defined there. There's the -E parameter of the sudo command which should export the environment but it looks like aliases are not part of it. – David Ferenczy Rogožan Oct 29 '19 at 14:00
0

Use sudo -E, which will export your environment.

| improve this answer | |
  • 3
    does not include aliases (tested on Ubuntu 16.04.1) – Antonios Hadjigeorgalis Nov 20 '16 at 16:39
  • It's working but only as sudo -E bash. When used with su as sudo -E su, the stuff defined in user's .bashrc (aliases, prompt, etc.) are not preserved, even though it looks like environment variables are preserved in both cases. I'm not sure how exactly this is working. – David Ferenczy Rogožan Oct 29 '19 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.