1

So I am in this situation where we have started adding a bunch of new servers because we are getting new clients, therefore, we need new servers for each new client we get, and with that follows the need to create users on those servers with privileges and all that stuff. So what I basically need to do on each new server is the following steps:

  1. sudo adduser John
  2. sudo usermod -aG sudo John
  3. sudo su John
  4. cd /home/John
  5. mkdir .ssh
  6. chmod 700 .ssh/
  7. touch .ssh/authorized_keys
  8. chmod 600 .ssh/authorized_keys

This is very tedious when you have to create 4-5 users on each server. What I am working on right now is to create a script that does all these steps for me with a simple command as

./create_users.sh John Jim Jill Peter

So what I have so far is:

for i in "$@"; do
    echo creating user: "$i"
    sudo adduser $i

    echo add user to sudo group
    sudo usermod -aG sudo $i

    echo creating ssh folder and authorized_keys
    sudo su $i
    cd /home/$i
    mkdir .ssh
    chmod 700 .ssh/
    touch .ssh/authorized_keys
    chmod 600 .ssh/authorized_keys
done

I have not tried it out yet but I know that it will not work since I have to pass on a password for each user. So my question is how can I do that? For now, it is okay to just set some kind of a standard password like 1Password because they need to change their password anyway.

Is it even possible to do it with a scripts and/or is it not recommend to do this.

EDIT 1

Ahh okay I am actually prompted with setting the password for each user if I run this, seem to work "perfectly fine" the only thing right now that I have trouble with is that I get to the step where I cd /home/$i which somehow seems not to work I get the following error:

mkdir: cannot create directory ‘.ssh’: Permission denied 
chmod: cannot access '.ssh/': No such file or directory
touch: cannot touch '.ssh/authorized_keys': No such file or directory
chmod: cannot access '.ssh/authorized_keys': No such file or directory

Which makes sense since I am not in the correct directory. So I do not get why my cd /home/$i is not working...?

1

Sorry, I figured out a solution a few days ago, and here is my solution:

for i in "$@"; do
    echo creating user: "$i"
    sudo adduser $i

    echo add user to sudo group
    sudo usermod -aG sudo $i

    echo creating ssh folder and authorized_keys
    sudo -H -u $i bash -c 'mkdir -p /home/"$0"/.ssh' $i
    sudo -H -u $i bash -c 'chmod 700 /home/"$0"/.ssh/' $i
    sudo -H -u $i bash -c 'touch /home/"$0"/.ssh/authorized_keys' $i
    sudo -H -u $i bash -c 'chmod 600 /home/"$0"/.ssh/authorized_keys' $i
done

This requires me, of course, to create a new script on each server, but is still way faster than having to run all those commands manually for 5 users. So what I do on each server is:

  1. touch create_users.sh
  2. chmod +x create_users.sh
  3. vi create_users.sh and past in the "script code"
  4. Lastly I run ./create_users.sh john joe jill

I am then prompted for each user to enter a password, I then just create a simple password for each user and let them change it asap. This works for me and is way faster than running adduser --> usermod --> mkdir etc. manually for each user. So for now I stick with this.

Feel free to post your solutions, if any, I would like to see other approaches, especially because I am pretty new to scripting and working with ubuntu servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.