010-160 - LPI Linux Essentials File Permissions and Ownership Questions and Answers — Questions and Answers
Question 1: A system administrator needs to set the permissions for a shell script named `deploy.sh`. The requirements are that the owner can read, write, and execute it; members of the group can read and execute it; and all other users can only read it. Which of the following `chmod` commands will set these permissions correctly using octal notation?
- chmod 644 deploy.sh
- chmod 755 deploy.sh
- chmod 754 deploy.sh (Correct answer)
- chmod 777 deploy.sh
Correct answer: chmod 754 deploy.sh
In octal notation, read is 4, write is 2, and execute is 1. The permissions are calculated for owner, group, and other. Owner (read+write+execute) = 4+2+1=7. Group (read+execute) = 4+1=5. Other (read) = 4. Therefore, the correct octal code is 754.
Question 2: Which command is used to change the ownership of `file.txt` to the user `anne` and the group `developers` in a single operation?
- chown anne:developers file.txt (Correct answer)
- chown anne developers file.txt
- chgrp developers file.txt && chown anne file.txt
- chmod anne:developers file.txt
Correct answer: chown anne:developers file.txt
The `chown` command is used to change file ownership. The syntax `chown user:group filename` allows changing both the user owner and the group owner simultaneously.
Question 3: A user runs the `umask 0027` command. What will the default permissions be for a newly created directory afterwards?
- drwxr-xrwx
- drwxr-x--- (Correct answer)
- drw-r-----
- drwxrwxrwx
Correct answer: drwxr-x---
The system starts with default permissions for a directory, which is 777 (`rwxrwxrwx`). The `umask` value is subtracted from this default. 777 - 027 results in 750. This octal value corresponds to `rwxr-x---`.
Question 4: What is the primary function of the execute (`x`) permission when it is set on a directory?
- It allows a user to create, delete, or rename files within the directory.
- It allows a user to list the contents of the directory using the `ls` command.
- It allows a user to enter the directory with the `cd` command. (Correct answer)
- It allows a user to read the contents of files within the directory.
Correct answer: It allows a user to enter the directory with the `cd` command.
For a directory, the execute (`x`) permission grants the ability to traverse it or enter it, for example, by using the `cd` command. Listing contents requires read (`r`) permission, and modifying the directory's contents (creating/deleting files) requires write (`w`) permission.
Question 5: A file has the permissions `-rw-r--r--`. The owner wants to add write permission for the group members without affecting any other permissions. Which of the following commands will achieve this?
- chmod 664 file.txt
- chmod g+w file.txt (Correct answer)
- chmod o+w file.txt
- chmod g=w file.txt
Correct answer: chmod g+w file.txt
The symbolic notation `g+w` specifically targets the 'group' (`g`) and 'adds' (`+`) the 'write' (`w`) permission. Using octal `664` would work in this specific case, but it resets all permissions rather than just adding one. The `g+w` syntax is the most precise way to add a permission without altering others.
Question 6: After running `ls -l`, you see the following output for a file: `-rwxr-x--- 1 student staff 4096 Mar 18 12:00 mydata`. What can a user who is a member of the `staff` group do with this file?
- Read, write, and execute the file.
- Only read the file.
- Read and execute the file. (Correct answer)
- Nothing, they have no permissions.
Correct answer: Read and execute the file.
The permission string `-rwxr-x---` is divided into three sets after the initial character: owner (rwx), group (r-x), and other (---). The group permissions are `r-x`, which means members of the `staff` group can read the file and execute it, but they cannot write to it.
A system administrator needs to set the permissions for a shell script named `deploy.sh`.
The requirements are that the owner can read, write, and execute it; members of the group can read and execute it; and all other users can only read it.
Which of the following `chmod` commands will set these permissions correctly using octal notation?