0

So I created a script that live in /usr/local/bin called Linux-Permissions. In the script, there are some sudo cp commands. I need a user to be able to run the command as sudo, which I was able to do by adding this into the sudoers file: username ALL=(root) /usr/local/bin/Linux-Permissions. However, after he puts in his password to run that command, it asks for his password again to run the sudo cp inside the script. Since he doesn't have permission to run cp as sudo, this fails. The sudo commands inside the script are within a selection menu if that matters at all.

2
  • 3
    if you run the script with sudo everything inside also runs as "sudo". – bac0n Jun 4 at 16:58
  • I removed sudo from everything inside and it works great! Not sure how to set your comment as the best answer. – TL_Arwen Jun 7 at 17:49
0

So, you have successfully identified the problem; adding an entry like that to sudoers only gives the ability to run /usr/local/bin/Linux-Permissions with sudo, so all of those other sudo commands will fail since, even though the script is being run with root privileges, sudo still sees that it's being executed by a normal user.

There are probably several different ways to approach this problem, but I would modify the script so that it only uses the sudo command if it is running as a non-root user. Here's a very simple example of a script like that:

#!/usr/bin/env bash
  
if [ "$USER" != "root" ]; then
  SUDO_CMD="sudo "
fi

$SUDO_CMD ls

In that example, the ls command will be run with sudo if the script is being run as a normal user, but it will not use sudo if the $USER environment variable is set to root, which will be the case if you run the script with sudo.

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.