问题
This is what I'm trying to do in a script. It works here manually, but prompts me for a password. How do I:
- Create a new user
- With sudo privs
- Switch to that user
Continue executing the rest of the script
sudo adduser centos sudo passwd centos usermod -aG wheel centos sudo su centos
I have tried the following but in Centos 7 bash, --disabled-password and -gecos both say "option not found."
adduser --disabled-password --gecos "" username
回答1:
You don't need sudo su centos because your script would be interrupted by a terminal.
If the following commands are actually "./install.sh" (like) that have to be started by "centos" user, then you can do the following modification:
sudo adduser centos
sudo passwd centos
usermod -aG wheel centos
sudo su - centos -c ./install.sh
sudo su - centos -c ./install_another.sh
sudo su - centos -c "./install_more.sh ; cd /tmp ; ./install_almostlast.sh"
sudo su - centos -c bash -c "cd /somewhere; ./install_more.sh
cp /tmp/files /somewhere
./install_last.sh
rm /tmp/install.sh"
Between double quotes you can write a whole script if you want and are careful of the content and quoting.
来源:https://stackoverflow.com/questions/43853533/create-a-sudo-user-in-script-with-no-prompt-for-password-change-to-user-without