why are my ~/.bashrc aliases not honoured by sudo

Asked by Shriramana Sharma

I have already read:

https://answers.launchpad.net/ubuntu/+question/25401
https://answers.launchpad.net/ubuntu/+question/25551

but they don't seem to address my exact problem, and I don't grok this login shell / interactive shell business completely so I'll go ahead and ask. In both my /home/samjnaa/.bashrc and /root/.bashrc I have the alias rm = 'rm -i' to prevent me from deleting stuff accidentally. (I can always use -f to override the -i if I'm really sure.) But I find that though both the user and root shells honour this alias, it is not honoured by sudo. See:

[samjnaa:~] cd /tmp
[samjnaa:/tmp] touch foo
[samjnaa:/tmp] rm foo
rm: remove regular empty file `foo'? n
[samjnaa:/tmp] sudo -i
[sudo] password for samjnaa:
[root:~] rm /tmp/foo
rm: remove regular empty file `/tmp/foo'? n
[root:~] exit
logout
[samjnaa:/tmp] sudo rm /tmp/foo
[samjnaa:/tmp]

Also with a sub-shell:

[samjnaa:/tmp] touch foo
[samjnaa:/tmp] sudo bash -c 'rm foo'
[samjnaa:/tmp]

I don't get it. IIRC sudo runs the command with the process's uid/gid=0. To execute the command, bash is needed anyhow. So whether bash reads the user's .bashrc or root's .bashrc, it must see and obey this alias. Why does this not happen? Does bash not see the rm command at all?

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu sudo Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Shriramana Sharma (jamadagni) said :
#1

I suppose another pertinent question to ask would be:

what can I do to get my aliases honoured even while using sudo?

Revision history for this message
Lasse Luttermann (lasse-luttermann) said :
#2

you properly just need to set the same aliases for root, seeing as it is root you execute the command as. This is guesswork thoug...

Revision history for this message
Shriramana Sharma (jamadagni) said :
#3

I already mentioned in my first post that both ~/.bashrc and /root/.bashrc have the aliases, and this also visible in the session copy-paste -- see sudo -i and rm foo causing the verification prompt.

Revision history for this message
Lasse Luttermann (lasse-luttermann) said :
#4

Sorry i didnt see that (was a bit tired when answering).

Then it might be due to sudo not using bash at all, or that bash isnt invoked as a interacive shell and therefor doesnt read .bash* files...

You will properly have more luck with your question at http://www.gratisoft.us/sudo/lists.html

Revision history for this message
Juhos Csaba-Zsolt (csaba-juhos) said :
#5

Hi Shirama,

Put
alias sudo=sudo$'\t'
in your /home/samjnaa/.bashrc

From bash.info 6.6 Aliases: "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."

Also,
bash -c 'rm foo'
doesn't work as you expect, because alias expansion is enabled by default only for interactive shells.
So
bash -ic 'rm foo'
should work. And in fact it does.

Csabi

Revision history for this message
Juhos Csaba-Zsolt (csaba-juhos) said :
#6

P.S.: Use cases like "sudo -s rm foo" and "sudo -i rm foo" still don't work because of the intervening options.

Csabi

Revision history for this message
Juhos Csaba-Zsolt (csaba-juhos) said :
#7

Also,
bash -c 'shopt -s expand_aliases ; rm foo'
doesn't work either.

From bash.info 6.6 Aliases:
"The rules concerning the definition and use of aliases are somewhat
confusing. Bash always reads at least one complete line of input
before executing any of the commands on that line. Aliases are
expanded when a command is read, not when it is executed. Therefore, an
alias definition appearing on the same line as another command does not
take effect until the next line of input is read."

Can you help with this problem?

Provide an answer of your own, or ask Shriramana Sharma for more information if necessary.

To post a message you must log in.