комната-i псевдоним, не работающий с sudo как корень

Я заметил во всех своих серверах Ubuntu псевдоним rm -i проигнорирован, когда Вы работаете sudo rm * как корень. Есть ли что-то в sudo, который вызывает это поведение? Я знаю, что Вы не должны использовать sudo, когда Вы - корень однако, если Младший SA должен был сделать так, он удалит содержание каталога. Также знание этого rm в Ubuntu не сохраняет / это могло означать общий системный крах.

Пример как корень:

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
2
задан 3 December 2011 в 21:07

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

Кредиты: дуга Wiki

2
ответ дан 2 December 2019 в 02:08

Взгляд на 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.

3
ответ дан 2 December 2019 в 02:08

Другие вопросы по тегам:

Похожие вопросы: