Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass sudo\root password to a Linux command using Free Pascal

Most of the other questions about this seem to be C related. I am using Free Pascal and the Lazarus IDE, version 2.4.0 and 0.9.30 respectively.

I am creating a GUI that calls and passes arguments to a Linux command. I don't want my users to have to run the whole GUI using sudo\root, but rather be prompted for the password at the appropriate time.

I am using TProcess to call the Linux commands and pass and read values. I can't work out, though, how to pass the sudo password to the command from my own program?

To give an example, take the following normal command line:

dd if=/dev/hda1 of=~/image.dd
Enter sudo password:

My program is calling dd, passing it the if and of values from a GUI interface - it then needs to gather and pass the sudo password to dd.

like image 736
Gizmo_the_Great Avatar asked Nov 26 '25 09:11

Gizmo_the_Great


2 Answers

A little bit old, but - use pkexec in front of your command, like: pkexec ls /root

You will have each time the GUI info for entering root password. Tested on LinuxMint

like image 89
jack_ Avatar answered Dec 02 '25 02:12

jack_


On the premise that you're already using TProcess, instead of issuing the command line call directly to dd you can use one of the following established methods of invoking su/sudo:

  • gksudo (Ubuntu natty man page)
  • kdesudo (Ubuntu natty man page)
like image 34
Gustavo Carreno Avatar answered Dec 02 '25 01:12

Gustavo Carreno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!