0

I have two files, one being a desktop file, the other being a shell script. The desktop file is meant to open the shell script, and execute it in a xTerm GUI as root, without needing "sudo password."

I was told to remove sudo completely from the shell script, leaving this. Since the program needed root access, I edited my /etc/sudoers to the following:

david ALL=(ALL) NOPASSWD:/home/david/upgrade.sh
david ALL=(ALL) NOPASSWD:/home/david/Desktop/Upgrade.desktop

As I was instructed to, and from there, configured the desktop file. I started with the following:

xterm -e sudo sh -p /home/david/upgrade.sh

But when I ran the desktop file it still asked for my password. I thought that the sudo in the desktop file could be the problem, but upon removing it, the xTerm window no longer opens. How can I execute this shell script from the desktop file without entering my password?

1
  • Ok a couple of things...<br/> – GrannySez Oct 3 '16 at 22:42
1

First, remove your line about the desktop file from /etc/sudoers, it's not needed.

Second, you configured the exact command /home/david/upgrade.sh to run with NOPASSWD, not sh -p /home/david/upgrade.sh which you try to run in the xterm window. You must run the configured command to profit from the NOPASSWD setting:

xterm -e sudo /home/david/upgrade.sh

By the way, the shebang of your script file specifies /bin/bash as interpreter, but you attempted to run it with /bin/sh. The outcome is the same as long as you don't use Bash specific features, but you should avoid such contradictions. This has nothing to do with sudo and NOPASSWD though.


As additional candy, it might be nice if one can call your script directly without sudo and it elevates its privileges automatically if not already running as root. I've written a little Bash snippet for this once, it can simply be added to the top of any Bash script that needs to run as root:

#!/bin/bash
if test "$(id -u)" -ne 0 ; then
    sudo "$0" "$1"
    exit $?
fi

This tests if the effective user id is 0 (root) and if not, it executes its own script file with sudo and also passes its exit code through when terminating. This works well together with a NOPASSWD setting for the script file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.