Beginner · Systems and cryptography · topic 11 of 43 · 35 min
Linux security basics
Explanation
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.
$ 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) ALLUser 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).
$ 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.shPermissions 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.
| Number | Meaning | Typical use |
|---|---|---|
| 600 | Owner read and write only | SSH private keys, secrets |
| 640 | Owner rw, group read | Config files, /etc/shadow |
| 644 | Owner rw, everyone read | Normal web files |
| 750 | Owner all, group read and run | Team scripts |
| 755 | Owner all, everyone read and run | Programs |
| 777 | Everyone can do everything | Almost never correct |
$ chmod 600 ~/.ssh/id_ed25519
$ find /var/www -perm -0002 -type f
/var/www/html/uploads/config.phpHardening 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.
$ 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 ssh2Common 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
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)
What permission number gives the owner read and write, and nobody else anything?
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Windows security basicsSystems and cryptography · same module
- Hashing: fingerprints for dataSystems and cryptography · same module
- Symmetric and asymmetric encryptionSystems and cryptography · same module
- Users, groups and permissionsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Linux basics for backend developersBackend Development · Deployment and scaling
- SSH: keys, ssh-copy-id and hardening sshdLinux and Red Hat (RHCSA to RHCE) · SELinux and SSH
Questions people ask about this
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.
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.
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.
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.
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
- 4 more topics in “Systems and cryptography” before its exam: Windows security basics, Hashing: fingerprints for data, Symmetric and asymmetric encryption, Certificates and PKI.
- Module 3 exam: Systems and cryptography · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Identity and network defence. 43 topics left in the course before the final exam.