Pass password to sudo in script -AND- 'faketme' help

I have need to run

sudo unshare -n sudo -u USER faketime ‘2025-04-20 23:00’ PROGRAM

from within a bash script. I have tried passing the password to sudo as such:

echo -e “PASSWORD\n” | sudo -S unshare -n sudo -u USER faketime ‘2025-04-20 23:00’ PROGRAM

but that isn’t passing the password. It just runs the program, ignoring the ‘unshare’ and ‘faketime’ completely.

-ALSO-

in that same scenario… if I run

sudo -S unshare -n sudo -u USER faketime ‘2025-04-20 23:00’ PROGRAM

substituting ‘bash’ for [PROGRAM], and then run ‘date’ it reports the current date as being what it was set to using faketime. BUT… if I, from within that bash prompt (or if I substitute ‘gedit’ for [PROGRAM]) and then save a text file from within gedit, the properties of that text file show that it was Created, Modified and Accessed on the ACTUAL date as reported by the OS.

Any and all help with this would be GREATLY appreciated.

I don’t know how either unshare or faketime works, so I can only help with the password passing problem. Using echo like you do doesn’t work because sudo doesn’t read the password from its standard input, but rather from the controlling terminal. Most security related programs which need to get a secret from the user work this way, in fact.

Solutions vary by the program, in the case of sudo please find the -A or --askpass option in the manpage. It looks like this might be as simple as having the one-line script

echo PASSWORD

as the askpass program should work, but I have not tested it :stuck_out_tongue:

Also, please note the security implications: you’re hardcoding the password in plaintext and putting it into a file. (This also holds of my suggestion of course.) So at the very least be sure the file can only be read by those who can be trusted with the password.

1 Like

Took me a little searching to figure out how to use that advice :wink: but…

I figured out that I needed to use the SUDO_ASKPASS environment variable to call that script:

SUDO_ASKPASS=/home/USER/.local/bin/passpass.sh sudo -A unshare -n sudo -u USER faketime ‘2025-04-23 23:00’ PROGRAM

with passpass.sh containing the echo command you suggested.

That seems to take care of the passing the password part of my problem!

Thank you so much for your answer. Now, I just need to figure out why faketime isn’t working as exected.

Maybe the BUGS section of manpage is a first hint on this.

A better way to get a program to run with elevated privileges without putting your password in a script would be to change ‘/etc/sudoers’ to allow this specific program to be run (by this specific users and with specific parameters …) without password. For details read ‘man 5 sudoers’.

2 Likes

Interesting. Most of that was greek, but I get the gist. Thank you.

I’ll check that out. Thank you.