0

I am using the below command to delete the file:

$ echo <Password> | sudo -S -u <User> rm -f <Filename>
rm: cannot remove ‘<Filename>’: Permission denied

Please let me know where I am going wrong

Kusalananda
  • 333,661

2 Answers2

1

Don't do it this way.

You can write a script, then configure sudo (/etc/sudoers), to allow your user to sudo it without a password.

You could also write a go program, and then use suid.

0

Don't do this, or any version of what you are trying to do, but you can use yes:

$ yes 'password' | sudo -Su USER rm -f FILE

What you should do instead is allow USER passwordless sudo access by modifying /etc/sudoers (use visudo where available), and adding that user to the required group.

How to run a specific program as root without a password prompt?

jesse_b
  • 37,005
  • Don't EVER do this - anyone who runs ps at the right time will see your password. – Andrew Henle Jan 11 '20 at 22:47
  • Yeah, I don't know if the "EVER" is necessary though. I can think of some cases where that wouldn't be an issue. For example on a testing machine with no network connectivity where your password is something like "password". Even on my personal laptop if someone is logged in and has the ability to run ps I already have bigger issues to worry about than them getting my password. – jesse_b Jan 11 '20 at 22:53