Skip to content

Beginner · Systems and cryptography · topic 11 of 43 · 35 min

Linux security basics

Explanation

Most servers, cloud machines and security tools run Linux. Its security rests on users and groups, file permissions (read, write, execute for owner, group and others), the all-powerful root account used carefully through sudo, regular updates, and logs in /var/log.

Why Linux

Most web servers, cloud machines, network devices and security tools run Linux. As a defender you will log in to Linux servers to read logs, check users and look for signs of attack. You do not need to be an expert admin, but you must be comfortable with the basics.

Users, groups and root

Every person and many services have their own user account. Users can belong to groups, which share permissions. The special user root (user ID 0) can do anything on the system. Because root can do anything, attackers want it, and admins avoid logging in as root directly. Instead, normal users run single commands as root with sudo ("superuser do"), and every sudo use is logged.

bash
$ id
uid=1000(asha) gid=1000(asha) groups=1000(asha),27(sudo)
$ sudo -l
User asha may run the following commands on web01:
    (ALL : ALL) ALL
id shows your user and groups. Membership of the sudo group means asha can run any command as root.

User accounts are listed in /etc/passwd (readable by everyone, no passwords inside). Password hashes are kept in /etc/shadow, which only root can read.

File permissions

Every file has an owner and a group, and three sets of permissions: for the owner, for the group, and for everyone else (others). Each set has r (read), w (write) and x (execute, or "enter" for a folder).

bash
$ ls -l /etc/passwd /etc/shadow deploy.sh
-rw-r--r-- 1 root root   2981 Mar  2 10:14 /etc/passwd
-rw-r----- 1 root shadow 1630 Mar  2 10:14 /etc/shadow
-rwxr-x--- 1 asha devops  412 Mar  9 18:40 deploy.sh
The first column shows the type (- for a file) and then three groups of rwx for owner, group and others.

Permissions are often written as three digits. Each digit adds r = 4, w = 2, x = 1. So rwx = 7, r-x = 5, r-- = 4, --- = 0. The file deploy.sh above is rwxr-x--- = 750.

NumberMeaningTypical use
600Owner read and write onlySSH private keys, secrets
640Owner rw, group readConfig files, /etc/shadow
644Owner rw, everyone readNormal web files
750Owner all, group read and runTeam scripts
755Owner all, everyone read and runPrograms
777Everyone can do everythingAlmost never correct
bash
$ chmod 600 ~/.ssh/id_ed25519
$ find /var/www -perm -0002 -type f
/var/www/html/uploads/config.php
chmod sets permissions. find -perm -0002 lists world-writable files: anyone could change them, which is a finding to fix.

Hardening basics

  • Update often: sudo apt update && sudo apt upgrade on Ubuntu/Debian, or sudo dnf upgrade on Red Hat family systems. Most break-ins use known, already-fixed bugs.
  • SSH: use key-based login, set PasswordAuthentication no and PermitRootLogin no in /etc/ssh/sshd_config.
  • Remove software and services you do not need.
  • Give each person their own account; never share root.
  • Turn on a host firewall (ufw or firewalld).

Where Linux keeps security logs

On Ubuntu and Debian, logins and sudo use go to /var/log/auth.log. On Red Hat, Rocky and Alma Linux it is /var/log/secure. Systems with systemd also keep a journal you read with journalctl.

bash
$ sudo grep "Failed password" /var/log/auth.log | tail -2
Mar 14 02:11:42 web01 sshd[2231]: Failed password for invalid user admin from 198.51.100.23 port 51514 ssh2
Mar 14 02:11:45 web01 sshd[2233]: Failed password for root from 198.51.100.23 port 51522 ssh2
Failed SSH logins. "invalid user" means the username does not even exist: someone is guessing.

Common mistakes

  • chmod 777 "to make it work". It lets any user, or any hacked web app, change the file. Find the right owner and use the smallest permission.
  • Logging in as root over SSH with a password. Use keys and sudo.
  • Ignoring updates on "internal" servers. Attackers who get in move to the weakest machine.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Linux security basics · 1/7

Linux runs the servers

Most servers and security tools run Linux, so defenders need the basics.

Prefer a video by a teacher? Find free videos on “linux file permissions chmod sudo explained for security beginners” on YouTube ↗

In real life: A world-writable upload folder

A college website on Linux let anyone upload assignments into a folder set to 777, and the web server would also run PHP files from that folder. An attacker uploaded a script and took over the site. The fix: uploads stored outside the web root, permissions set to 750 owned by the right user, and the web server told never to execute files in the upload folder.

Practice (0/3 done)

Task 1 · Question

What permission number gives the owner read and write, and nobody else anything?

Task 2 · Code · Python

Write to_octal(perms) that turns a 9-character permission string like "rwxr-x---" into its 3-digit number string "750". Each group of three adds r=4, w=2, x=1.

Task 3 · Code · Python

Write world_writable(files) where files is a dict of path → permission string (9 characters). Return a sorted list of the paths where "others" can write (the 8th character, index 7, is "w").

Quick quiz

Question 1

1.Which file holds Linux password hashes?

Question 2

2.Why use sudo instead of logging in as root?

Question 3

3.What does chmod 755 give "others"?

Question 4

4.Which sshd_config line stops direct root logins over SSH?

Question 5

5."Failed password for invalid user oracle from 203.0.113.9" means…

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which file holds Linux password hashes?

Answer: /etc/shadow

/etc/shadow holds the hashes and is readable only by root (and the shadow group).

Why use sudo instead of logging in as root?

Answer: Each admin action is tied to a named user and logged

Named users plus logging gives accountability and fewer mistakes.

What does chmod 755 give "others"?

Answer: Read and execute

The last digit 5 = 4 (read) + 1 (execute).

Which sshd_config line stops direct root logins over SSH?

Answer: PermitRootLogin no

PermitRootLogin no is the standard OpenSSH setting.

"Failed password for invalid user oracle from 203.0.113.9" means…

Answer: Someone tried a username that does not exist on this server

"invalid user" means the account is not on the system: typical of automated guessing.

Where are user accounts listed on Linux?Related

Answer: /etc/passwd

/etc/passwd lists accounts; password hashes are in /etc/shadow.

From Users, groups and permissions

Which command keeps showing new lines of a log file as they are written?Related

Answer: tail -f app.log

tail -f follows the file and prints new lines as they arrive.

From Linux basics for backend developers

On RHEL 9, sshd_config includes sshd_config.d/*.conf at the top. Which value wins if both set PasswordAuthentication?Related

Answer: The first one read

sshd uses the first value it obtains.

From SSH: keys, ssh-copy-id and hardening sshd

A file is owned by asha with mode 604 (rw----r--). asha is also in the file's group. Can asha read it?Related

Answer: Yes, as owner

The owner set rw- applies to asha, so she can read and write.

From File permissions: rwx, chmod and chown

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “Systems and cryptography” before its exam: Windows security basics, Hashing: fingerprints for data, Symmetric and asymmetric encryption, Certificates and PKI.
  2. Module 3 exam: Systems and cryptography · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Identity and network defence. 43 topics left in the course before the final exam.

← Previous: HTTP, HTTPS and TLS