last day (17 days later) » 

13:55
2
A: How can I run su root and enter credentials in a script?

Parsa MousaviYou have to add an entry in the /etc/sudoers file to be able to run just your script as root without using any password : username ALL = (root) NOPASSWD: /your/script's/absolute/address You have to replace the username with your own user name. But it's dangerous in the sense that if a non-pri...

I'll have a look.
It's not enough to have the file read-only for non-root users. All parent directories must be writable by root only. sudoers can be configured with the digest of the file but that leaves a race condition.
@HaukeLaging What do you mean by "digest of the file " ? What kind of race condition ?
Also, chmod alone is necessary but not sufficient. The OP also needs to sudo chown root your_script
@JimL. Thanks for pointing out. Updated.
13:55
No - didn't work I'm afraid, still requests password from the script. - if anybody has an answer to this question (in the link here) then this would solve my problem too. LINK CLICK HERE FOR QUESTION
@JimL. Please see my comment above
@ParsaMousavi Please see my comment above
@HaukeLaging Please see my comment above
@ORICKETTS The syntax is really strict . Are you sure you typed it correctly ? That works for me . I use it everyday.
@ParsaMousavi Do I replace USERNAME with the root username or the username that will run it? And the (root) in brackets, does that need to be there or replaced with anything? Thanks
@ORICKETTS Oh sorry , my bad. You have to replace USERNAME with your username and leave the (root) as it is.
@ParsaMousavi Thanks - I'll have a go tomorrow and let you know the verdict. Please note, the current time for me is 19:22 BST, ENGLAND
You get the SHA256 digest of a script e.g. with sha256sum ./script.sh. See man sudoers, search for sha256. sudo calculates the digest and in case of a match runs the command. The race condition is that after reading the file, the file can be changed in the (very) short time until it is opened by the interpreter. If the file itself is writable it can be effectively changed even after the interpreter has started executing it.
13:55
@HaukeLaging Thanks for explanation . Didn't know that.
@HaukeLaging Please see my updated question above, I have added some context below my original question. Also, please could you click the 'FOLLOW' button on this answer to keep updated so I don't have to notify you with @
@ParsaMousavi Please see my updated question above, I have added some context below my original question. Also, please could you click the 'FOLLOW' button on this answer to keep updated so I don't have to notify you with @
@JimL. Please see my updated question above, I have added some context below my original question. Also, please could you click the 'FOLLOW' button on this answer to keep updated so I don't have to notify you with @
@ParsaMousavi Also - it still requests the password.
@ORICKETTS Here's mine that works everytime : "spm ALL=(ALL) NOPASSWD: /usr/sbin/pm-suspend" . ALL=(ALL) could be ALL=(root) but that doesn't make a difference here , because that only root can run this , not any other user.
@ParsaMousavi and 'spm' is your username?
@ORICKETTS Yes.
@ParsaMousavi I've updated the question - I'll have another go at it to check - If there isn't a straightforward answer to this question, an answer to 'Allow any standard user (non sudoer) to mount any filesystem including network drives and windows shares'. However, when I followed something allowing me to do that, I got the error: only root can use --options option Thanks
@ParsaMousavi If your answer doesn't work - do you think this LINK would come in handy? CLICK HERE FOR LINK
13:55
@ORICKETTS Yes . That should work. Setuid bit is what the "sudo" program actually have to be able to run a command as another user.
@ParsaMousavi Is there any chance if you understand that link that you could break it down into commands that I should use because I'm very new to this and don't really understand that.
@ParsaMousavi Also - in the script, if I have carried out your answer, does the script need su root at the top or not?
@ORICKETTS No . The script now is running as root so there's no need to change the current user to root again.
@ParsaMousavi Thanks - I'll have another go, if it doesn't work, is there any chance you could simplify the answer on the link for me?
@ORICKETTS This answer doesn't work in newer linux systems. As stated in the post "this solution does NOT generally work on any modern system with any shebang script other than Perl < 5.12.0". I tried it and it doesn't work . So that cannot be the solution.
@ParsaMousavi I'm running Linux CentOS7 to the latest update for that version of operating system. I'm having a go now, doing a reboot and seeing if it works.
13:55
@ORICKETTS Have you logged out and logged back after modifying the "sudoers" file ?
@ParsaMousavi I've just rebooted the entire system with sudo reboot
@ParsaMousavi Just checking the outcome now
@ParsaMousavi You have saved my day! It's worked. Absolutely amazing. Simply incredible Thank you so much
@ParsaMousavi I've also set it to run on startup with mate-session-properties and config with that and it's mounted on startup. Amazing
@ORICKETTS You're welcome. I think the problem was that you hadn't rebooted your system ( or at least re-login ) to let the changes take effect.
@ParsaMousavi Maybe - I did make a few changes to the format of it in /etc/sudoers, and felt like I had been rebooting quite a bit though. Thanks
@ParsaMousavi please could you look at this link: CLICK HERE
14:16
@ParsaMousavi Discussion ended.
14:51
@ORICKETTS please don't continue discussing in the comments. This room will remain linked to the answer. If anything is particularly interesting, then it should be edited into the answer, that's how these sites work. We don't do discussion and comments are ephemeral by nature.

  last day (17 days later) »