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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I run sudo directly from the script:

if [ $EUID != 0 ]; then
    sudo "$0" "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="644024">[email protected]</a>"
    exit $?
fi

Method 2

Add this as the first line of the script:

[ "$UID" -eq 0 ] || exec sudo bash "$0" "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96b2d6">[email protected]</a>"

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

Method 3

I suggest:

#!/bin/bash

if (($EUID != 0)); then
  if [[ -t 1 ]]; then
    sudo "$0" "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e0a6e">[email protected]</a>"
  else
    exec 1>output_file
    gksu "$0 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="351175">[email protected]</a>"
  fi
  exit
fi

# some example stuff
ls -l /root
echo "app: $0"
for f; do
  echo ">$f<"
done

Method 4

An example script that I don’t mind sharing::

#!/bin/bash
[ "$UID" -eq 0 ] || exec sudo "$0" "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="163256">[email protected]</a>" && echo -n "sudo bash what: "
read WHAT
sudo $WHAT

Method 5

I use visudo to edit the sudoers file as follows:

  • In the line with your user, add this next to the username:
    ALL=(ALL)       NOPASSWD: ALL

    then save.

  • When that user
    sudo su -

    he will be logged on as root without entering a password.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x