Saturday, 28 December 2019

Prevent Some Commands from Running in Bash

Bash loads these files when start and exit:
  • All users:
    • Start up file: /etc/profile (TTY only)
    • Start up file: /etc/bashrc (TTY & PTY, CentOS)
    • Start up file: /etc/bash.bashrc (TTY & PTY, Ubuntu)
    • Exit file:     /etc/bash.bash_logout (TTY only)
  • Specific user:
    • Start up file: ~/.bash_profile (TTY only)
    • Start up file: ~/.bashrc (TTY & PTY, All Linuxes)
    • Exit file:     ~/.bash_logout (TTY only)
To prevent some commands from running, for example "sudo rm", add a function similar to this to the a start up  file:

function sudo {
  if [[ $1 == "rm" ]]; then
    echo "Command not allowed: sudo rm";
  else
    #the doublequotes are important to run
    #commands with quotes/doublequotes
    command sudo "$@";
  fi
}

No comments:

Post a Comment