RHCSA Essential Command-Line Tools Questions and Answers — Questions and Answers
Question 1: A system administrator needs to find all files within the `/etc` directory that are owned by the `root` user and have been modified in the last 7 days. Which command will accomplish this?
- find /etc -user root -mtime +7
- find /etc -user root -mtime -7 (Correct answer)
- find /etc -group root -mmin -7
- find /etc -user root -ctime 7
Correct answer: find /etc -user root -mtime -7
The correct command is `find /etc -user root -mtime -7`. The `find` command searches the specified path (`/etc`). The `-user root` predicate selects files owned by 'root'. The `-mtime -7` predicate selects files whose data was modified less than 7 days ago (i.e., within the last 7 days). `-mtime +7` would find files modified more than 7 days ago, and `-mtime 7` would find files modified exactly 7 days ago.
Question 2: Which of the following commands correctly creates a bzip2-compressed tar archive named `backup.tar.bz2` containing the contents of the `/home/user/data` directory?
- tar -czf backup.tar.bz2 /home/user/data
- tar -xjf backup.tar.bz2 /home/user/data
- tar -cjf backup.tar.bz2 /home/user/data (Correct answer)
- tar -cvf backup.tar.bz2 /home/user/data
Correct answer: tar -cjf backup.tar.bz2 /home/user/data
The correct command is `tar -cjf backup.tar.bz2 /home/user/data`. The `-c` flag is for creating an archive. The `-j` flag specifies bzip2 compression. The `-f` flag is used to specify the output archive filename. The `-z` flag is for gzip compression, `-x` is for extracting, and omitting a compression flag like `-j` or `-z` would create an uncompressed archive.
Question 3: A system administrator needs to search through `/var/log/messages` for lines containing the exact word 'error', but not 'errors' or 'error-code'. Which command should be used?
- grep 'error' /var/log/messages
- grep -w 'error' /var/log/messages (Correct answer)
- grep -v 'error' /var/log/messages
- grep -i 'errors' /var/log/messages
Correct answer: grep -w 'error' /var/log/messages
The `grep -w 'error' /var/log/messages` command is correct. The `-w` option tells `grep` to select only those lines containing matches that form whole words. A simple `grep 'error'` would match substrings. `grep -v` inverts the match, showing lines that do not contain 'error'. `grep -i` performs a case-insensitive search.
Question 4: An application with PID 2501 is completely unresponsive. The administrator first tried `kill 2501`, but the process did not terminate. What is the most appropriate next command to forcefully stop the process?
- kill -1 2501
- kill -15 2501
- killall -9 application_name
- kill -9 2501 (Correct answer)
Correct answer: kill -9 2501
The command `kill -9 2501` sends the SIGKILL signal. This is a non-catchable, non-ignorable signal that is handled directly by the kernel to terminate the process immediately. The default `kill` command and `kill -15` both send SIGTERM, which is a polite request to terminate that an unresponsive process can ignore. `kill -1` (SIGHUP) typically tells a process to reload its configuration. While `killall -9` could also work, using the specific PID with `kill -9` is more direct when the PID is already known.
Question 5: To perform an in-place replacement of every occurrence of the IP address `10.0.1.50` with `10.0.2.50` within the file `/etc/config.txt`, which `sed` command should be used?
- sed 's/10.0.1.50/10.0.2.50/' /etc/config.txt > /etc/config.txt
- sed -i 's/10.0.1.50/10.0.2.50/g' /etc/config.txt (Correct answer)
- sed 's/10.0.1.50/10.0.2.50/g' /etc/config.txt
- sed -i 's/10.0.1.50/10.0.2.50/' /etc/config.txt
Correct answer: sed -i 's/10.0.1.50/10.0.2.50/g' /etc/config.txt
The correct command is `sed -i 's/10.0.1.50/10.0.2.50/g' /etc/config.txt`. The `-i` flag modifies the file in-place. The `s/.../.../` is the substitute command. The `g` flag at the end ensures the replacement is global, affecting all occurrences on a line, not just the first. Redirecting output to the same file you are reading from can truncate the file, and omitting the `g` flag will only replace the first match on each line.
Question 6: A user account named `testuser` exists. A system administrator needs to add this user to the `wheel` supplementary group and also change their login shell to `/usr/bin/zsh`. Which of the following commands accomplishes both tasks in a single operation?
- usermod -G wheel -s /usr/bin/zsh testuser
- useradd -aG wheel -s /usr/bin/zsh testuser
- usermod -aG wheel -s /usr/bin/zsh testuser (Correct answer)
- groupmod -a wheel testuser && usermod -s /usr/bin/zsh testuser
Correct answer: usermod -aG wheel -s /usr/bin/zsh testuser
The command `usermod -aG wheel -s /usr/bin/zsh testuser` is correct. `usermod` is used to modify an existing user. The `-aG` flags append (`-a`) the user to a supplementary group (`-G`). It is critical to use `-a` to avoid removing the user from other supplementary groups. The `-s` flag sets the new login shell. `useradd` is for creating new users. Omitting `-a` with `-G` would replace the user's supplementary group list entirely.
A system administrator needs to find all files within the `/etc` directory that are owned by the `root` user and have been modified in the last 7 days.
Which command will accomplish this?