I'm writing a shell script where most commands don't require root privileges. The script should be ran by the administrator. But I would like to be able to "su" to a normal user account for the parts of the scripts that don't require root privileges. This would minimize the number of operations done with root privileges and would improve security.
Is there a linux command to do that?
I know it is possible to su to any user account but I don't want to rely on the hypothesis that a particular username exists on the machine.
I thought of creating a temporary account for the time of the script and delete it at the end of the script. But if I don't set a password on this account, wouldn't an attacker be able to use it during the short lifetime of the account? I can't set the user shell to /sbin/nologin because apparently this prevents
executing commands in a shell script "suing" to the account.
Thanks for your help.