Users and Groups

Creating Users and Groups

1. The pi has privileges. To run system commands using the superuser privilege, use the command sudo before the command you want to run. For example, to run the command ls as a superuser, use the command sudo ls. You will be prompted for your so that the user trying to run the command as a superuser can be authenticated. 2. Unix provides for creating multiple user accounts. Each user has a unique username, a user id (uid), a home , the starting shell, and some other information. All this information is in the file /etc/. 3. Users belong to one or more groups. Each group has a group name, a group id (gid), and a list of users in that group. This information is in the file /etc/group. 4. Each user has an initial group that the user belongs to when the user is created. This group must exist before the user is created.

5. On Unix/ systems based on the Debian distribution the default convention is to create for each user a group with the same name as the username and that is the only user in that group when the user is created.

6. To create a user, say newuser, first create a group called newuser and then create the user with the username newuser. To create a group use the command groupadd or the command addgroup. Read the man pages to find the differences between the two commands. To create a user use the command useradd or adduser. Read the man pages to find the differences between the two commands. 7. Do you need to create the group for a user before creating the user? Create a user called user1 using the command adduser and another user using the command user2 using the command useradd. What is the difference between the two commands? What is the group for each of the two users you created?

8. When creating a user the system assumes a default location for the home directory for the user. Find out from the man pages where this location is specified. Is the directory actually created when a user is created? Does the directory have to be created explicitly? Find out from the man pages. 9. One can also specify a different (from the default) home directory for a user when creating a user. Find out from the man pages how this is done.

10. Is the password for a user set when creating the user? If so, how is that done? If not, how is the password set? Find out from the man pages. 11. When creating a group or a user, unless specified otherwise, the system will assign the next available gid and uid, respectively, to the new group and user. Find out from the man pages where the information for the next available gid/uid is stored. 12. Find out from the man pages how to specify a gid/uid while creating a group/user.

13. The command groups returns the groups that a user is a member of. Find out what groups your first created user belongs to. Can you find the groups that another user belongs to? Can the superuser find this information?

14. Every file/directory is owned by a user and a group. Find out from the man pages for the command ls how to determine the user owner and the group owner for a file/directory.

1 15. The command chown allows the owner of a file to change ownership (user and group) of the file to some other user and group, as long as the current owner has privileges for the new user and new group. Can the superuser change the ownership of any file and directory to any user and group?

16. Create a user called gilmour with the default uid/gid and with the home directory /home/music/gilmour. 17. Create a user called wright with the uid 600 and gid 700 with the group name wright, and home directory /home/alum/wright. 18. Create a user called waters with the default uid/gid and with the home directory /home/music/waters.

19. Create a user called mason with the default uid/gid and with the home directory /home/music/mason. 20. Create a group called pink with gid 500 and a group called floyd with gid 501. 21. Add the users gilmour, wright, waters and mason to the group floyd, and the users gilmour, wright and mason to the group pink.

22. Log on as the user gilmour using the command

su - gilmour

in a terminal window. This will log you on as the user gilmour, and the shell session will be initialised with all the environment variables for that user. Create a directory in the home directory called division. What is the default group owner of the directory? Change the group owner to be the group pink and make the directory readable, writeable and executable by the group (use the command chmod). How can you test if other members of the group have access to this directory to read and write fom that directory? Does it matter if the home directory for the user gilmour is not owned by the group pink? Does it matter that the home directory for the user gilmour is not readable by the group? writeable by the group? executable by the group? Write the sequence of tasks you will do to check all of the above, and perform to tasks to check. Once you are done checking the tasks, you can log off as the user gilmour by using the command exit. This will end the shell session that you logged on to as the user gilmour and return you back to your shell session in your terminal window. 23. When another member of the group (other than the user gilmour) creates a file in the directory division created above, who is the user/group owner of the file? 24. Log on as the user waters. Create a directory in the home directory called saucerful. What is the default group owner of the directory? Change the group owner to be the group pink. Did this succeed? If so, why? If not, why? Change the group owner to be the group floyd. Did this succeed? Make the directory readable, writeable and executable by the group. Check if the other members of the group floyd have access to this directory.

Moving Users and Creating Many Users

1. Use the command usermod and change the uid and gid for user gilmour to be 3000 and 2000, respec- tively, and move the home directory for the user from /home/music/gilmour to /home/guitarists/gilmour. First, write the steps you would take to achieve this, show me the steps, and then perform the task. 2. You can process command line arguments in a bash script. Consider the following bash script:

#!/bin/bash

echo "Number of arguments = $#";

for arg in "$@"; do echo "Argument: $arg"; if [ "$arg" == "-a" ]; then echo "-a option"; fi if [ "$arg" == "-c" ]; then echo "-c option"; fi done;

Suppose the above script is saved in a file called comline.sh and the file is made executable. Then, try running the script using the commands:

./comline.sh ./comline.sh a b ./comline.sh -a b -c d

Study the script and the output from the above commands to understand how to process command line arguments. 3. You can read in a given file in a bash script. Consider the following bash script:

#!/bin/bash

echo enter file name read fname

exec<$fname i=1; while read -a line; do echo Line No. $i : ${line[*]}; i=$(($i+1)); numWords=${#line[@]}; echo This line has $numWords words; if [ $numWords -gt 0 ]; then echo The first word is ${line[0]}; fi done

Suppose the above script is saved in a file called readfile.sh and the file is made executable. Then, try running the script using the command:

./readfile.sh

and type the name of a file (e.g., comline.sh) when the script asks for input. Study the script and the output from the above command to understand how to read from a file, and how to process individual words on each line of the file. 4. As a system administrator, you need to create several users, each with a different uid (=gid). You would like assign the values for the uid (and hence gid). You would also like to assign the home directory for each user. Assume that you have a file that contains information about each user to be created on one line of the file. The information is the user name, the uid (= gid) and the home directory. A sample file with two users may look like: john 6000 /home/insects/john paul 7000 /home/wings/paul

(a) Write a script that gets the name of the file containing user information on the command line and creates the users specified in the file. You may assume that the uids specified in the file are not used for any other users/groups on the system, and that the parent directories for the home directories (in the above example, the directories /home/insects and /home/wings) exist. (b) Write a second script for the same task, but without making the two assumptions above, i.e., your script will need to make sure that (1) the uid specified is not currently in use as a uid for any user, or as a gid for any group, and (2) the parent directories of the home directories specified do exist.