1

I want to write a shell script that will require sudo privilege to do certain task, but also need to change certain configuration as current user. I want to automate the script as much as possible so it does not need any user intervention.

The problem I have is that if I run the whole script as sudo, I cannot change current user's configuration. But if I just add sudo to the specific command that require root privilege, I will need to keep input the password because the script is long and sudo will time out.

Is there a way I can run the script in current user, but keep the sudo privilege when I need it?

auzn
  • 113
  • For the commands that need root, configure sudoers to allow the current user to perform them without requiring a password. – glenn jackman Jun 27 '18 at 15:00

1 Answers1

1

There are basically two ways to go:

  • separate the subtask which needs root access into a different program/script, and run that part as root (either via a setuid root binary, or via sudo)

  • run the whole thing as root, and do the part which has to be run as a normal user via su - user

Laszlo Valko
  • 1,292