Introduction
Securing a Linux server is critical to protecting your business from automated brute-force attacks, unauthorized access, data breaches, and malware. Whether you are running Ubuntu, Debian, AlmaLinux, or Rocky Linux, deploying default installations leaves standard ports open to continuous network scans.
In this guide, we will cover 10 essential Linux server hardening steps to secure your server infrastructure and prevent unauthorized entry.
Step 1: Update Your Server Packages
Outdated packages contain known vulnerabilities that attackers can exploit easily. Run regular system updates:
- Ubuntu / Debian:Bash
sudo apt update && sudo apt upgrade -y - RHEL / AlmaLinux / Rocky Linux:Bash
sudo dnf update -y
Step 2: Disable Root Login via SSH
Logging into a server directly as root gives full system access without an audit trail. Create a dedicated non-root user with administrative privileges instead.
- Create a new user and add them to the sudo group:Bash
adduser appuser usermod -aG sudo appuser - Disable root login in SSH configuration (
/etc/ssh/sshd_config):PlaintextPermitRootLogin no
Step 3: Enforce SSH Key Authentication
Disable password authentication entirely to prevent brute-force login attempts.
- Generate an SSH key pair on your local machine:Bash
ssh-keygen -t ed25519 - Copy the public key to your Linux server:Bash
ssh-copy-id appuser@YOUR_SERVER_IP - In
/etc/ssh/sshd_config, disable password logins:PlaintextPasswordAuthentication no - Reload the SSH daemon:Bash
sudo systemctl reload sshd
Step 4: Change the Default SSH Port
Automated bots continuously scan port 22 for SSH targets. Changing your SSH port reduces random scan noise significantly.
- Open
/etc/ssh/sshd_configand changePort 22to a high-range port (e.g.,2288):PlaintextPort 2288 - Ensure your firewall allows traffic on the new port before restarting SSH!
Step 5: Configure a Firewall (UFW or Firewalld)
Only open network ports that are actively required for your services (e.g., HTTP/HTTPS and your custom SSH port).
- For UFW (Ubuntu/Debian):Bash
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow 2288/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable - For Firewalld (RHEL/CentOS/AlmaLinux):Bash
sudo firewall-cmd --permanent --add-port=2288/tcp sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Step 6: Install Fail2ban to Block Brute-Force Attacks
Fail2ban monitors login logs and automatically blocks IP addresses showing suspicious activity (like too many failed password attempts).
- Install Fail2ban:Bash
sudo apt install fail2ban -y # Ubuntu/Debian sudo dnf install fail2ban -y # RHEL/AlmaLinux - Enable and start the service:Bash
sudo systemctl enable --now fail2ban
Step 7: Remove Unused Services & Open Ports
Unnecessary background services increase your attack surface. Check active listening ports with:
Bash
ss -tulpn
Stop and disable any service you do not recognize or require:
Bash
sudo systemctl stop service_name
sudo systemctl disable service_name
Step 8: Set Up Automated Security Updates
Ensure critical security patches apply automatically so your system stays protected against zero-day exploits.
- On Ubuntu/Debian: Install
unattended-upgrades:Bashsudo apt install unattended-upgrades -y sudo dpkg-reconfigure --priority=low unattended-upgrades - On RHEL/AlmaLinux: Enable
dnf-automatic:Bashsudo dnf install dnf-automatic -y
Step 9: Enforce Strict File Permissions
Ensure world-writable permissions are removed and sensitive files like /etc/shadow and /etc/passwd are properly restricted.
- Protect SSH directory:Bash
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
Step 10: Enable Monitoring & Log Auditing
Regularly check /var/log/auth.log (Debian/Ubuntu) or /var/log/secure (RHEL/AlmaLinux) for unauthorized access attempts.
Using proactive server monitoring tools like Nagios, Zabbix, or Datadog alerts your engineering team instantly when unexpected activity or CPU spikes occur.
Need Help Securing Your Cloud Infrastructure?
Server security requires ongoing vigilance and audit updates. At CoreServerSupport, our certified Linux administrators provide 24/7 server monitoring, firewall configuration, automated backup management, and routine security audits to keep your infrastructure safe.
Contact CoreServerSupport Todayfor a free security audit!