Create a sudo user in script with no prompt for password, change to user without interrupting script

孤者浪人 提交于 2019-12-20 04:23:58

问题


This is what I'm trying to do in a script. It works here manually, but prompts me for a password. How do I:

  1. Create a new user
  2. With sudo privs
  3. Switch to that user
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!