0

I have a bash script that installs a lot of software. The whole process takes around 1h therefore I would like to increase the sudo timeout variable. I see it is possible as described here: Increase duration of Sudo.

However, I cannot find a description for the command line anywhere...
In my case I would like to make this change from within the script. I could put something like: sudo echo 'Defaults timestamp_timeout=300' >> /etc/sudoers at the first line within the bash script. This, however, is not allowed.

maciek
  • 165
  • 1
  • 7
  • It's usually simpler to remove any `sudo`s from *inside* the script, and run the whole script with `sudo` – steeldriver Apr 25 '20 at 11:30
  • BTW here's why your attempt to modify the sudoers file failed: [How to solve “permission denied” when using sudo with redirection in Bash?](https://askubuntu.com/questions/230476/how-to-solve-permission-denied-when-using-sudo-with-redirection-in-bash) (I don't recommend doing that though) – steeldriver Apr 25 '20 at 11:35
  • @steeldriver : I cannot run the whole script as sudo, it contains commands which specifically cannot be run with root privileges – maciek Apr 25 '20 at 11:39
  • So how about dropping privileges for those specific commands (using `sudo -u` for example)? – steeldriver Apr 25 '20 at 11:48
  • This would require the users to be known to the bash from inside the script? This is not the case... – maciek Apr 25 '20 at 11:54
  • ... `$SUDO_USER` doesn't work for some reason? – steeldriver Apr 25 '20 at 11:57
  • @steeldriver: I just tested that, it works, it correctly returns my name from inside the script. But now I have a problem that when the script calls `sudo -u $SUDO_USER cp DIR/file file` (being inside `$HOME`) . I get `cp failed to access 'file': Permission denied`. Is it possible to operate on `$HOME` as non-root while the script was called with `sudo`? – maciek Apr 26 '20 at 20:02
  • OK, I have solved it, it was because `$HOME` points to a different place whether calling the script with/without `sudo`. Thank you for all the help! – maciek Apr 27 '20 at 10:48

1 Answers1

0

I will post an answer to my problem in case anyone else runs into such issue in the future.
Thanks for the help @steeldriver.

Indeed, it is better to use sudo on the whole script and then escape commands within which specifically need to be run as the user with a prefix: sudo -u $SUDO_USER. Importantly, when the script is run with root privileges the $HOME changes, so be careful. In order to refer to the original user's home create a variable:

USER_HOME=$(sudo -u $SUDO_USER -H -s eval 'echo $HOME')

and then operate on paths with the prefix of $USER_HOME, if required.

maciek
  • 165
  • 1
  • 7