Show contents
Chapter 5 of 7 · 20 min
Hardening a server
By the end of this chapter, you'll be able to
- Read and set a file's permissions, including in octal notation
- Explain and set the umask creation mask
- Spot dangerous files: world-writable or setuid
- Apply the principle of least privilege to SSH and sudo
Reminder: permissions in octal
Each file has three sets of permissions: for its owner (u), its group (g) and others (o). Each set combines read (r = 4), write (w = 2) and execute (x = 1). Octal notation adds up these values: rwx = 7, r-x = 5, r-- = 4. So 750 means: everything for the owner, read and execute for the group, nothing for others.
touch report.txt
chmod 640 report.txt
stat -c '%a %A %n' report.txt
chmod u+x,o-r report.txt
stat -c '%a %A %n' report.txt
640 -rw-r----- report.txt
740 -rwxr----- report.txt
stat -c '%a %A %n' prints the permissions in octal, then in letters, then the name.
umask: default permissions
When a program creates a file, it asks for permissions (usually 666 for a file, 777 for a folder), and the umask removes some of them. With the usual mask 022, write access is removed for the group and others: files are born as 644, folders as 755. A server holding sensitive data rather uses 027: nothing for others.
umask 022
touch public.txt
mkdir public-folder
umask 027
touch private.txt
mkdir private-folder
stat -c '%a %n' public.txt public-folder private.txt private-folder
644 public.txt
755 public-folder
640 private.txt
750 private-folder
Spotting dangerous files
Two kinds of files deserve regular checks:
- world-writable files (
o+w): any account, including a hacked service's, can change them; - setuid programs (
u+s): they run with their owner's rights, oftenroot, whoever launches them. A few are normal (passwd,sudo); a newcomer is suspicious.
find -perm finds them: -perm -o+w means "at least write permission for others".
mkdir -p site/images
touch site/index.html site/config.php site/images/logo.png
chmod 644 site/index.html site/images/logo.png
chmod 666 site/config.php
chmod 777 site/images
find site -perm -o+w | sort
site/config.php
site/images
The configuration file (which often holds the database password) and the images folder are writable by everyone: to be fixed right away. On a real server, you also look for setuid files with find / -xdev -perm -4000 -type f 2>/dev/null and compare the list with last week's.
A person only gets the right they need: restarting the site, nothing else.
Text description of the animation
- No site file is writable by other accounts.
- The web server runs under an account that can only write to its own folders.
- People log in with a personal key, never directly as root.
- A person only gets the right they need: restarting the site, nothing else.
SSH and sudo: access kept to the minimum
Access rules are set in configuration files. We do not test them here (it would take a real server), but here are the essential lines of /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
- no direct
rootlogin: each administrator logs in with their own account, which leaves a trace of who did what; - no password: only SSH keys, which cannot be guessed by brute force.
For administration tasks, sudo grants specific rights. A rule in /etc/sudoers.d/ (always edited with visudo, which checks the syntax) can allow a single command:
awa ALL=(root) /usr/bin/systemctl restart site
Your turn
Worked exercise
Worked exercise: fixing the site's permissions
In the site folder created above, fix the permissions so that all files are 644 and all folders 755, without handling files one by one. Then check that nothing is writable by others, and print each item's permissions.
Hint 1
find site -type f -exec chmod 644 {} + handles all files; do the same with -type d for folders.Hint 2
find site -perm -o+w | wc -l must print 0. For the list, find site -exec stat -c '%a %n' {} + | sort -k2.Show the solution and common mistakes
find site -type f -exec chmod 644 {} +
find site -type d -exec chmod 755 {} +
echo "items writable by others: $(find site -perm -o+w | wc -l)"
find site -exec stat -c '%a %n' {} + | sort -k2
items writable by others: 0
755 site
644 site/config.php
755 site/images
644 site/images/logo.png
644 site/index.html
The configuration file could even go down to 640, with the web service's group: only the owner and the service can read it.
Common mistakes:
chmod -R 644 site: folders lose thexpermission, and nobody can enter them any more.chmod -R 755 site: every file becomes executable, which makes it easier to run a booby-trapped file.
Summary
- 1Octal permissions add up read (4), write (2) and execute (1) for the owner, the group and others.
- 2umask removes permissions at creation: 022 gives 644/755, 027 closes everything to others.
- 3find -perm -o+w finds world-writable files; find -perm -4000 finds setuid programs.
- 4Least privilege: services under a dedicated account, SSH with keys and no root, sudo limited to specific commands.
- 5Never chmod -R 777: give the folder to the service's account and keep permissions closed.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.