File: ubuntu_linux_user_setup.txt Last updated: Saturday 31st October 2015, 14:59 PT This document is based on: http://www.annedawson.net/Ubuntu_Server_User_Management.txt (see summary at bottom of) and http://www.annedawson.net/165_server_user_ssh.txt Note: I had already set up 30 student users for my CSCI165 class. This document sets up 60 more users for the CSCI100 classes. 1. Add the group student ------------------------- # groupadd student (if not already done) 2. Create a text file called usernames.txt that contains the required usernames. e.g student31 ... student90 --------------------------------------------------------- (This file will be used later to set passwords) Python code to do this: file1 = open("usernames.txt","w") name = "student" for i in range(31,91): file1.write(name + str(i) + "\n") file1.close() FileZilla to ad4.ca and copy usernames.txt /home/anne Putty to ad4.ca sudo -s mv usernames.txt /var/www/html 3. Make the directories student31 - student90 in /var/www/html --------------------------------------------------------------- See: http://unix.stackexchange.com/questions/48750/creating-numerous-directories-using-mkdir Shows this suggestion: mkdir s{1..50} (where s is the string you want to use as the start of the username) Putty to ad4.ca $ sudo -s # cd /var/www/html # mkdir student{31..90} # ls (to see the new directories) 4. Add the new users: student31 to student90 --------------------------------------------- From: https://www.howtoforge.com/user_password_creating_with_a_bash_script Create a file called addusers.bat in /var/www/html (Note: this batch file refers to the text file created in 2. above) # cd /var/www/html # vi addusers.bat #!/bin/sh for i in `more usernames.txt ` do echo $i adduser $i done IMPORTANT: make sure you use the backquote ` and not a single quote ' Backquote is on the tilde key (~) on US keyboards - below the Esc key. Make the file executable: # chmod 755 addusers.bat Execute the file taking care to use the full path: # /var/www/html/addusers.bat This will add all the users to the system. It will prompt for passwords. Enter blank defaults for firstname, lastname etc and 'y' for password. Passwords are changed later - see next section. To list all users you can use: cut -d: -f1 /etc/passwd 5. Change all passwords ----------------------- Now we have to change the passwords. Let's say we want usernameabc as the password. So for user student31 the password will be student31abc, student32abc for user student32 and so on. I tried the script in here to change all passwords but it didn't work (for passwd or chpasswd): https://www.howtoforge.com/user_password_creating_with_a_bash_script (I believe the latest versions of Linux have no option for chpasswd --stdin for security reasons!) I saw an alternate method here: http://linoxide.com/linux-command/change-passwords-batch-mode-chpasswd/ So an alternate method: Run this Python 3 script: file1 = open("usernames_passwords.txt","w") name = "student" for i in range(31,91): file1.write(name + str(i) + ":" + name + str(i) + "abc" + "\n") file1.close() The above makes a file usernames_passwords.txt: student31:student31abc student32:student32abc student33:student33abc etc student90:student90abc A better script is this: import random animals = ["rat","ox","tiger","rabbit","dragon","snake","horse","sheep","monkey","rooster","dog","pig"] colours = ["red","orange","yellow","green","blue","indigo","violet","black","white"] file1 = open("usernames_passwords.txt","w") name = "student" for i in range(31,91): colourvalue = random.randint(1,len(colours)-1) colour = colours[colourvalue] animalvalue = random.randint(1,len(animals)-1) animal = animals[animalvalue] number = random.randint(1,100) file1.write(name + str(i) + ":" + colour + animal + str(number) + "\n") file1.close() The above makes a file usernames_passwords.txt with passwords made from random colours, animals and numbers, e.g.: student31:reddragon71 student32:greenrat24 student33:yellowhorse88 etc student90:blackdog91 These are not the actual passwords, and the password file is deleted from the server after use. Using FileZilla SFTP that file to ad4.ca and move it to /var/www/html then # cd /var/www/html # chpasswd < usernames_passwords.txt That worked. I could log in on FileZilla as student31, but I could cd .. to see all other users. This will change once sections 6. and 7. are done. See 7. for permissions changes. 6. Change the default home directory ------------------------------------- Create a file called userchangehome.bat in /var/www/html (Note: this batch file refers to the text file created in 2. above) # cd /var/www/html # vi userchangehome.bat #!/bin/sh for i in `more usernames.txt ` do echo $i usermod -d /var/www/html/$i $i done IMPORTANT: make sure you use the backquote ` and not a single quote ' Backquote is on the tilde key (~) on US keyboards - below the Esc key. Make the file executable: # chmod 755 userchangehome.bat Execute the file taking care to use the full path: # /var/www/html/userchangehome.bat This will change all the home directories for users to /var/www/html/student31 etc 7. Set student users' permissions ------------------------------------- Create a file called userpermissions.bat in /var/www/html (Note: this batch file refers to the text file created in 2. above) # cd /var/www/html # vi userpermissions.bat #!/bin/sh for i in `more usernames.txt ` do echo $i chown $i $i adduser $i student chgrp student $i done Explanation: # chown student1 student1 (each student owns their own directory) # chown student2 student2 (etc for each student user) # adduser student1 student (puts each student into the group student) # adduser student2 student (etc for each student user) # chgrp student student1 (changes group permissions of ***directory***) # chgrp student student2 (etc for each student user) # IMPORTANT: make sure you use the backquote ` and not a single quote ' Backquote is on the tilde key (~) on US keyboards - below the Esc key. Make the file executable: # chmod 755 userpermissions.bat Execute the file taking care to use the full path: # /var/www/html/userpermissions.bat 8. The following global permissions had already been set --------------------------------------------------------- # cd /var/www (to get ready to set file permissions on directory html) # chmod o-rw html (others are removed read write access to html) # ls -al # drwxr-x--x html # cd /var (to get ready to set file permissions on directory www) # chmod o-rw www (others are removed read write access to www) # cd / (to get ready to set file permissions on directory var) # chmod o-rw var (others are removed read write access to var) # cd / (to get ready to set file permissions on root directory /) # chmod o-rw . (the dot meaning current directory) By default, all users have SSH access *************************************************************** 9. Delete users: student31 to student90 and their home folders --------------------------------------------------------------- Create a file called deleteusers.bat in /var/www/html (Note: this batch file refers to the text file created in 2. above) # cd /var/www/html # vi deleteusers.bat #!/bin/sh for i in `more usernames.txt ` do echo $i deluser --remove-home $i done IMPORTANT: make sure you use the backquote ` and not a single quote ' Backquote is on the tilde key (~) on US keyboards - below the Esc key. Make the file executable: # chmod 755 deleteusers.bat Execute the file taking care to use the full path: # /var/www/html/deleteusers.bat This will delete these users and their home directories from the system. To list all users you can use: cut -d: -f1 /etc/passwd