Skip to content

Instantly share code, notes, and snippets.

@jtsagata
Last active March 7, 2017 07:58
Show Gist options
  • Select an option

  • Save jtsagata/3cebd6f0cee1dcc422b10dc8d7ea04cd to your computer and use it in GitHub Desktop.

Select an option

Save jtsagata/3cebd6f0cee1dcc422b10dc8d7ea04cd to your computer and use it in GitHub Desktop.
Checking how sudo works
#!//usr/bin/env bash
set -e
function iColor() {
echo -n "'$(tput setaf $1)"; shift; echo -n "${@}$(tput sgr0)'"
}
function print_info() {
function iColor() {
printf "'$(tput setaf $1)"; shift; printf "${@}$(tput sgr0)'"
}
GID=$(id -g)
echo -e " I'am user `iColor 2 ${USER}` with id: $(iColor 2 ${UID}), gid:`iColor 2 ${GID}` , and effective id `iColor 2 ${EUID}`"
echo -e " My home directory is: `iColor 2 ${HOME}` "
if [ "${SUDO_USER}" != "" ]
then
echo -e " Running command under `iColor 1 sudo` enviroment"
echo -e " The sudo said that user is `iColor 2 ${SUDO_USER}`, with id: `iColor 2 ${SUDO_UID}` and gid: `iColor 2 ${SUDO_GID}`"
fi
echo
}
FUNC=`declare -f print_info`
echo "Running command `iColor 3 bash -c`"
bash -c "${FUNC};print_info"
echo "Running command `iColor 3 sudo bash -c`"
sudo bash -c "${FUNC};print_info"
echo "Running command `iColor 3 sudo -H bash -c`"
sudo -H bash -c "${FUNC};print_info"
echo "Running command `iColor 3 sudo -HP bash -c`"
sudo -H -P bash -c "${FUNC};print_info"
# And finally sudo. Don't do this
echo "Password again please ... (Don't use su)"
echo "Running command `iColor 3 su -c`"
su -c "${FUNC};print_info"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment