0

I have a c++ qt program (lets call it test_program to make it easier to refer) which runs sudo lsblk using popen and prints the output.

When I run the test_program from within the qt application (the qt application itself is started using sudo), I can see the output printed.

But when I start the test_program from the binaries created using the qt application like sudo test_program the output of sudo lsblk prints

sudo: account validation failure, is your account locked?\nsudo: a password is required

I tried adding my current user to the sudoers file. Neither did that help.

user ALL=(ALL) NOPASSWD:/usr/bin/lsblk,/home/user/test_program

A program started with sudo should be able to run any commands with sudo right? I tested this with a c++ program where I start that program with sudo and it runs sudo lsblk and does not have any issue.

Here's what my sudoers file looks like: enter image description here

1
  • This looks like an AB problem. There is probably a better way to do this. You probably don't need to give your program root privileges. And in any case sudo is probably not the best way to do it. Consider: file-permissions, suid/sgid, capabilities, etc. Apr 5 at 11:03

1 Answer 1

2

As I understand it,

  1. you have sudo lsblk called from within your QT program
  2. you start your QT program with something like sudo qt_program

If so, your program is already running with root privileges and you don't then need to use sudo to run lsblk as root. Alternatively, just start your QT program normally and continue to use sudo to run lsblk as root. You really shouldn't need both.

Actually, you don't need sudo to run lsblk anyway: the information it provides is already available for unprivileged users.

The reason for the error may be that sudo is configured to require root to enter a password (unusual) or that the absence of a TTY from which to attempt to read a password is causing a hard failure. Without seeing the full sudoers configuration I don't think it's possible to be definitive.

8
  • Yes, using sudo once is good to gain root privileges and run lsblk, but at this point I'm just more curious why using "sudo lsblk" gives an error when I run "sudo test_program" but when I run test_program is run from the Qt IDE(note Qt IDE is started with sudo) , "sudo lsblk" executes fine.
    – yash
    Apr 5 at 8:16
  • 2
    Better solution would be to stop using sudo. For example, you don't need it for lsblk Apr 5 at 8:37
  • 2
    Adding to this @yash you may want to start looking into e.g. polkit libraries to perform authentication, if you need it, instead of using sudo (i believe that's how, e.g., gparted does that), and maybe escalate privileges only when needed.
    – kos
    Apr 5 at 8:48
  • @kos, I'll look into it. Thank you.
    – yash
    Apr 8 at 11:00
  • @ChrisDavies, lsblk does not require sudo. I have removed it. But do you have any idea why it would not work?
    – yash
    Apr 10 at 4:45

You must log in to answer this question.

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