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:
sudo adduser John
sudo usermod -aG sudo John
sudo su John
cd /home/John
mkdir .ssh
chmod 700 .ssh/
touch .ssh/authorized_keys
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...?
sudo ./my_script.sh
– rhubarbdog Dec 4 '18 at 4:02