Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Scenario test · 80 minutes

Scenario test - take over a server that is broken in five ways

After this lesson you can
  • Collect evidence first, and find five faults of different kinds with the method of this part
  • Repair each fault with the smallest safe change and prove it with a test
  • Write a short incident report that another person can check
Lab files of this lesson (25)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

Your colleague who ran the reporting server has moved to another team. The handover note says: "Everything was fine on Friday. Since then the report service is down, the data volume is full, the web user gets errors on the shop page, the client network cannot find the shop by name, and the machine is slow. I did not have time to look." Your lead wants the server working again by the end of the day, and a written report of what you found. Nothing that works may be broken, and nothing may be deleted that someone needs.

The server is your practice system. A script plants the faults, so that every reader starts from the same state. Save it as handover.sh. It makes a service, a small volume, a test user, a web page, two service units and a private network, so run it only on a practice system that you can restore. It needs netlab.sh and krdns.py of lesson 6 in the same folder. Run it with sudo bash handover.sh up. The last line is handover done: the server is yours.

handover.sh

#!/bin/bash
# handover.sh - plants five faults on YOUR practice system, as a colleague's server that you take over.
#   sudo bash handover.sh up      plant the faults
#   sudo bash handover.sh down    remove everything that "up" made
# It needs netlab.sh and krdns.py of lesson 6 in the same folder. Do not run it on a system that is not a lab.
set -u
[ "$(id -u)" -eq 0 ] || { echo "run it with sudo"; exit 1; }
HERE=$(cd "$(dirname "$0")" && pwd)
U=/etc/systemd/system

case "${1:-}" in
up)
    # fault 1: a service that exits at once
    cat > /usr/local/bin/kr-report.sh <<'EOS'
#!/bin/bash
conf=/etc/krreport.conf
[ -r "$conf" ] || { echo "kr-report: cannot read $conf" >&2; exit 1; }
. "$conf"
while :; do echo "report every ${interval}s"; sleep "$interval"; done
EOS
    chmod 755 /usr/local/bin/kr-report.sh
    rm -f /etc/krreport.conf
    printf '[Unit]\nDescription=Report service\n\n[Service]\nExecStart=/usr/local/bin/kr-report.sh\n\n[Install]\nWantedBy=multi-user.target\n' > $U/kr-report.service

    # fault 2: a full volume with one file that must stay
    umount /srv/krdata 2> /dev/null
    mkdir -p /srv/krdata
    mount -t tmpfs -o size=4m krdata /srv/krdata
    mkdir /srv/krdata/keep
    echo "do not delete" > /srv/krdata/keep/important.txt
    head -c 1572864 /dev/zero > /srv/krdata/old-1.log
    head -c 100M /dev/zero > /srv/krdata/old-2.log 2> /dev/null

    # fault 3: a web page that the web user cannot read
    id kr_web > /dev/null 2>&1 || useradd --system --shell /usr/sbin/nologin kr_web
    mkdir -p /srv/krweb/site
    echo "<h1>shop</h1>" > /srv/krweb/site/index.html
    chown root:root /srv/krweb/site/index.html
    chmod 755 /srv/krweb /srv/krweb/site
    chmod 600 /srv/krweb/site/index.html

    # fault 4: a client in a private namespace that cannot find the name server
    bash "$HERE/netlab.sh" down > /dev/null 2>&1
    bash "$HERE/netlab.sh" up > /dev/null
    bash "$HERE/netlab.sh" fault 3 > /dev/null

    # fault 5: a spinning process that a service restarts at once
    printf '#!/bin/bash\nwhile :; do :; done\n' > /usr/local/bin/kr-batch.sh
    chmod 755 /usr/local/bin/kr-batch.sh
    printf '[Unit]\nDescription=Nightly batch job\n\n[Service]\nExecStart=/usr/local/bin/kr-batch.sh\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n' > $U/kr-batch.service

    systemctl daemon-reload
    systemctl enable --now kr-batch > /dev/null 2>&1
    systemctl start kr-report 2> /dev/null
    echo "handover done: the server is yours"
    ;;
down)
    bash "$HERE/netlab.sh" down > /dev/null 2>&1
    for s in kr-report kr-batch; do
        systemctl disable --now $s > /dev/null 2>&1
        rm -f $U/$s.service
    done
    systemctl daemon-reload
    systemctl reset-failed 2> /dev/null
    rm -f /usr/local/bin/kr-report.sh /usr/local/bin/kr-batch.sh /etc/krreport.conf
    umount /srv/krdata 2> /dev/null
    rm -rf /srv/krdata /srv/krweb
    userdel kr_web 2> /dev/null
    echo "handover removed"
    ;;
*)
    echo "usage: sudo bash handover.sh up|down"
    exit 1
    ;;
esac

The lesson also gives you the script that judges your work. Save it as verify.sh. It prints one line for each fault, fixed or still broken, and a last line with the count. Use it only to confirm a repair. The faults are for you to find.

verify.sh

#!/bin/bash
# verify.sh - checks the five repairs of the scenario test. Run it with sudo. It prints one line for each fault.
[ "$(id -u)" -eq 0 ] || { echo "run it with sudo"; exit 1; }
fixed=0

report() {    # report NUMBER NAME COMMAND...: prints "fixed" when the command succeeds
    local n=$1 name=$2
    shift 2
    if "$@" > /dev/null 2>&1; then
        echo "fault $n $name: fixed"
        fixed=$((fixed + 1))
    else
        echo "fault $n $name: still broken"
    fi
}

service_up() { systemctl is-active --quiet kr-report; }
volume_ok() {
    [ "$(df --output=avail -B1 /srv/krdata | tail -n 1)" -ge 1048576 ] &&
        [ "$(cat /srv/krdata/keep/important.txt)" = "do not delete" ]
}
page_readable() {
    sudo -u kr_web cat /srv/krweb/site/index.html &&
        [ $((8#$(stat -c %a /srv/krweb/site/index.html) & 2)) -eq 0 ]
}
name_works() { [ "$(ip netns exec krclient curl -s -m 5 http://shop.krlab.test:8080/)" = "shop ok" ]; }
batch_gone() { ! pgrep -f kr-batch.sh && [ "$(systemctl is-enabled kr-batch 2> /dev/null)" != enabled ]; }

report 1 "service kr-report" service_up
report 2 "volume /srv/krdata" volume_ok
report 3 "page /srv/krweb" page_readable
report 4 "name lookup in krclient" name_works
report 5 "runaway kr-batch" batch_gone
echo "fixed $fixed of 5"
[ "$fixed" -eq 5 ]

Your tasks

Work in this order. The tasks name the area, not the commands: use the lessons of this part. Test each repair and keep the proof for your report.

Task 1. Evidence first. Before you change anything, collect a bundle with the kit of lesson 10 (as an administrator, so that it holds more), and run sudo bash verify.sh once to see the state. Write down what each fault looks like from outside.

Task 2. The report service. kr-report is down. Find the status code and the message that the program itself wrote, and repair the cause. The service must run, and it must still be the same script and the same unit.

Task 3. The data volume. /srv/krdata is full. Find what fills it, and free at least 1 MB. The folder keep and its file must stay as they are. Nothing may be deleted: someone may still need a log, so move what you remove to another volume, and write down where it went.

Task 4. The shop page. The web user kr_web must read /srv/krweb/site/index.html. Give it the right, and no more: the file must not become writable by everybody. Test as that user.

Task 5. The name lookup. In the namespace krclient, the request http://shop.krlab.test:8080/ fails, and the request by address works. Find the cause in the order of the checks of lesson 6, change one thing, and test again. Use ip netns exec krclient to work inside the client.

Task 6. The slow machine. A process uses a whole CPU. Find out which unit starts it, and why a plain kill does not help. Stop it, and make sure that it does not return at the next boot.

Task 7. Prove it. Run sudo bash verify.sh after all repairs, and collect a second evidence bundle.

Hand in

Write a short incident report in the file incident.md. Use these headings, in this order:

  1. ## Summary: two or three sentences for a manager. What was wrong, what is the state now.
  2. ## Timeline: the steps in order, each with the time, the command and what you saw.
  3. ## Causes and fixes: for each of the five faults, the cause, the repair and the file or setting that you changed.
  4. ## Proof: for each repair, the test that failed before and passes now, and the output of verify.sh.
  5. ## Prevention: at least three changes that would stop each kind of fault from coming back, and what you did not fix.

Keep it to one page. A reader who did not watch you must be able to repeat your tests.

Review your work

A model solution

The faults, the lesson of each and the repair:

#FaultLessonEvidenceRepair
1The service reads a settings file that is missing9status=1/FAILURE, and the journal line kr-report: cannot read /etc/krreport.confWrite the file /etc/krreport.conf with interval=5, and start the unit
2The volume is full of old logs8df -h shows 100%, du -ab shows old-2.log as the biggestMove the biggest log to another volume, and keep the keep folder
3The page has mode 600 and the owner root9namei -l shows -rw-------, and the user gets Permission deniedchgrp kr_web and chmod 640 on the file
4The namespace uses a name server that does not answer6Request by address works, by name times out, and resolv.conf names 192.0.2.53Write the right name server, 203.0.113.1, in /etc/netns/krclient/resolv.conf
5A batch unit spins, and Restart=always brings it back7ps shows kr-batch.sh at 100%, and systemctl show -p Restart prints alwayssystemctl disable --now kr-batch

The commands, in the order of the tasks:

sudo bash kr-evidence.sh -o before
sudo bash verify.sh
systemctl status kr-report --no-pager | grep -E "Active|Process"
sudo journalctl -u kr-report -n 3 --no-pager -o cat
printf 'interval=5\n' | sudo tee /etc/krreport.conf
sudo systemctl reset-failed kr-report && sudo systemctl start kr-report
du -ab /srv/krdata | sort -rn | head -n 3
sudo mv /srv/krdata/old-2.log /var/tmp/krdata-old-2.log
df -h /srv/krdata | tail -n 1
namei -l /srv/krweb/site/index.html
sudo chgrp kr_web /srv/krweb/site/index.html
sudo chmod 640 /srv/krweb/site/index.html
sudo -u kr_web cat /srv/krweb/site/index.html
sudo ip netns exec krclient cat /etc/resolv.conf
echo "nameserver 203.0.113.1" | sudo tee /etc/netns/krclient/resolv.conf
sudo ip netns exec krclient curl -s -m 5 http://shop.krlab.test:8080/
ps -eo pid,pcpu,args --sort=-pcpu | head -n 3
systemctl show -p Restart --value kr-batch
sudo systemctl disable --now kr-batch
sudo bash verify.sh
sudo bash kr-evidence.sh -o after

Why not kill for fault 5: the unit has Restart=always, so systemd starts the process again within a second. The unit must be stopped, and disabled so that it does not start at the next boot. kill is the right tool only for a process that no unit owns.

What can be left open, for example: the settings file of kr-report was written by hand and is not in Git yet (lesson 4); the old log is in /var/tmp and its owner must say whether it can go; the volume will fill again without log rotation; the name server setting of the client is not managed by a description file (lesson 5); and nobody knows what the batch job was meant to do, so its owner must be asked before it is deleted.

Mark your report

You mark this yourself, or you ask a colleague or a mentor to mark it. The page checks the numbers above; it cannot read your report.

CriterionNot yetMeets the standardStrong
CausesA fault has a symptom but no cause.All five causes: missing settings file, big log, mode 600, wrong name server, Restart=always.Each cause also names the lesson method that found it.
EvidenceA cause is a guess, or the first bundle came after a change.Bundle before any change; each timeline step has time, command and output.Both bundles and both verify.sh runs are attached or quoted.
RepairsA file was deleted, made world-writable, or the process was only killed.Smallest change, with its file; the moved log's new place is written down.Each repair also says how to undo it.
Judgement"All fixed", with no test or open point.Three prevention steps, and what is still open and who must decide.Open points ranked, with a reason for the order.
WritingThe summary needs Linux knowledge to read.Dated, one page; a manager understands the summary.Neutral words about the colleague; facts, not blame.

Meets the standard in every row: you can show this report as a work sample. Remove names and data that are not yours to share.

The report shows your work on this scenario, done on your own machine without a proctor. It does not show work on a live server under pressure.

Clean up when you have finished: sudo bash handover.sh down, sudo bash netlab.sh down if it is still there, and sudo rm -r before after bundles /var/tmp/krdata-old-2.log.

Get a result link

When all 9 answers above are right, the server can check them again and give you a signed link. You can put the link in a CV or send it to a manager: it opens a page that shows the course, the test, the date and the name you give here. No account, and nothing is stored: keep the link.