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.

  1. 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?

    1. Change it to /opt/app/report.sh | 2>&1 /var/log/report.log
    2. Change it to /opt/app/report.sh 2>&1 >> /var/log/report.log
    3. Change it to /opt/app/report.sh 2>/dev/null > /var/log/report.log
    4. 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

  2. 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?

    1. init=/sbin/rescue
    2. systemd.unit=rescue.target
    3. systemd.default=emergency.service
    4. 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

  3. Question 3

    Domain: Services and User Management

    Which single command starts the nginx service now and also configures it to start at every boot?

    1. systemctl enable nginx
    2. systemctl enable --now nginx
    3. systemctl start --boot nginx
    4. 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

  4. 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?

    1. OnBootSec=3h
    2. RemainAfterExit=true
    3. AccuracySec=1d
    4. 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

  5. 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?

    1. passwd -x 7 -w 90 alice
    2. chage -m 90 -I 7 alice
    3. chage -M 90 -W 7 alice
    4. 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

  6. 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?

    1. %ops ALL=(root) /usr/bin/systemctl restart nginx
    2. ops ALL=(root) /usr/bin/systemctl restart nginx
    3. %ops ALL=(root) NOPASSWD: ALL
    4. %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/

  7. 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?

    1. $#
    2. $!
    3. $$
    4. $?
    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

  8. Question 8

    Domain: Automation, Orchestration, and Scripting

    Which built-in Python type is an ordered sequence that cannot be modified after it is created?

    1. list
    2. dict
    3. tuple
    4. 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

  9. 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?

    1. ip route add default via 192.168.50.1
    2. ip addr add default via 192.168.50.1
    3. ip route add 192.168.50.1 dev ens3
    4. 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

  10. 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?

    1. cat /etc/resolv.conf
    2. nmcli general hostname
    3. resolvectl status
    4. 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.