Optimum Web
Security 12 min read

Linux Server Security Best Practices

OP

Olga Pascal

CEO & Founder

Server security cannot be treated as a one-time deployment task. As threat landscapes evolve with increasingly sophisticated automated attacks, supply chain vulnerabilities, and zero-day exploits, organizations must adopt a proactive, defense-in-depth security posture. This comprehensive guide provides actionable strategies and practical implementations for hardening Linux infrastructure across single-server deployments and large-scale cloud environments.

If your team lacks the time or expertise to implement these measures, our Linux server performance tuning service covers both optimization and security hardening for $149.

1. Implementing Zero Trust Architecture

The perimeter-based security model has become obsolete in modern distributed systems. Zero Trust Architecture (ZTA) operates on the principle of "never trust, always verify," requiring continuous authentication and authorization for every access request, regardless of network location.

1.1 Network Micro-Segmentation

Concept: Isolate services and workloads into distinct security zones to limit lateral movement during a breach.

Implementation with NFTables:

bash
# Flush existing rules
nft flush ruleset

# Create a new table
nft add table inet filter

# Define base chains
nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }
nft add chain inet filter forward { type filter hook forward priority 0 \; policy drop \; }
nft add chain inet filter output { type filter hook output priority 0 \; policy accept \; }

# Allow established connections
nft add rule inet filter input ct state established,related accept

# Allow loopback
nft add rule inet filter input iif lo accept

# Web server can only communicate with database on specific port
nft add rule inet filter input ip saddr 10.0.1.10 tcp dport 5432 accept

# SSH access only from management network
nft add rule inet filter input ip saddr 10.0.0.0/24 tcp dport 22 accept

# Save configuration
nft list ruleset > /etc/nftables.conf
systemctl enable nftables

Implementation with Cilium (Kubernetes environments):

yaml
apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: web-to-db-policy
spec:
  endpointSelector:
    matchLabels:
      app: web-frontend
  egress:
  - toEndpoints:
    - matchLabels:
        app: database
    toPorts:
    - ports:
      - port: "5432"
        protocol: TCP

1.2 Multi-Factor Authentication for SSH

Deploying Google Authenticator:

For businesses needing professional SSH hardening and Linux user management, Optimum Web offers fixed-price implementation starting at $89.

bash
# Install PAM module
sudo apt install libpam-google-authenticator  # Debian/Ubuntu
sudo yum install google-authenticator          # RHEL/CentOS

# Configure PAM
sudo nano /etc/pam.d/sshd

# Add this line at the top:
auth required pam_google_authenticator.so

# Modify SSH configuration
sudo nano /etc/ssh/sshd_config

# Update these directives:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

# Generate tokens for user
google-authenticator

# Restart SSH
sudo systemctl restart sshd

Implementing Hardware Token Authentication (YubiKey):

bash
# Install required packages
sudo apt install libpam-u2f

# Register YubiKey
mkdir -p ~/.config/Yubico
pamu2fcfg > ~/.config/Yubico/u2f_keys

# Configure PAM
sudo nano /etc/pam.d/sshd
# Add:
auth required pam_u2f.so authfile=/home/%u/.config/Yubico/u2f_keys cue

# Configure SSH
sudo nano /etc/ssh/sshd_config
AuthenticationMethods publickey,keyboard-interactive:pam

sudo systemctl restart sshd

⚡ Is Your Linux Server Also Running Slow?

Security hardening is essential — but so is performance. Misconfigured servers waste CPU, leak memory, and respond slowly. Our engineers audit and optimize your Linux infrastructure: kernel tuning, I/O optimization, memory management, and service configuration.

$149 · 2-3 business days · 14-day warranty

Optimize My Server — $149 →

2. Advanced Kernel Hardening

The Linux kernel is the primary target for privilege escalation. Hardening it at the source is vital for neutralizing zero-day exploits.

Kernel Lockdown Mode: Activating the lockdown feature in integrity or confidentiality mode prevents even the root user from modifying kernel code or accessing sensitive memory addresses.

bash
# Check current lockdown status
cat /sys/kernel/security/lockdown

# Enable lockdown at boot (GRUB configuration)
sudo nano /etc/default/grub

# Add to GRUB_CMDLINE_LINUX:
lockdown=confidentiality

# Update GRUB
sudo update-grub  # Debian/Ubuntu
sudo grub2-mkconfig -o /boot/grub2/grub.cfg  # RHEL/CentOS

# Reboot and verify
sudo reboot
dmesg | grep -i lockdown
cat /sys/kernel/security/lockdown

2.2 Linux Kernel Runtime Guard (LKRG)

Deploying technologies like LKRG helps monitor the kernel's integrity in real-time. It acts as a proactive security layer that detects and kills exploit attempts as they happen.

bash
# Install dependencies
sudo apt install build-essential linux-headers-$(uname -r)

# Download LKRG
wget https://github.com/lkrg-org/lkrg/archive/refs/tags/v0.9.7.tar.gz
tar xzf v0.9.7.tar.gz
cd lkrg-0.9.7

# Build and install
make
sudo make install

# Load module
sudo insmod /lib/modules/$(uname -r)/extra/lkrg.ko

# Configure auto-load on boot
echo "lkrg" | sudo tee -a /etc/modules-load.d/lkrg.conf

# Verify LKRG is running
sudo dmesg | grep LKRG
lsmod | grep lkrg

2.3 Kernel Parameter Hardening

Comprehensive sysctl Configuration:

bash
sudo nano /etc/sysctl.d/99-security-hardening.conf

# Network security
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.log_martians = 1

# Kernel hardening
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
kernel.unprivileged_bpf_disabled = 1
kernel.yama.ptrace_scope = 2
kernel.kexec_load_disabled = 1
kernel.randomize_va_space = 2

# Apply settings
sudo sysctl -p /etc/sysctl.d/99-security-hardening.conf

🔒 Need Expert Help Implementing These Security Measures?

Zero Trust, kernel lockdown, NFTables, MFA — implementing production-grade security requires deep Linux expertise. Our senior engineers handle security hardening and performance optimization as part of the same engagement.

  • Linux User Management Setup — $89
  • Daily Website Maintenance — $149/mo
Get Server Optimization — $149 →

3. Mandatory Access Control (MAC)

Standard Linux permissions such as Owner, Group, and Others are often too blunt for modern security needs. To truly isolate processes, you must leverage Mandatory Access Control.

  • SELinux and AppArmor: Do not disable these features. SELinux (standard on RHEL/Fedora) and AppArmor (Ubuntu/Debian) provide granular control. They ensure that even if a service like Nginx is hacked, the attacker remains trapped within a restricted sandbox.
  • Rootless Containers: If you run Docker or Podman, always aim for rootless mode. This ensures that a container breakout does not result in full administrative access to the host machine.

4. Intelligent Threat Detection and Live Patching

Manual log checking is too slow for modern security. You need automation to detect and respond to threats instantly. While Fail2Ban is a classic tool, CrowdSec is a powerful modern alternative. It uses behavior-based detection and a community database of malicious IPs to block attackers before they even reach your login prompt.

Furthermore, high-traffic servers cannot afford downtime for updates. Tools like Canonical Livepatch, kpatch, or KernelCare allow you to apply critical security fixes to the kernel without restarting the server, keeping you protected and online at the same time.

5. The Essential Hardening Checklist

For immediate results, ensure your configuration meets these baseline requirements:

Security MeasureImplementationWhy it matters
Disable Root SSHPermitRootLogin noBlocks direct attacks on the admin account
Change SSH PortUse a port above 1024Stops 90% of automated bot scans
Strict Firewalldefault deny incomingOnly opens the doors you actually use
Auditd Monitoringsystemctl enable auditdRecords every major change for later review
Disk EncryptionLUKS / dm-cryptProtects data if the physical drive is accessed

Security is a process, not a product. Securing a Linux server is an ongoing journey that requires a balance of automated tools and vigilant administration. By shrinking your attack surface and assuming a breach is always possible, you create an environment that is too costly and difficult for intruders to penetrate.

Don't have a dedicated DevOps team? Our infrastructure services cover everything from server optimization to 24/7 monitoring.

🛡️ Your Linux Server Needs Professional Attention

Security hardening + performance optimization. Our senior engineers audit, configure, and document everything. $149 fixed price · 2-3 days · Senior engineers only

Order Server Optimization →
LinuxSecurityServer HardeningSSHFirewall

Frequently Asked Questions

What are the most important Linux server security settings?
The most critical settings are: disable root SSH login (PermitRootLogin no in sshd_config), enable key-based authentication only, configure UFW firewall to allow only necessary ports, enable automatic security updates (unattended-upgrades), and install fail2ban to block brute-force attempts.
How do I harden SSH on a Linux server?
Edit /etc/ssh/sshd_config: set PermitRootLogin no, PasswordAuthentication no, PubkeyAuthentication yes, and change Port from 22 to a non-standard port. Restart SSH with `systemctl restart sshd`. Always test the new config before closing your current session.
What is fail2ban and should I use it?
Fail2ban monitors log files and temporarily bans IPs that show malicious behavior (e.g., too many failed SSH attempts). Yes, you should use it — it significantly reduces brute-force attacks. Install with `apt install fail2ban` and enable the SSH jail in /etc/fail2ban/jail.local.