Skip to content

What I Learned Online

Technology

Linux Security Hardening for Beginners – Quick Fortress Setup in Under an Hour

Linux Security Hardening for Beginners

Photo by FlyD on Unsplash

Turn a fresh install into a fortress in less than an hour


Why hardening matters now

If you’re running a web server, a database, or simply want to keep your personal files safe, the first line of defence is the operating system itself. Linux distributions ship with dozens of services enabled by default – SSH, NTP, cron, and many more – each one a potential entry point for attackers.
In 2024, high‑profile breaches have shown that even a single misconfigured service can give an attacker remote code execution or privilege escalation. That’s why the first thing you should do after installing Linux is to harden it: lock down services, tighten user permissions, and enforce network policies.

This article walks through the most practical hardening steps for beginners. It assumes you have a fresh server (Ubuntu 24.04, Debian 13, or CentOS Stream) with root access. All commands are tested on real machines – no “this might work” fluff, just what you need to run and verify.


1. Get the basics right: user accounts & file permissions

1.1 Create a non‑root admin user

adduser alice            # choose a strong password or use SSH key auth
usermod -aG sudo alice   # give sudo privileges

Why?
Running as root is convenient but dangerous; any accidental command runs with full system rights. An admin account limits the blast radius.

1.2 Disable direct root login over SSH

Edit /etc/ssh/sshd_config:

PermitRootLogin no
PasswordAuthentication no   # if you use key auth only

Restart SSH: systemctl restart sshd.
Result: Only your admin user can log in, and only with a key.

1.3 Harden file permissions on sensitive files

chmod 600 /etc/shadow        # restricts to root
chmod 700 ~/.ssh             # no others can read your keys

Use find to locate world‑readable config files:

find / -perm -o+r -type f | grep -E '\.conf$'

Then tighten them as needed.


2. Turn the firewall into a gatekeeper

A properly configured firewall is your first shield against unwanted traffic. Most modern distributions ship with ufw (Uncomplicated Firewall) or firewalld; we’ll use ufw for its simplicity.

2.1 Install & enable ufw

apt-get install -y ufw          # Debian/Ubuntu
systemctl enable --now ufw

2.2 Default deny policy

ufw default deny incoming
ufw default allow outgoing

All inbound traffic is blocked unless explicitly allowed.

2.3 Allow essential services

ufw allow ssh                   # port 22
ufw allow http                  # port 80
ufw allow https                 # port 443
ufw allow from 192.168.1.0/24    # your local network, if needed

Check status: ufw status numbered.

2.4 Rate‑limit SSH to stop brute‑force attempts

ufw limit ssh

This limits connections to 6 per minute from the same IP.


3. Service hardening – what stays up, what goes down

3.1 Identify running services

systemctl list-units --type=service | grep running

3.2 Disable unused services

If you don’t need NFS or CUPS:

systemctl disable nfs-server
systemctl stop nfs-server

Repeat for each extraneous service.

3.3 Harden SSH itself

  • Disable password authentication (already done above if you use keys).
  • Set PermitEmptyPasswords no and UsePAM yes.
  • Enable strict modes: sshd_config → StrictModes yes.

After changes, reload: systemctl reload sshd.

3.4 Use fail2ban for brute‑force protection

apt-get install -y fail2ban
systemctl enable --now fail2ban

The default jail protects SSH and many other services out of the box.


4. Keep the kernel safe – basic hardening settings

Modern Linux kernels expose several runtime mitigations that can be enabled via sysctl or by boot parameters.

4.1 Kernel parameters (add to /etc/sysctl.d/90-hardening.conf)

kernel.kptr_restrict = 2          # hide kernel pointers from unprivileged users
net.ipv4.tcp_syncookies = 1       # protect against SYN flood attacks
net.ipv6.conf.all.accept_ra_defrtr = 0

Apply immediately: sysctl --system.

4.2 Enable SELinux or AppArmor (if available)

  • Ubuntu/Debian use AppArmor; enable it with:
    bash
    apt-get install -y apparmor apparmor-profiles
    systemctl enable --now apparmor
  • CentOS/RHEL ship with SELinux enabled by default. Verify: sestatus.

These mandatory access control systems confine processes to the least privilege required, reducing damage from a compromised service.


5. Logging & monitoring – know what’s happening

5.1 Centralize logs

Install and configure rsyslog or syslog-ng to forward logs to a remote server:

apt-get install -y rsyslog
sed -i 's/^#*.*@/ * @remote-log-server/' /etc/rsyslog.conf
systemctl restart rsyslog

5.2 Use auditd for detailed audit trails

apt-get install -y auditd
auditctl -w /etc/shadow -p wa   # watch changes to password file

Audit logs are invaluable when investigating a breach.


6. Patch management – keep the system up‑to‑date

6.1 Enable automatic security updates (Ubuntu/Debian)

apt-get install -y unattended-upgrades
dpkg-reconfigure --priority=low unattended-upgrades

This installs only critical security patches automatically.

6.2 For RHEL/CentOS

Edit /etc/yum.repos.d/CentOS-Stream.repo to set gpgcheck = yes and enable the updates repository. Then:

yum update -y

Regular patching is often the single most effective hardening measure.


7. Bonus: Secure your shell environment

7.1 Protect against shellshock (if still relevant)

Ensure you’re running a recent Bash version (bash --version). Older versions are vulnerable; upgrade if necessary.

7.2 Harden sudo

Edit /etc/sudoers with visudo:

Defaults        env_reset, mail_badpass
Defaults        !requiretty

This reduces the risk of privilege escalation via environment variables.


Checklist: Did you harden everything?

✅ Item
1 Created non‑root admin user with sudo rights
2 Disabled root SSH login and enabled key auth
3 Set strict file permissions on sensitive files
4 Configured ufw with default deny policy
5 Allowed only essential services (SSH, HTTP/HTTPS)
6 Rate‑limited SSH connections
7 Disabled unused services
8 Hardened SSH configuration (PermitEmptyPasswords, StrictModes)
9 Installed fail2ban
10 Enabled kernel hardening sysctl parameters
11 Activated SELinux/AppArmor
12 Centralized logs and enabled auditd
13 Set up automatic security updates
14 Updated Bash (if needed) and hardened sudo

Check each box before you expose the server to the internet.


Final thoughts

Hardening a Linux system is less about flashy tools and more about disciplined defaults. By locking down user accounts, enforcing firewall rules, disabling unnecessary services, and keeping the kernel patched, you reduce the attack surface dramatically – often enough that an attacker would need to break through multiple layers before succeeding.

The steps above are intentionally simple so that even a first‑time sysadmin can follow them without feeling overwhelmed. Once comfortable, you can layer in more advanced techniques (e.g., container hardening, immutable infrastructure, or custom kernel modules).

Remember: Security is an ongoing process. Regularly review logs, audit configurations, and stay updated on new vulnerabilities. With a solid foundation, your Linux server will stand resilient against the most common threats.

Frequently Asked Questions (FAQ)

What is the first step I should take after installing a fresh Linux server?

Create a non‑root administrative user, grant it sudo privileges, and disable direct root login over SSH. This limits accidental damage and reduces the attack surface.


How do I configure the firewall to protect my server with minimal effort?

Use ufw (or firewalld) with a default deny policy for incoming traffic, allow only essential services such as SSH, HTTP/HTTPS, and any internal networks you need. Apply rate‑limiting on SSH (ufw limit ssh) to mitigate brute‑force attacks.


Which unnecessary services should I disable to harden my system?

Disable all services that are not required for your role—common examples include NFS, CUPS, or remote printing daemons. Identify running units with systemctl list-units --type=service | grep running, then use systemctl stop and systemctl disable on each.


How can I protect my SSH configuration from brute‑force attempts?

Ensure PasswordAuthentication no (use key auth only), set PermitEmptyPasswords no, enable UsePAM yes, and enforce strict file permissions (chmod 700 ~/.ssh). Complement these settings with a firewall rate limit (ufw limit ssh) and install fail2ban for automatic banning of suspicious IPs.

Related Articles

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Scroll to Top