Linux Security & Hardening: Permissions, SSH, Firewalls, and System Protection

11 min read

How Linux Protects Itself and How Administrators Make It Safer.

Linux is famous for stability, flexibility, and power; but none of that matters if the system cannot be trusted.

This tutorial focuses on how Linux enforces security by design, not as an afterthought. Linux does not rely on antivirus tools, external scanners, or bolt‑on security products. Instead, the operating system itself is built to:

  • Limit damage when failures or misconfigurations occur
  • Prevent unauthorized access
  • Enforce least‑privilege access
  • Harden itself for real production environments
  • Detect suspicious activity
  • Protect remote access
  • Restrict what processes can do

This is the point where learners move beyond simply running commands and begin protecting systems.

It’s where beginners start thinking like administrators.

Security is not a checklist.

Security is not a tool.

Security is a mindset.

Every Linux system is built on four pillars:

But there is a fifth pillar, one that determines whether a system survives the real world: Trust

Trust is the foundation of Linux security.

In this guide, security is not treated as a checklist or an afterthought. Instead, it answers the core questions every administrator must be able to explain:

  • Who is allowed to do what on the system?
  • What is permitted to run and what is not?
  • What happens when something goes wrong?
  • How does Linux protect itself from mistakes, attacks, or misconfigurations?

This chapter is about understanding those answers and applying them with confidence in real systems.

WHAT IS LINUX SECURITY?

Linux security is a layered model, enforced by the kernel and configured by administrators. It combines:

  • User & permission controls: who can read/write/execute
  • Process isolation: processes don’t share memory/state by default
  • Network restrictions: inbound/outbound traffic control
  • Service hardening: run with minimal privileges and capabilities
  • Auditing & logging: detect misuse and investigate incidents
  • Mandatory Access Control (MAC): SELinux/AppArmor policies that restrict processes even if compromised

There is no single “secure” command. Security comes from correct configuration and a mindset of limiting blast radius.

Lab: Check Your System’s Security Layers

Goal: This gives you a snapshot of:

  • Identity — who you are
  • Permissions — what you can access
  • Firewall — what traffic is allowed
  • Security modules — SELinux/AppArmor status

Run the following commands:

whoami
groups
ls -l /
sudo ufw status
getenforce 2>/dev/null || aa-status 2>/dev/null

What you’ll see:

  • Your user and group memberships (affects access)
  • Root directory permissions (sanity check for obvious misconfigurations)
  • UFW firewall status (Ubuntu/Debian)
  • SELinux mode (Enforcing/Permissive/Disabled) or AppArmor profiles (Loaded/Enforced/Complain)

Verify: If SELinux/AppArmor and the firewall are both disabled, note it you’ll harden them later.

Principle of Least Privilege (Core Concept)

The most important security rule:

Every user, process, and service should have only the access it needs and nothing more.

Linux enforces least privilege with:

  • Users & groups
  • File permissions (r/w/x, ownership)
  • sudo rules (limited elevation)
  • Service isolation (systemd, chroot, containers, namespaces)
  • MAC policies (SELinux/AppArmor)

Most breaches become severe breaches because least‑privilege wasn’t enforced.

Lab: Identify Users with Excessive Privileges (sudo Access)

grep 'sudo' /etc/group
getent group sudo

On RHEL‑family systems, the admin group might be wheel:

getent group wheel

Expected: Only intended admins appear here.

Lab: Remove Unnecessary sudo Access

sudo gpasswd -d username sudo     # Debian/Ubuntu
sudo gpasswd -d wheel     # RHEL/CentOS/Fedora

This reduces the blast radius of mistakes or compromises.

Verify:

id 
sudo -l -U username 2>/dev/null || echo "No sudo privileges"

Rollback (if needed):

sudo usermod -aG sudo victor     # or wheel on RHEL-family

File & Directory Security

Permissions control:

  • Read files (r): Who can read files
  • Write/modify (w): Who can modify them
  • Execute programs (x): Who can execute programs

Critical system files are protected by:

  • Root ownership
  • Strict permission modes: (e.g., /etc/shadow is -rw------- root-only)
  • Limited write access for normal users

Linux assumes users will make mistakes so permissions limit damage.

Lab: Inspect Critical System File Permissions

ls -l /etc/passwd
ls -l /etc/shadow
stat /etc/passwd /etc/shadow
``

Expected:

  • /etc/passwdworld‑readable but not world‑writable (needed for username lookups)
  • /etc/shadowroot‑only (contains password hashes)

Expected octal permissions:

  • /etc/passwd644
  • /etc/shadow400 or 640 (depending on distro)

Lab: Harden a Sensitive File

sudo touch /root/secrets.txt #Creates an empty file that is owned by root

#print the text and writes it into the file as root then output is suppressed with >/dev/null
#This avoids permission errors you would get if tried echo > file without root
echo "TOP-SECRET" | sudo tee /root/secrets.txt >/dev/null

#This ensures the file is owned by root and prevents accidental access by other users or groups
sudo chown root:root /root/secrets.txt

#Lock down permissions - Owner: read/write, Group: no access, Others: no access
#This is the correct mode for sensitive files (same as /etc/shadow)
sudo chmod 600 /root/secrets.txt

This means:

  • Only root can read or modify it
  • No other user can even open it
  • Perfect for secrets, tokens, or lab exercises

Verify:

ls -l /root/secrets.txt
#perfect way to test file-permission hardening.
sudo -u nobody cat /root/secrets.txt || echo "Access correctly denied"

  • nobody is the most restricted account on the system
  • /root is only accessible to root
  • /root/secrets.txt has permissions 600 (root‑only)

So the read attempt

  • nobody is the most restricted account on the system
  • /root is only accessible to root
  • /root/secrets.txt has permissions 600 (root‑only)

So the read attempt must fail.

Tip: For shared project directories, consider setgid on directories to keep group ownership consistent:

#Change the group ownership - the /srv/project now belongs to the group dev
sudo chgrp dev /srv/project

#apply secure, collaborative permissions
sudo chmod 2770 /srv/project

Bit Meaning
2 (setgid) New files inherit the directory’s group (dev)
7 (owner) Full access (rwx)
7 (group) Full access (rwx)
0 (others) No access

🔐 Why this is powerful

  • Every file created inside /srv/project automatically belongs to the dev group
  • No accidental “wrong group” issues
  • No outsiders can read or write
  • Perfect for team collaboration, dev groups, or shared service directories

Lab: Find World‑Writable Files (Dangerous)

sudo find / -type f -perm -0002 2>/dev/null

Meaning:

  • / → search the entire filesystem
  • -type f → only files (not directories)
  • -perm -0002 → files that are world‑writable
  • 2>/dev/null → hide permission‑denied errors

World‑writable files are dangerous because any user can modify them, which opens the door to:

  • privilege escalation
  • tampering
  • malicious code injection
  • log poisoning
  • service hijacking

This command helps you spot them.

SSH Security: Protecting Remote Access

SSH is the most common entry point and the most targeted. Most compromises arise from weak SSH configuration, not kernel flaws.

Core SSH hardening:

  • Disable root login
  • Restrict which users can connect
  • Restricting authentication methods
  • Enforce key‑based auth (avoid password logins)
  • Use firewalls to limit SSH exposure
  • Log & monitor authentication attempts

Lab: Disable Root Login & Enforce Polic

Edit SSH config:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F)
sudo nano /etc/ssh/sshd_config

Set or ensure:

PermitRootLogin no
PasswordAuthentication no        # if you have keys in place
PubkeyAuthentication yes
ChallengeResponseAuthentication no
UsePAM yes
LoginGraceTime 30
MaxAuthTries 3
AllowUsers victor adminuser

Test config safely:

sudo sshd -t

Restart SSH (and keep your current session open in case of lockout):

sudo systemctl restart ssh

Verify from another terminal:

ssh victor@SERVER
ssh root@SERVER  # should be denied

Tip: If you must keep password auth temporarily, set Fail2ban or equivalent, and use strong passwords. Ultimately, move to key‑only.

Firewalls in Linux (Network Security)

Firewalls reduce exposure by controlling:

  • Which ports are open
  • Who can connect
  • Which protocols/interfaces are allowed

Common layers:

  • UFW: beginner‑friendly, Ubuntu/Debian
  • nftables/iptables: advanced, enterprise‑grade (RHEL, Debian, others)

Lab: UFW Quick‑Start (Ubuntu/Debian)

#Block all unsolicited inbound traffic
#This means no external system can initiate a connection to your machine unless you explicitly allow it.
sudo ufw default deny incoming

#Allow all outbound traffic
#Your system can reach the internet normally (updates, browsing, package installs).
sudo ufw default allow outgoing

#Allow SSH access
#This opens port 22 so you don’t lock yourself out if you’re using SSH.
sudo ufw allow OpenSSH

#Enable the firewall
#Activates UFW with the rules you’ve configured.
sudo ufw enable

#View the full firewall status
sudo ufw status verbose

Shows:

  • Default policies
  • Allowed ports
  • Logging level
  • IPv6 status
  • Whether UFW is active

Expected output (example)
You should see something similar to:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

Verify:

#Check which services are listening on ports 22, 80, 443
ss -tulpn | grep -E ':(22|80|443)\b'

#Check UFW rules with numbering
sudo ufw status numbered

Lab: nftables (Modern Backend) Minimal Policy (Advanced)

Note: Test on a VM or maintenance window.

Create a simple ruleset file:

sudo bash -c 'cat >/etc/nftables.conf' <<'EOF'
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
  chain input {
    type filter hook input priority 0;
    policy drop;

    iif lo accept
    ct state established,related accept
    tcp dport 22 accept
    ip protocol icmp accept
    ip6 nexthdr icmpv6 accept
  }

  chain forward {
    type filter hook forward priority 0;
    policy drop;
  }

  chain output {
    type filter hook output priority 0;
    policy accept;
  }
}
EOF

Load & enable:

sudo nft -f /etc/nftables.conf
sudo systemctl enable --now nftables
sudo nft list ruleset

Rollback:

sudo systemctl stop nftables
sudo nft flush ruleset

Security Modules (SELinux / AppArmor)

Mandatory Access Control (MAC) adds a kernel‑enforced policy layer that limits what processes can access, even if they’re compromised.

  • SELinux (RHEL/CentOS/Fedora)
  • AppArmor (Ubuntu/Debian)

They prevent lateral movement and unauthorized resource access.

Lab: Check Enforcement Mode

SELinux:

getenforce # Enforcing | Permissive | Disabledsestatus # detailed view (if installed)

AppArmor:

sudo aa-status
sudo aa-status | sed -n '1,20p'

Best practice: Aim for Enforcing (SELinux) or profiles in enforce (AppArmor) in production.

Lab: Review & Resolve Denials

SELinux:

sudo journalctl -t setroubleshoot --since "1 hour ago" 2>/dev/null | tail
sudo ausearch -m AVC,USER_AVC -ts recent 2>/dev/null | aureport -av

AppArmor:

sudo dmesg | grep -i apparmor | tail -n 50
sudo aa-logprof # step through suggestions interactively

Tip: If you must switch SELinux to permissive while tuning:

sudo setenforce 0 # temporary (runtime)# Re-enable to test:sudo setenforce 1

Do not leave MAC permanently disabled without a documented reason.

Logging & Auditing (Detection)

Prevention isn’t enough; detection is essential to respond, investigate, and improve.
Linux logs include:

  • Authentication (/var/log/auth.log, /var/log/secure)
  • Systemd journal (journalctl)
  • Kernel warnings and denials
  • Audit logs (if auditd enabled)
  • Service logs (Nginx, SSH, DBs, etc.)

Lab: View Authentication & System Logs

Debian/Ubuntu:

sudo tail -n 100 /var/log/auth.logsudo journalctl -xe

RHEL/CentOS/Fedora:

sudo tail -n 100 /var/log/securesudo journalctl -xe

Lab: Monitor Logs in Real Time

sudo tail -f /var/log/syslog # Debian/Ubuntu
sudo tail -f /var/log/messages # RHEL-family

Lab: Quick Auditd Peek (If Enabled)

sudo ausearch -m USER_AUTH,USER_LOGIN -ts today | aureport -au

Action: Create a habit of reviewing failed logins, sudo attempts, and service errors. Set alerts where possible.

Takeaway: Security Assumes Failure

Linux security is not about paranoia. It’s about engineering for failure:

  • Users will make mistakes.
  • Services will crash.
  • Attackers will try things.
  • Misconfigurations will happen.

Linux is built to limit damage by design. Administrators make it safer by:

  • Enforcing least privilege
  • Hardening SSH
  • Applying host firewalls
  • Enabling SELinux/AppArmor
  • Securing files and services
  • Monitoring logs and audits

This tutorial provides you with the mindset and hands-on skills to protect real systems, not just operate them.

Appendix: Recommended Hardening Checklist (Quick Reference)

  • Remove unnecessary sudo rights; review sudoers regularly
  • Enforce key‑based SSH; disable root login; reduce MaxAuthTries
  • Restrict SSH to specific users or groups (AllowUsers/AllowGroups)
  • Enable firewall (UFW or nftables); default deny inbound
  • Keep SELinux/AppArmor enforcing; resolve denials properly
  • Lock critical file permissions; use 600/640 where appropriate
  • Rotate and monitor logs; set alerts for failures & anomalies
  • Minimize services; disable or mask unused daemons
  • Patch regularly; automate updates with maintenance windows
  • Document configurations and rollback procedures

Environment & Prerequisites (for Labs)

  • A Linux VM or server (Ubuntu 20.04+/22.04+, Debian 11+, RHEL/CentOS 8+/Stream, Fedora 38+)
  • A non‑root user with sudo privileges
  • A terminal with network access
  • SSH keys set up before disabling password authentication
  • Snapshot/backup recommended before firewall/MAC changes

Discover more from Humble Cloud Tech

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Decline
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Suggested text: Our website address is: https://humbletech.cloud.

Comments

Suggested text: When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymised string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service Privacy Policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

Suggested text: If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

Suggested text: If you leave a comment on our site you may opt in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Suggested text: Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

Suggested text: If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

Suggested text: If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognise and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

Suggested text: If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where your data is sent

Suggested text: Visitor comments may be checked through an automated spam detection service.
Save settings
Scroll to Top