rm -i alias not working with sudo as root
I have noticed in all my Ubuntu servers the alias rm -i
is ignored when you run sudo rm *
as root. Is there something in sudo that is causing this behavior? I know you don't need to use sudo when you are root however in the event that a Jr SA were to do so it would remove the contents of the directory. Also knowing that rm
in Ubuntu does not preserve / it could mean a total system meltdown.
Example as root:
johndoe@hostname:/tmp/foobar $ sudo su -
root@www3d:~# cd /tmp/foobar
root@hostname:/tmp/foobar# for i in 'a b c d e f g' ; do touch $i ; done
root@hostname:/tmp/foobar# sudo rm *
root@hostname:/tmp/foobar# for i in 'a b c d e f g' ; do touch $i ; done
root@hostname:/tmp/foobar# rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y
root@hostname:/tmp/foobar# for i in 'a b c' ; do touch $i ; done
root@hostname:/tmp/foobar# rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
root@hostname:/tmp/foobar# for i in 'a b c' ; do touch $i ; done
root@hostname:/tmp/foobar# sudo rm *
root@hostname:/tmp/foobar# ls
root@hostname:/tmp/foobar# exit
logout
Example as user:
johndoe@hostname:/tmp/foobar $ for i in 'a b c' ; do touch $i ; done
johndoe@hostname:/tmp/foobar $ rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
johndoe@hostname:/tmp/foobar $ for i in 'a b c' ; do touch $i ; done
johndoe@hostname:/tmp/foobar $ sudo rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
Looking at the sudo
manual, I see the following:
-i [command]
The -i (simulate initial login) option runs the shell
specified in the passwd(5) entry of the target user as a
login shell. This means that login-specific resource files
such as .profile or .login will be read by the shell. If a
command is specified, it is passed to the shell for
execution. Otherwise, an interactive shell is executed.
sudo attempts to change to that user's home directory
before running the shell. It also initializes the
environment, leaving DISPLAY and TERM unchanged, setting
HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the
contents of /etc/environment on Linux and AIX systems. All
other environment variables are removed.
That is, you don't get the environment variables from your .bashrc
file (where the alias definitions from .bash_aliases
are executed) unless you use -i
.
There is a very neat trick to solve this problem. Use the alias for sudo as follow.
alias sudo="sudo "
#Trailing space at the end.
Reason from the credit page at the end of post:
A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded
.
Example
user@user-desktop:~/test$ for i in 'a b c d e f g' ; do touch $i ; done
(reverse-i-search)`al': un^Cias -a
user@user-desktop:~/test$ alias rm="rm -i"
user@user-desktop:~/test$ rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
r m: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y
user@user-desktop:~/test$ for i in 'a b c d e f g' ; do touch $i ; done
user@user-desktop:~/test$ alias sudo='sudo '
user@user-desktop:~/test$ sudo rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y
Credits: arch wiki