LINUX · LPI LINUX ESSENTIALS · CHAPTER 6

Security and Permissions

Users, permissions, links and SSH: the system under control.

5 min readDomain 015 · Security and PermissionsLevel: beginner

From point A to point B

Point A: you work with your own user and, when something blocks you, you try sudo or chmod 777 without being quite sure why it works. Point B: by the end of this chapter you will understand the types of users, create users and groups, read and assign permissions precisely, use special permissions, tell hard links from symbolic links, and connect to other machines securely with SSH.

This is the last chapter of the course and the one you will notice most day to day: almost every security incident starts with one permission too many.

Types of users and where they are stored

  • root (UID 0): the superuser. It can do anything, including breaking the system.
  • System users (usually UID below 1000): accounts for services such as www-data or sshd. They do not log in; they exist so that each service runs with the minimum permissions.
  • Regular users (usually UID from 1000 up): the people.
FileContentsWho can read it
/etc/passwdUsers: name, UID, GID, home directory and shellEveryone
/etc/shadowEncrypted passwords (hashes) and their expirationOnly root
/etc/groupGroups and their membersEveryone
/etc/skel/Template copied into the home directory of every new userEveryone
id                       # your UID, GID and groups
whoami                   # your user name
groups                   # your groups
who                      # who has a session open
w                        # who is logged in and what they are doing
last                     # latest logins
getent passwd jesus      # a user's line
getent group sudo        # the members of a group

Privileges: sudo and su

sudo apt update          # runs ONE command as root, with YOUR password
sudo -i                  # opens a root shell (use it as little as possible)
su - ana                 # switches to user ana, with ana's password
su -                     # switches to root, with root's password

sudo is the recommended option: every use is logged, there is no need to share the root password, and you control who can use it in /etc/sudoers (always edited with visudo). Ubuntu comes with it configured out of the box; on Debian, if you set a root password during installation, you will need to add your user to the sudo group.

Principle of least privilege. Work with your regular user and elevate privileges only for the command that needs it. An open root shell is a mistake waiting to happen.

Creating and managing users and groups

sudo groupadd teachers
sudo useradd -m -s /bin/bash -G teachers lucia   # -m creates /home/lucia · -s their shell · -G extra groups
sudo passwd lucia                                  # sets the password
sudo usermod -aG docker lucia                      # ADDS a group (without -a, it replaces them all)
sudo passwd -l lucia                               # locks the account
sudo passwd -u lucia                               # unlocks it
sudo userdel -r lucia                              # deletes the user and their home directory
sudo groupdel teachers

On Debian and Ubuntu there is also adduser, which is more interactive. Groups in Linux have no hierarchy: a user has one primary group and can belong to several secondary ones.

Permissions: read, write and execute

Every file has three sets of permissions: for its owning user (u), for its group (g) and for everyone else, the others (o).

Reading Linux permissions In -rwxr-x--- the first character is the file type. Then come three groups of three: user rwx equals 7, group r-x equals 5, others none equals 0, so the octal mode is 750. Read is 4, write is 2 and execute is 1. -rwxr-x--- = 750 - rwx r-x --- type- file · d dirl link user (u)4+2+1 = 7 group (g)4+0+1 = 5 others (o)0+0+0 = 0 r = 4 readw = 2 writex = 1 executea = u + g + o on a directory: r = list its contents (ls) · w = create or delete inside it · x = enter it (cd) chmod 750 script.sh = chmod u=rwx,g=rx,o= script.sh

How to read an ls -l line and its octal equivalent. Diagram: The House of Routing.

ls -l
# -rwxr-x---  1 jesus devops  127 oct 19 15:10 backup.sh
# drwxr-xr-x  2 jesus jesus  4096 oct 19 20:47 project
ls -ld project          # permissions of the directory, not its contents

Changing permissions with chmod

chmod u+x backup.sh            # adds execute for the owner
chmod g-w,o-r report.txt      # removes write from the group and read from others
chmod a+r public.txt          # read for everyone (a = u+g+o)
chmod u=rwx,g=rx,o= backup.sh  # assigns exactly
chmod 750 backup.sh            # the same in octal
chmod 644 notes.txt            # typical for a file: rw-r--r--
chmod 755 scripts/             # typical for a directory: rwxr-xr-x
chmod -R g+w project/         # recursive
Avoid chmod 777. It gives write access to every user on the system. If something does not work, find out who needs which permission and grant it only to that user or group.

Owners: chown and chgrp

sudo chown ana report.txt           # changes the owning user
sudo chown ana:devops report.txt    # user and group at once
sudo chgrp devops report.txt        # only the group
sudo chown -R www-data:www-data /var/www/site   # recursive
newgrp devops                         # opens a shell with devops as the primary group

Special permissions: SUID, SGID and sticky bit

PermissionOctalIn ls -lEffect
SUID4rwsr-xr-xOn an executable: it runs with its owner's permissions. That is how passwd can write to /etc/shadow.
SGID2rwxr-sr-xOn an executable: it runs with its group. On a directory: whatever is created inside inherits the directory's group.
Sticky bit1rwxrwxrwtOn a directory: each user can only delete their own files. This is the case with /tmp.
ls -l /usr/bin/passwd        # -rwsr-xr-x root root ... the s of SUID
ls -ld /tmp                  # drwxrwxrwt root root ... the t of the sticky bit

mkdir shared
chmod 2775 shared        # SGID on a team directory
chmod 1777 shared        # sticky bit, like /tmp
chmod g+s,o+t shared     # SGID and sticky bit in symbolic notation

If the special bit is set but the corresponding execute permission is missing, you will see the letter in uppercase: S or T.

SUID is a risk. A root SUID executable with a bug is a direct path to taking over the machine. Never set it on administration tools such as fdisk to "save yourself" the sudo. To audit your system: find / -perm -4000 -type f 2>/dev/null.

Temporary directories

/tmp is for short-lived temporary files, and many distributions empty it on reboot. /var/tmp is also temporary but survives reboots. Both have the sticky bit.

Hard and symbolic links

Hard links and symbolic links A hard link is a second name pointing to the same inode and data; the data survives until the last name is removed, and it cannot cross filesystems or point to directories. A symbolic link is a small file that stores a path to another name; if the target is removed, the link breaks. Symbolic links can point to directories and across filesystems. HARD LINK · ln SYMBOLIC LINK · ln -s notes.txt copy-hard inode 4021 data on disk two names, one inode · same filesystem · files only shortcut → /etc/hosts /etc/hosts data on disk stores a path · breaks if the target is removed

A hard link is another name for the same data; a symbolic link stores a path. Diagram: The House of Routing.

ln notes.txt hard-copy         # hard link
ln -s /etc/hosts hosts-shortcut    # symbolic link
ln -s /var/www/site web        # symbolic link to a directory
ls -li                          # -i shows the inode number
Hard linkSymbolic link
What it isAnother name for the same inodeA small file that stores a path
If you delete the originalThe data is still accessibleThe link is broken
To directoriesNoYes
Across file systemsNoYes

Remote access with SSH

Servers and cloud machines are administered remotely with SSH (Secure Shell), which encrypts the whole session. Version 2.0 of the exam includes it in this block.

ssh jesus@192.168.1.20              # logs in to another machine
ssh -p 2222 jesus@server.example  # on a port other than 22

ssh-keygen -t ed25519               # creates your key pair (~/.ssh/id_ed25519 and .pub)
ssh-copy-id jesus@192.168.1.20      # installs your public key on the server

scp report.pdf jesus@192.168.1.20:/home/jesus/   # copies a file
sftp jesus@192.168.1.20                           # interactive transfer session

With keys you no longer need a password: the server stores your public key in ~/.ssh/authorized_keys and your private key never leaves your machine.

Protect your private key. Give it a passphrase when you create it, never share it, and check that ~/.ssh has 700 permissions and the private key 600. On servers exposed to the internet, disable password login and direct root login.

Linux Essentials-style challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. What permissions does a file with mode 640 have?

rw-r-----: the owner reads and writes, the group only reads and others have no access.

2. Which file holds the users' encrypted passwords?

/etc/shadow, which only root can read. /etc/passwd is readable by everyone and contains no passwords.

3. Everyone can write to /tmp. What stops one user from deleting another user's files?

The sticky bit (the t in drwxrwxrwt): only a file's owner can delete it.

4. You delete the original file of a symbolic link. What happens to the link?

It breaks, because it only stored the path. With a hard link, the data would still be accessible.

What you take away

  • root (UID 0), system users and regular users; they are stored in /etc/passwd, /etc/shadow and /etc/group.
  • sudo for a single command, with logging; least privilege, always.
  • useradd -m, passwd, usermod -aG and groupadd manage accounts.
  • r=4, w=2, x=1 for user, group and others: chmod 750 is rwxr-x---.
  • SUID, SGID and sticky bit: powerful, and SUID, dangerous.
  • Hard link: same inode. Symbolic: a path that can break.
  • SSH with ed25519 keys, with no passwords and no remote root.

With this you complete the Linux Essentials syllabus. The next step on the path is Docker, where everything you have learned (processes, permissions, networking and the command line) applies to containers.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes for the LPI Linux Essentials certification (2022), updated to version 2.0 of the exam (010-200). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official documentation from LPI, GNU and the distributions.

Navigate