Sometimes you need to add a user to your Jetstream VM, such as when a new member is joining your lab, when you need help from someone in NCGAS, or when you are running a Hackathon, as we just did. Here’s some help on adding one or multiple users at a time!
Adding users to a VM at any given time
sudo suThen, you will need to make a script file:
nano add_user.shAdd the following script (this example adds NCGAS admin access to your VM). This script is also available on github:
#!/bin/bash
#declare -a names=("ncgas_admin" "user2")
#uncomment above and add the names you want to add!
p=temporarypasswords4thewin!
for i in "${names[@]}"
do
#check if user exists
if grep -q $i /etc/passwd;
then : ;
else adduser --disabled-password --gecos "" $i ;
echo "$i:$p" | chpasswd ;
usermod -G users $i;
#### This will give sudo access as well as ssh
fi
#check if user home exists
if [ -d /home/$i ]; then : ; else mkdir -p /home/$i; fi
#force home owned by user and not root
chown $i /home/$i
#check if user home has correct permissions
if [ -r /home/$i ] && [ -w /home/$i ] && [ -x /home/$i ];
then : ;
else chmod 755 /home/$i;
fi
done
You will have to make this file executable:
chmod 755 add_user.sh
And then simply run the file – as root!
./add_user.sh
2) If you are using Ubuntu, use the above code. If you are using CentOS, change the following line:
else adduser --disabled-password --gecos "" $i ;
to
else adduser $i ;
3) If you add the people to the users group, they will likely have sudo – meaning root privileges. If you do not want this, you will have to make a separate group (e.g. “train”) and add it to the allowable ssh groups in /etc/ssh/sshd_config. However, If they are given sudo, they will be able to set their own passwords with “sudo passwd $username”, where $username is their username. Otherwise, you may have to change it for them.
Adding users to a VM at boot up
Sometimes, it is also nice to add default users to a VM when it boots. This is useful when you want to make an image for a lab, or if you are setting up an RStudio instance for a workshop and want to make default usernames, etc. Also, it’s a good introduction to writing services to launch at boot!
1) Setup the service
The first thing you will have to do is make a service file. This tells the computer the very least it needs to run what you want to be run at a given time. To do this you will need to make a new service file, in this case, we are using the add-user script above to make sure the necessary users are created at initial boot of the VM (or upon the next reboot).
sudo nano /etc/systemd/system/add-user.service
Add this to the file and save:
[Unit] Description=Add Users [Service] ExecStart=/usr/bin/add-user.sh [Install] WantedBy=multi-user.target
What this all means:
[Unit] Description=Add Users
This is the description that will be written to the system logs when the system is running the script. Make it something useful for future sane debugging!
[Service] ExecStart=/usr/bin/add-user.sh
This is the part where you point the system to the script you want to be run. In this case, we will be running the script from /usr/bin/ so that it survives imaging.
[Install] WantedBy=multi-user.target
This defines that this script will run each time linux is booted into a normal multi-user runlevel (i.e. not in safe mode).
2) make the boot script
This script will run every time the computer boots, and checks to make sure the user exists, has a home, owns said home, and can read, write, and execute in that folder.
You can just move the file you created above to /usr/bin (make sure it is named what you put in the service file!), or make a new copy of it there.
Make sure to make it executable:
sudo chmod 755 /usr/bin/add-user.sh
3) set the service to start at boot
sudo systemctl enable add-user.service