Linux+ User and Group Permissions Questions and Answers — Questions and Answers
Question 1: A user named 'alex' is a member of the 'developers' and 'qa' groups. A system administrator needs to add 'alex' to the 'sysadmins' group without removing them from their existing groups. Which of the following commands will accomplish this?
- usermod -g sysadmins alex
- usermod -G sysadmins alex
- usermod -aG sysadmins alex (Correct answer)
- usermod -A sysadmins alex
Correct answer: usermod -aG sysadmins alex
The `usermod` command modifies a user account. The `-G` option specifies the supplementary groups. Crucially, the `-a` (append) option must be used with `-G` to add the user to the new group without removing them from their current supplementary groups. Using `-G` alone would replace the user's current group list with the new one.
Question 2: A directory named '/data/shared' is used by the 'editors' group for collaborative work. What is the effect of setting the SetGID (SGID) bit on this directory?
- All files created in the directory will be executable by members of the 'editors' group.
- Any program executed from within the directory will run with the permissions of the 'editors' group.
- Only members of the 'editors' group will be able to create new files in the directory.
- All new files and subdirectories created within '/data/shared' will inherit the group ownership of the directory ('editors'). (Correct answer)
Correct answer: All new files and subdirectories created within '/data/shared' will inherit the group ownership of the directory ('editors').
When the SetGID bit is set on a directory, it forces all new files and subdirectories created within that directory to inherit the group ownership of the parent directory. This is extremely useful for shared directories, as it ensures that new files automatically belong to the correct group ('editors' in this case) for collaboration, rather than the primary group of the user who created them.
Question 3: Which of the following `chmod` commands will set the permissions on a file named `report.txt` to: owner can read and write, group can read only, and others can read only?
- chmod 755 report.txt
- chmod 644 report.txt (Correct answer)
- chmod 664 report.txt
- chmod 744 report.txt
Correct answer: chmod 644 report.txt
Permissions are calculated using octal values: read (4), write (2), and execute (1). For the owner (read+write): 4+2=6. For the group (read-only): 4. For others (read-only): 4. Combining these gives the octal value 644.
Question 4: A system administrator needs to change the ownership of a file named `config.bak` from the user `root` to the user `jdoe` and also change its group ownership to `admins`. Which command achieves this in a single operation?
- chmod jdoe:admins config.bak
- chown jdoe:admins config.bak (Correct answer)
- chgrp admins config.bak && chown jdoe config.bak
- setown jdoe:admins config.bak
Correct answer: chown jdoe:admins config.bak
The `chown` command is used to change the owner and/or group of a file. The syntax `chown user:group filename` allows for changing both the user and group ownership simultaneously. `chmod` changes permissions, not ownership, and `chgrp` only changes the group.
Question 5: The `/tmp` directory on a Linux system has the sticky bit set. A user, 'emma', creates a file `/tmp/emma_file`. Another user, 'liam', who has write permissions to the `/tmp` directory, attempts to delete this file. What will be the outcome?
- Liam will be able to delete the file because he has write permissions on the directory.
- Liam will not be able to delete the file because he is not the file owner. (Correct answer)
- Liam will be able to delete the file only if he is a member of emma's primary group.
- Liam will be prompted for emma's password to delete the file.
Correct answer: Liam will not be able to delete the file because he is not the file owner.
The sticky bit, when set on a directory, restricts file deletion. Even if a user has write permissions on the directory, they can only delete or rename files that they own. Since 'liam' does not own '/tmp/emma_file', he cannot delete it. Only the file owner ('emma') or the root user can delete it.
Question 6: A system administrator sets the umask to `0027` for a user. What will the default permissions be for a newly created directory by this user?
- 770 (rwxrwx---)
- 750 (rwxr-x---) (Correct answer)
- 640 (rw-r-----)
- 777 (rwxrwxrwx)
Correct answer: 750 (rwxr-x---)
The umask value is subtracted from the default base permissions to determine the permissions of new files and directories. For directories, the base permission is `777`. Subtracting the umask (`027`) from the base (`777`) results in `750`. (7-0=7, 7-2=5, 7-7=0). This translates to `rwxr-x---`.
A user named 'alex' is a member of the 'developers' and 'qa' groups.
A system administrator needs to add 'alex' to the 'sysadmins' group without removing them from their existing groups.
Which of the following commands will accomplish this?