rm -i 别名无法以 root 身份使用 sudo

rm -i 别名无法以 root 身份使用 sudo

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

以 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

用户示例:

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

答案1

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

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

答案2

有一个巧妙的技巧可以解决这个问题。使用 sudo 的别名,如下所示。

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

文章末尾信用页中的原因:

值中的尾随空格会导致在扩展别名时检查下一个单词是否进行别名替换

例子

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

致谢:拱门维基

相关内容