Linux on call · Chapter 15 of 30

Ownership and sudo

By the end of this chapter you can see who owns a file, give files to the right user and group with sudo, and use sudo for one command without leaving root's files behind. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Permissions and users
  • 4 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

The ordering app runs as you, student, and this morning it cannot save a single order. On his last night Arjun ran the importer and the server with sudo, so what they wrote belongs to root, the all-powerful admin user. Bunty "fixed" orders.json with sudo chmod 666:

ls -l app/orders.json app/logs/app.log handover.txt
-rw-r--r-- 1 root root 14 Oct  3 18:10 app/logs/app.log
-rw-rw-rw- 1 root root 48 Oct  3 18:10 app/orders.json
-rw------- 1 root root 90 Oct  3 18:10 handover.txt

Now anyone on the machine can change Tapri's orders. What you know from chapter 14 changes a file's mode, and only its owner may do even that. Kabir: "It is not the mode, it is the owner." Ravi also needs the web server to read the menu page, and Arjun left the shop's spare key code in a handover note only root can read.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Make everything under app/ yours (student) again

    Make the whole app folder yours again. Today app/orders.json and app/logs/app.log belong to root.

    1. Use find to list what under app is not owned by student.

    2. Use sudo with chown to make student the owner of app and everything inside it.

    3. Run find again. It should print nothing.

    To pass: app and every file and folder inside it are owned by the user student. app still has its four files (server.js, public/menu.html, orders.json and logs/app.log), and orders.json still has its orders.

  2. Task 2

    Stop everyone writing to orders.json (644)

    Only you should change the orders. The group and others should only read them.

    1. Do task 1 first, so app/orders.json is yours.

    2. Use chmod to set app/orders.json to mode 644. You do not need sudo for this.

    3. Run ls -l app/orders.json to see the owner and the mode.

    To pass: app/orders.json is owned by student and has mode 644.

  3. Task 3

    Give menu.html to the web server: group www-data, mode 640

    The web server reads files through its group, www-data. Let it read the menu.

    1. Use sudo with chgrp to put app/public/menu.html in the group www-data.

    2. Use chmod to set app/public/menu.html to mode 640.

    3. Run ls -l app/public/menu.html. The owner is still student.

    To pass: app/public/menu.html is owned by student, is in the group www-data, and has mode 640. If you run task 1's chown again later, set the group again too.

  4. Task 4

    Save the key code from root's note in answer.txt

    Root's note handover.txt has a spare key code. Read it without changing the note.

    1. Run cat handover.txt. It is refused, because the note is root's.

    2. Read it again with sudo in front.

    3. Save only the key code in answer.txt, in the chapter folder. Do not use sudo for this.

    To pass: you read handover.txt with sudo in the chapter folder (a command with an error does not count), and answer.txt has the key code and nothing else. answer.txt must be owned by student. handover.txt must stay owned by root, in the group root, with mode 600.

What you will learn

  • Owners and groups: every file has one user and one group, and which of the three rwx sets applies depends on who you are.
  • sudo: run one command as root.
  • chown and chgrp: give a file to another user or group.
  • Where sudo stops: which part of a command really runs as root.

More in Permissions and users

Build "Ownership and sudo" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.