1

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
11
  • 1
    You don't want --disabled-password. That would let anyone access that user account without a password. Commented May 8, 2017 at 17:15
  • And by the way, it's not bash that provides adduser -- you'd have the same problem with CentOS 7 running adduser from any other shell, or running it without a shell at all (ie. with subprocess.Popen(['adduser', ...], shell=False) in Python). Commented May 8, 2017 at 17:15
  • Charles I don't really care because it's a vagrant box to setup a local instance for developers. They can do WHATEVER they want with the entire box. Commented May 8, 2017 at 17:15
  • "run the rest of the script with this user", by the way, is best done by encapsulating the rest of your script in a heredoc. Commented May 8, 2017 at 17:16
  • 1
    btw, see stackoverflow.com/a/24696790/14122 re: the "change to this user for the rest of the script" part of things. Personally, I think this question is too broad in scope to be a good fit as it is -- it's asking something like three different things, each of which has already been individually asked and answered elsewhere on the site. Commented May 8, 2017 at 17:21

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.