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

后端 未结 1 517
傲寒
傲寒 2021-01-23 14:01

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. Sw
相关标签:
1条回答
  • 2021-01-23 14:38

    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.

    0 讨论(0)
提交回复
热议问题