2 years ago

#58946

test-img

Holger Pandel

Running 'bash -i' in re-invoked script (via sudo -S) exits immediately - why?

When I launch the following script, it restarts as root, the new bash shell opens up, but "something" sends an "exit" command immediately and the script runs to its end.

#!/bin/bash
#set -x
PASSWORD=<rootpass>

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    echo "$PASSWORD" | sudo -S --prompt '' /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i
    echo Bash exitcode: $?
    echo "After bash: $$"
fi

BUT when I start the script in a way, that sudo has to ask for the password interactively, everything works as expected. The bash shell opens, I can do what I like, enter "exit" myself and the rest of the script is being executed.

#!/bin/bash
#set -x

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    sudo /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i
    echo Bash exitcode: $?
    echo "After bash: $$"
fi

Can someone explain to me, why piping the password into sudo -S is behaving so differently, than a "normal" sudo call?

EDIT: If I explicitely disable the bash internal exit command, it seems to work, but I still don't understand, where the first "exit" comes from. So this solution is more of a hack than anything else.

bash --rcfile <(echo "enable -n exit; echo; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i

EDIT 2: @WilliamPursell The following works now (added < /dev/ttyto bash call), but I don't know, if it has any further implications:

#!/bin/bash
#set -x
PASSWORD=<rootpass>

echo At start: $$

if [[ $EUID -ne 0 ]]; then
    echo "No root: $$"
    echo "Before sudo: $$"
    sudo --remove-timestamp
    echo "$PASSWORD" | sudo -S --prompt '' /bin/bash "$0" -- "$@"
    echo "After sudo: $$"
    #exit 0
else
    echo "Root: $$"
fi

if [[ $EUID -eq 0 ]]; then
    echo "Before bash: $$"
    bash --rcfile <(echo "echo ; echo; echo 'Console was opened from within test.sh.'; echo In bash: $$; echo ;echo '\"exit\" to return to program.'; echo ;PS1='\[\033[01;31m\]test.sh\[\033[00m\]:\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '; pwd; cd ~; pwd;") -i </dev/tty  
    echo Bash exitcode: $?
    echo "After bash: $$"
fi

bash

sudo

subshell

0 Answers

Your Answer

Accepted video resources