secure linux

A freshly installed Linux server might feel clean, fast, and ready for production—but from a security perspective, it’s also at its most vulnerable.

Over the years, I’ve built and supported Linux servers across web hosting environments, internal business systems, and security-sensitive networks. One pattern always repeats itself: compromised servers are rarely hacked months later—they’re usually hit within days of being exposed.

Automated scanners don’t care whether your server is “new” or “temporary.” The moment it has an IP address and an open port, it becomes a target.

This guide walks through the exact steps I follow after every fresh Linux install, whether it’s Ubuntu, Debian, CentOS, Rocky Linux, or RHEL. These are not theoretical best practices—they’re lessons learned from production incidents, audits, and clean-ups after things went wrong.


1. Create a Non-Root Sudo User (And Stop Logging in as Root)

One of the most common mistakes I still see—even in professional environments—is admins logging in directly as root.

Yes, Linux allows it.
No, it’s not a good idea.

Why This Matters in the Real World

When root credentials are compromised, there is no containment. Every command has full system impact, and logs often lack clarity on who did what.

Instead, create a dedicated administrative user and grant sudo access:

adduser adminuser
usermod -aG sudo adminuser

Once confirmed working, disable root SSH login entirely:

PermitRootLogin no

From experience: this single change blocks thousands of automated attacks per day on internet-facing servers.


2. Patch Immediately: Don’t Trust the Install Media

Fresh installs are rarely fully patched. ISO images are often weeks—or months—out of date.

I’ve seen servers compromised simply because they were brought online before patching.

Run updates immediately:

# Debian / Ubuntu
sudo apt update && sudo apt upgrade -y

# RHEL / CentOS / Rocky
sudo dnf update -y

Pro Tip

Reboot after kernel updates. Running an unpatched kernel defeats the purpose.


3. Lock Down SSH Before Anything Else

SSH is the front door to your server—and attackers test it relentlessly.

Essential SSH Hardening Steps

Edit /etc/ssh/sshd_config:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes

Switch to key-based authentication only:

ssh-keygen -t ed25519

From real-world logs: once password authentication is disabled, brute-force noise drops to near zero.

Changing the SSH port can reduce log noise, but it’s not a security control by itself—consider it optional, not protection.

Always restart SSH carefully and keep your current session open while testing.


4. Enable a Firewall (Even on “Internal” Servers)

If I had a dollar for every time someone said “It’s behind a firewall already”

Defense in depth matters.

Using UFW (Ubuntu / Debian)

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

Only allow:

  • SSH (custom port if used)
  • HTTP/HTTPS (if required)
  • Application-specific ports—never “just in case”

A local firewall ensures that misconfigured services don’t accidentally expose ports.


5. Install Fail2Ban to Stop Brute-Force Attacks

Fail2Ban is one of the simplest and most effective security tools available.

Install it:

sudo apt install fail2ban
# or
sudo dnf install fail2ban

Enable it immediately:

sudo systemctl enable fail2ban --now

In production environments, I’ve watched Fail2Ban automatically block hundreds of malicious IPs within hours of deployment—without manual intervention.


6. Remove Unused Services and Packages

Every running service increases your attack surface.

List enabled services:

systemctl list-unit-files --type=service

Disable anything you don’t explicitly need:

sudo systemctl disable bluetooth.service

Remove insecure legacy tools:

sudo apt purge telnet ftp rsh -y

Real-world lesson: services you forget about are often the ones that get exploited.


7. Configure Automatic Security Updates (Carefully)

Manually patching servers sounds responsible—until life gets busy.

Debian / Ubuntu

sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades

RHEL-Based Systems

Use dnf-automatic with security-only updates enabled.

In enterprise environments, I’ve seen automatic security patching prevent incidents simply because vulnerabilities were fixed before scanners found them.


8. Harden File Permissions and SSH Directories

Permissions issues are a silent killer in Linux security.

Lock down SSH files:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Audit critical files:

  • /etc/shadow
  • /etc/passwd
  • /etc/sudoers

Set a secure default umask:

umask 027

This prevents new files from being world-readable by default—a mistake I’ve seen lead to credential leaks.


9. Audit Listening Ports and Network Services

You can’t secure what you don’t know is running.

Check active listeners:

ss -tuln

or:

netstat -tulpn | grep LISTEN

If you see a port you don’t recognize, investigate immediately. In incident response cases, unexpected open ports are often the first clue.


10. Install Malware and Rootkit Detection Tools

Linux malware exists—just not in the same way as Windows.

Recommended tools:

  • ClamAV – malware scanning
  • rkhunter or chkrootkit – rootkit detection

Schedule scans via cron and review reports regularly. These tools won’t replace monitoring, but they provide valuable visibility.


11. Enable Logging, Auditing, and Alerts

Logs are useless if no one looks at them.

At minimum:

For production systems, enable:

  • auditd
  • logwatch
  • SIEM or central logging agents

In post-incident reviews, logs are often the only way to reconstruct what happened.


12. Set Up Backups Before You Need Them

Security isn’t just prevention—it’s recovery.

Automate backups using:

  • rsync
  • BorgBackup
  • Snapshot-based solutions

Store backups off the server, test restores, and document recovery steps. Ransomware incidents hurt less when backups actually work.


13. Enable SELinux or AppArmor (Don’t Skip This)

Mandatory access controls provide powerful protection—when enabled.

  • Ubuntu / Debian: AppArmor
  • RHEL / CentOS: SELinux (enforcing mode)

Yes, they require tuning.
Yes, they sometimes break poorly written apps.
But in real breaches, I’ve seen SELinux prevent full system compromise even when an application was exploited.


Bonus: Physical and Firmware Security Still Matters

If attackers have physical access, software defenses only go so far.

  • Set BIOS/UEFI passwords
  • Disable booting from external media
  • Enable Secure Boot where possible

Threat models don’t end at the OS.


Final Thoughts: Secure First, Deploy Second

Hardening a Linux server after it’s already exposed is like installing locks after a break-in.

From decades in IT—helpdesk to sysadmin to security—I can confidently say this:
Most Linux compromises are preventable with basic, consistent hardening.

Start secure. Stay patched. Monitor continuously.
And revisit your configuration regularly—because security is a process, not a checkbox.

Leave a Reply

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