简体   繁体   中英

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 to sudoers using visudo :

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

And I've changed /etc/init/tty1.conf to automatically log that user in:

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

Up to this point, my whiptail_config.sh 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... message.

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

Where do you use sudo? Nowhere. Run the script directly from inittab.

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

#!/bin/bash
[ $UID != 0 ] && exec sudo $0 "$@"
# the rest of the script...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM