# Prompt for sudo password and programmatically elevate privilege in bash script?

I'm currently working on a bash script that installs and sets up various programs on a stock Linux system (currently, Ubuntu). Because it installs programs and copies a number of files to various folders that require elevated privileges, I've already done the standard "I need elevated privileges"-and-exit.

However, I would like, if possible, to be able to prompt the user for their sudo password and elevate the script's privileges automatically if the user doesn't run the script command with sudo (such as launching it from the GUI file manager), without the user having to restart the script.

As this is designed to run on stock Linux installs, any option that modifies the system won't work for my purposes. All options need to be contained to the script itself.

Is this possible within Bash? If so, what's the best (secure, yet concise) way to do this?

• – Michael Mrozek Jan 10 '12 at 19:56
• @MichaelMrozek - Ah, the joys of having so many niche sites while trying to minimize fragmentation. And you know, the link you gave never once came up for me while searching Google. – Shauna Jan 10 '12 at 20:19
• @jww you do realize that nearly all of those links were asked about 4 years after this one, right? – Shauna Apr 6 '18 at 20:58
• @Shauna - I'm guessing you have solved the problem by now. The links are for future visitors. – user56041 Apr 6 '18 at 21:04

I run sudo directly from the script:

if [ $EUID != 0 ]; then sudo "$0" "$@" exit$?
fi


I suggest:

#!/bin/bash

if (($EUID != 0)); then if [[ -t 1 ]]; then sudo "$0" "$@" else exec 1>output_file gksu "$0 $@" fi exit fi # some example stuff ls -l /root echo "app:$0"
for f; do
echo ">$f<" done  • What does if [[ -t 1 ]]; check for? – Shauna Jan 10 '12 at 20:05 • Ah, ok. I figured it had something to do with terminal vs GUI, but wasn't sure what the if statement itself was checking. – Shauna Jan 10 '12 at 20:24 • Amazing, thanks! – Fire-Dragon-DoL Mar 15 at 18:46 Add this as the first line of the script: [ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"


Change sudo to gksu or gksudo if you prefer a graphical prompt.

• Note that "$*" is going to merge all the arguments into one (/path/to/script one two three is going to result in $1 being one two three), and $* without quotes will mess up spaces in arguments. "$@" works right – Michael Mrozek Jan 10 '12 at 19:55
• @MichaelMrozek Ah, right. That's the one I was looking for, Fixed – Kevin Jan 10 '12 at 19:57
• Is there a way to do this on Debian, where there seems to be no sudo command? – wrongusername Jul 25 '15 at 18:23
• @wrongusername install sudo, it's much better than using su. But if you really want, probably exec su -c "$0" "$@" – Kevin Jul 27 '15 at 17:23

example script i dont mind sharing::

#!/bin/bash
[ "$UID" -eq 0 ] || exec sudo "$0" "$@" && echo -n "sudo bash what: " read WHAT sudo$WHAT


--ImP.TeK

• [ "$UID" -eq 0 ] || exec sudo "$0" "$@" -- Looks like a really efficient one liner! Nice! – groovenectar Dec 17 '18 at 18:41 • Would it make sense to use Effective UID here, $EUDI, or always the same outcome? E.g., [[ "$EUID" -eq 0 ]] || exec sudo "$0" "\$@" – groovenectar Dec 17 '18 at 19:04