I have a c++ code and I need to running from it a command to adjust the system time. so I thought using system("su root -c date hh:mm") command from my c++ code.
The problem is that when I write 'su root -c date hh:mm' in the terminal its requires password after, and how can I pass the password the the system command in one line? or another solution... Thanks!
Best How To :
A workaround is to modify the
sudoers file and remove the requirement of a password from your user ID for a particular script to have sudo privileges.
After this, add the details in the following manner.
username ALL=(ALL) NOPASSWD: /path/to/script
Another method would be to pipe the password as input to the sudo command. I don't recommend this, as you would be entering your password as plain text. You could use a variable to store the password, and then access it, but it still isn't a secure method. You could run the command as
echo $PASSWORD | sudo ./a.out