rm -i别名不能以root用户身份使用sudo

我注意到在我的所有Ubuntu服务器中,当你以root身份运行sudo rm *时,会忽略别名rm -i 。 sudo中是否存在导致此行为的内容? 我知道当你是root用户时你不需要使用sudo,但是如果Jr SA要这样做它会删除目录的内容。 还知道Ubuntu中的rm没有保留/它可能意味着整个系统崩溃。

以root为例:

 johndoe@hostname:/tmp/foobar $ sudo su - root@www3d:~# cd /tmp/foobar root@hostname:/tmp/foobar# for i in 'abcdefg' ; do touch $i ; done root@hostname:/tmp/foobar# sudo rm * root@hostname:/tmp/foobar# for i in 'abcdefg' ; 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 'abc' ; 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 'abc' ; do touch $i ; done root@hostname:/tmp/foobar# sudo rm * root@hostname:/tmp/foobar# ls root@hostname:/tmp/foobar# exit logout 

用户示例:

 johndoe@hostname:/tmp/foobar $ for i in 'abc' ; 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 'abc' ; 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 

有一个非常巧妙的技巧来解决这个问题。 使用sudo的别名如下。

 alias sudo="sudo " #Trailing space at the end. 

post末尾信用页面的原因:

当别名展开时,值的尾随空格会导致检查下一个单词的别名替换

 user@user-desktop:~/test$ for i in 'abcdefg' ; 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 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 user@user-desktop:~/test$ for i in 'abcdefg' ; 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 

致谢: arch wiki

看看sudo手册,我看到以下内容:

  -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. 

也就是说,除非使用-i否则不会从.bashrc文件(执行.bash_aliases中的别名定义)中获取环境变量。