CompTIA Linux+ sample questions with answers
10 free CompTIA Linux+ sample questions across the exam's domains, each with its answer and an explanation. No account needed.
CompTIA Linux+ sample questions
CompTIA Linux+ (V8), CompTIA.
Question 1
Domain: System Management
A cron job runs: /opt/app/report.sh 2>&1 > /var/log/report.log. Error messages are still being mailed to root instead of appearing in the log.
What is the correct fix?
- Change it to /opt/app/report.sh | 2>&1 /var/log/report.log
- Change it to /opt/app/report.sh 2>&1 >> /var/log/report.log
- Change it to /opt/app/report.sh 2>/dev/null > /var/log/report.log
- Change it to /opt/app/report.sh > /var/log/report.log 2>&1
Show the answer
Answer: D. Change it to /opt/app/report.sh > /var/log/report.log 2>&1
Redirections are processed left to right: 2>&1 first copies stderr to wherever stdout points at that moment (cron's output), and only afterwards is stdout moved to the file. Putting > file before 2>&1 makes both streams go to the log; switching to >> merely appends and keeps the same ordering problem.
Checked against: https://www.gnu.org/software/bash/manual/html_node/Redirections.html
Question 2
Domain: System Management
A systemd-based server keeps hanging while starting a network-dependent service. You want to boot it once into single-user maintenance with the local filesystems mounted, without editing any files on disk.
What should you append to the kernel command line from the GRUB edit screen?
- init=/sbin/rescue
- systemd.unit=rescue.target
- systemd.default=emergency.service
- runlevel=S systemd.mask=network
Show the answer
Answer: B. systemd.unit=rescue.target
systemd.unit= on the kernel command line overrides the default target for that boot only, and rescue.target starts a single-user shell after mounting local filesystems. The init= option does replace PID 1 but only with a program path that exists, and /sbin/rescue is not a real binary, so the kernel would fail to start it.
Checked against: https://www.freedesktop.org/software/systemd/man/latest/kernel-command-line.html
Question 3
Domain: Services and User Management
Which single command starts the nginx service now and also configures it to start at every boot?
- systemctl enable nginx
- systemctl enable --now nginx
- systemctl start --boot nginx
- systemctl unmask --now nginx
Show the answer
Answer: B. systemctl enable --now nginx
systemctl enable creates the symlinks that pull the unit in at boot, and --now also starts it immediately. enable on its own is the tempting answer, but it only affects future boots and leaves the service stopped until you start it.
Checked against: https://www.freedesktop.org/software/systemd/man/latest/systemctl.html
Question 4
Domain: Services and User Management
A systemd timer uses OnCalendar=*-*-* 03:00:00 to trigger a backup service. The laptop it runs on is often powered off at 03:00, so backups are silently skipped.
Which setting in the [Timer] section makes the missed run happen as soon as the system next boots?
- OnBootSec=3h
- RemainAfterExit=true
- AccuracySec=1d
- Persistent=true
Show the answer
Answer: D. Persistent=true
With Persistent=true, systemd stores when the timer last fired and starts the service immediately if a calendar event was missed while the machine was off. RemainAfterExit is a [Service] option for oneshot units, and OnBootSec schedules a run relative to boot every time rather than catching up only on missed runs.
Checked against: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html
Question 5
Domain: Security
Policy says alice's password must expire every 90 days, with a warning 7 days beforehand.
Which command applies this to her account?
- passwd -x 7 -w 90 alice
- chage -m 90 -I 7 alice
- chage -M 90 -W 7 alice
- usermod -e 90 -f 7 alice
Show the answer
Answer: C. chage -M 90 -W 7 alice
In chage, -M sets the maximum number of days a password stays valid and -W the number of warning days before expiry. Lower-case -m sets the minimum days between changes and -I the inactivity period after expiry, a common mix-up, while usermod -e sets an account expiry date rather than password ageing.
Checked against: https://man7.org/linux/man-pages/man1/chage.1.html
Question 6
Domain: Security
Members of the ops group must be able to restart nginx as root, but nothing else, using sudo.
Which rule, added with visudo -f /etc/sudoers.d/ops, grants this?
- %ops ALL=(root) /usr/bin/systemctl restart nginx
- ops ALL=(root) /usr/bin/systemctl restart nginx
- %ops ALL=(root) NOPASSWD: ALL
- %ops ALL=(ALL) ALL, !/usr/bin/systemctl
Show the answer
Answer: A. %ops ALL=(root) /usr/bin/systemctl restart nginx
The % prefix makes the rule apply to a group, and listing the full command with its arguments restricts sudo to exactly that command. Without %, 'ops' is interpreted as a user name, so group members would get nothing; the other two rules grant far broader rights than required.
Checked against: https://www.sudo.ws/docs/man/sudoers.man/
Question 7
Domain: Automation, Orchestration, and Scripting
In a bash script, which special parameter holds the exit status of the most recently executed foreground command?
- $#
- $!
- $$
- $?
Show the answer
Answer: D. $?
$? expands to the exit status of the last foreground pipeline, where 0 means success. $! is the PID of the most recent background job and $$ the PID of the shell itself; $! is easy to confuse because of its similar role around jobs.
Checked against: https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html
Question 8
Domain: Automation, Orchestration, and Scripting
Which built-in Python type is an ordered sequence that cannot be modified after it is created?
- list
- dict
- tuple
- set
Show the answer
Answer: C. tuple
Tuples are immutable sequences: their elements are ordered and indexed, but items cannot be added, removed or reassigned. A list is also an ordered sequence, which makes it the tempting pick, but it is mutable.
Checked against: https://docs.python.org/3/library/stdtypes.html#tuples
Question 9
Domain: Troubleshooting
A server can reach hosts on 192.168.50.0/24 but nothing beyond it. ip route shows only: 192.168.50.0/24 dev ens3 proto kernel scope link src 192.168.50.20. The gateway is 192.168.50.1.
Which command restores off-subnet connectivity immediately?
- ip route add default via 192.168.50.1
- ip addr add default via 192.168.50.1
- ip route add 192.168.50.1 dev ens3
- ip link set default 192.168.50.1
Show the answer
Answer: A. ip route add default via 192.168.50.1
With no default route, the kernel only knows the directly connected subnet; adding a default route via the gateway sends all other traffic there (make it persistent in the network configuration too). ip route add 192.168.50.1 dev ens3 only adds a host route to the gateway itself and still leaves no path to other networks.
Checked against: https://man7.org/linux/man-pages/man8/ip-route.8.html
Question 10
Domain: Troubleshooting
On an Ubuntu server using systemd-resolved, /etc/resolv.conf contains only nameserver 127.0.0.53, and some internal names fail to resolve.
Which command shows the DNS servers and search domains actually in use for each interface?
- cat /etc/resolv.conf
- nmcli general hostname
- resolvectl status
- hostnamectl --dns
Show the answer
Answer: C. resolvectl status
systemd-resolved runs a local stub resolver on 127.0.0.53, so resolv.conf only points at the stub; resolvectl status reveals the per-link upstream servers and search domains. Reading /etc/resolv.conf is the tempting step, but here it shows only the stub address.
Checked against: https://www.freedesktop.org/software/systemd/man/latest/resolvectl.html
More practice
A 20-question practice sampler is free with an account; Pro adds the full question bank and timed mock exams.
CompTIA Linux+ course and practice exam: XK0-006: the exam guide, with the format, cost, pass mark and domains from the vendor.