-1

we add the following line to bashrc file

alias reboot="echo you not allowed to do reboot on this machine - sorry"

so we get

more ~/.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


alias reboot="echo you not allowed to do reboot on this machine - sorry"

then we did

source ~/.bashrc

and indeed when we do

reboot

we got

you not allowed to do reboot on this machine - sorry

but when we want to back to real reboot , we removed the line --> alias reboot="echo you not allowed to do reboot on this machine - sorry"

as the following:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

and we refresh the bashrc

source ~/.bashrc

but we still get the old alias when we try to reboot:

 you not allowed to do reboot on this machine - sorry

what we are missing here?

another example with function

echo '
function reboot
{
echo "ERROR reboot command not allowed on this machine"
return 1
}
' >>/root/.bashrc

with example above we cant to do unalias

3
  • alias reboot="echo you not allowed to do reboot on this machine - sorry"? Is the really supposed to stop a root user from rebooting the system? unalias -a and then reboot still works, or \reboot, or /sbin/reboot or init 6 or a bunch of others. If you don't want someone to have root privileges you can't give them access to the root account. Commented Apr 6, 2022 at 9:37
  • Am I understanding this right? Do you want to disallow reboot for the root user? Would it not be better to limit the people who can become root? I mean, it looks as if you are setting up an interactive account for the root user. You might just as well set up a non-privileged account, which would automatically not be allowed to reboot, and then selectively give that account access to different commands with sudo.
    – Kusalananda
    Commented Apr 6, 2022 at 10:54
  • reboot its only example , its can be any other command , I accept your suggestion about to limit the using of command by secure or limit root
    – yael
    Commented Apr 9, 2022 at 18:15

1 Answer 1

3

Sourcing ~/.bashrc does not magically reset all settings. The file is just a bunch of commands to be executed. If they are executed in a clean shell then you will get what you expect.

But if the alias has already been defined, the lack of it in the file will not unalias it. Think about it: if you executed the commands from the file by hand, none would affect the already defined alias. There is no unalias reboot in the file. Sourcing ~/.bashrc is exactly executing the commands, only not "by hand".

Do not treat .bashrc as a file that holds current settings for the shell. It is meant to be sourced once in a clean shell, automatically. People sometimes source it again manually and it works* if they add something, because the added thing gets executed. A removed thing cannot be executed or magically reverted. Start a new shell and let .bashrc do its job in the way it's designed for.

Note it's totally easy to bypass an alias or a function. The alias or the function is not a good way to disallow rebooting.


* It works with possible side effects from executing old things for the second time.

6
  • unalias isnt relevant for example to function reboot in bashrc , so what is your suggestion if we remove function for reboot ?
    – yael
    Commented Apr 6, 2022 at 8:57
  • see the update in the Question
    – yael
    Commented Apr 6, 2022 at 8:59
  • @yael I don't get the point. A function is also not a good way to disallow rebooting. .bashrc is meant to be sourced once in a clean shell, automatically. People sometimes source it again manually and it works (with possible side effects!) if they add something, because the added thing gets executed. A removed thing cannot be executed or magically reverted. Commented Apr 6, 2022 at 9:02
  • so what is your suggestion to avoid using reboot command ? instead of bashrc
    – yael
    Commented Apr 6, 2022 at 9:06
  • 1
    @yael yes, please post a new question and clarify exactly what you need and how robust it must be. The alias you describe is trivial to circumvent (just use the full path to the reboot command or command reboot or \reboot; see How can I run original command that aliased with same name?), so we need to know more details to give you a good solution.
    – terdon
    Commented Apr 6, 2022 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .