Godzilla74 Godzilla74 - 1 year ago 127
Linux Question

Run bash whiptail script after login with sudo

I'm creating a virtual machine configuration script using whiptail that I'd like to have automatically start after the user logs in. The thing is, that I don't want the user to be root, however because the user will need to be able to change things like hostname, ip address, add directories, etc. they'll need sudo privileges in order for the script to actually do it's job.

What I've done so far is created a user that uses my whiptail shell script:

adduser -M -s /scripts/whiptail_config.sh user1

I've also added

user1 ALL=(root) NOPASSWD: /scripts/whiptail3_config.sh

And I've changed
to automatically log that user in:

exec /sbin/getty -8 38400 tty1 -a user1

Up to this point, my
shell script loads fine after bootup and the forced login. However, anything that actually requires the sudo access within the script errors out with the
Permission denied...

Is what I'm looking to do possible? Are there alternatives that I haven't considered and should? Thanks!

Answer Source

If /scripts/whiptail3_config.sh doesn't do a sudo, then it won't have permissions. You can make the script check the uid under which it's running and sudo to itself.

Something like

[ $UID != 0 ] && exec sudo $0 "$@"
# the rest of the script...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download