rm -i alias не работает с sudo как root

Я заметил, что во всех моих серверах Ubuntu псевдоним rm -i игнорируется при запуске sudo rm * с правами root. Есть ли что-то в судо, которое вызывает такое поведение? Я знаю, что вам не нужно использовать sudo, когда вы root, но в случае, если 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
2
задан 3 December 2011 в 23: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

Кредиты: arch wiki

2
ответ дан 25 May 2018 в 16:19
  • 1
    Спасибо, псевдоним для sudo сделал это. Я положил его в .bash_aliases. – user36225 2 December 2011 в 02:04
  • 2
    Отлично, нам нужны оба "alias sudo = 'sudo' " и "alias rm = 'rm -i'" – diyism 16 January 2015 в 12:25

Глядя на руководство 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 файла (где определения псевдонима из [ f4]), если вы не используете -i.

3
ответ дан 25 May 2018 в 16:19

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

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