0

We are creating this nodejs script for Debian version 9 or 10, to be specific, but it needs to run as sudo or a user that has same privileges as root. Below is sample code:

exec('iptables -S',
    function (err, stdout, stderr) {
        resolve(stdout)
        if (err !== null) {
          reject(err.toString('utf8'));
        }
    })

So our question is do all debian based linux distributions use sudo? If yes, case closed. But if the answer is no, what are other alternatives so this command can be used and run by other user group or any other user that will always be present in a Debian distribution and doesn't use sudo?

2
  • 3
    What exactly do you need? I mean, how will you be running this? Do you need to use sudo or is it enough to test if the current user is root (when running with sudo, you are root). How would you use sudo? And is this only for Debian? If so, why do you mention derivatives? Does that include things like Mint or Ubuntu?
    – terdon
    Commented Oct 7, 2020 at 11:03
  • 3
    Every system, Debian based or not, will have a different setup as to which user is allowed to do what by which means. But most distros should be able to install sudo if it is not present. And personally, I'd never trust any random node script to run anything at root. So I wouldn't touch your script with a long stick unless you make it very transparent to me what you are doing, and allow me to do the critical parts myself. The last thing I want is to let a random script mess with my system. So, redefine the way you want to solve your problem. And define the original problem.
    – dirkt
    Commented Oct 7, 2020 at 11:08

2 Answers 2

1

The most common way I've seen things like this handled, is that the program will simply check if it is being run by a user with an effective UID of 0, which is the user id of the root user. For example, if I were writing a shell script that can only be run as root, I would do something like this:

#!/bin/sh
uid=$(id -u)
if [ $uid != 0 ]; then
    echo "This must be run as root!"
    exit
else
    echo "You are root, proceed!"
fi

(OK, actually, if I were writing a shell script, I would just use the $UID variable, but presumably that won't work for you in node.js or may be complicated, so I'm using id -u instead.)

If we now run the script in different ways, we see:

$ foo.sh
This must be run as root!

$ sudo foo.sh
You are root, proceed!

$ sudo -i ## open a root shell
# ~terdon/scripts/foo.sh
You are root, proceed!

In other words, forget about sudo (and no, you can't be 100% sure it will always be installed on Debian systems, see Why is sudo not installed by default in Debian?) and simply check the user ID and exit if it isn't root. Write an error message informing the user that this needs to be run as root and there you go. If you need this to be automated, you cannot use sudo anyway since you would need to manually enter a password.

0

Short answer: Yes, they all can use sudo, but you can't assume that it's installed.

Longer answer: Any distribution -- not just Debian -- can use sudo. sudo is the preferred way to perform actions as root (and also as other users) on the system. From experience, sudo is usually not installed by default on Debian installations, unless it's been pulled in by some other package which depends on it. Some distributions that inherit from Debian (e.g. Ubuntu) come with sudo preinstalled. Your application should not run as root and using sudo to run commands like iptables is a good plan. I'd recommend checking for the availability of sudo in your application initialisation phase and error out if it's not available.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .