Ravindra BagaleCourses & study guides Track your progress

Guides

How to Harden a Linux Server (Checklist)

Hardening a Linux server means shrinking what attackers can use: update packages, lock down SSH, enable a host firewall after allowing SSH, remove unused services, fix permissions (never world-writable shortcuts like chmod 777), add logging and backups. Work from a checklist so you do not lock yourself out.

Friends, a new EC2 / VPS is often a bit open by default. Hardening means shrinking the attack surface. Today’s checklist is vertical: update → SSH → firewall → services → permissions → logs → backup. Commands for Ubuntu and Amazon Linux — pick your OS.

Quick answer

Baseline order (do not skip ahead of SSH allow rules):

  1. Apply OS updates and reboot if the kernel requires it.
  2. Prefer SSH keys; disable password login for SSH when keys work.
  3. Disable direct root SSH login; use sudo from a named user.
  4. Configure the host firewall; allow SSH before enable.
  5. Remove or stop packages/services you do not need.
  6. Set sensible ownership and modes on web content (for example directories 755, files 644 — not 777).
  7. Configure time sync, logging, and unattended security updates where appropriate.
  8. Snapshot / backup before major changes.

Amazon Linux 2023 sketch:

sudo dnf update -y
sudo systemctl enable --now chronyd
# SSH: edit /etc/ssh/sshd_config.d/ then:
sudo systemctl reload sshd
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Ubuntu sketch:

sudo apt update && sudo apt upgrade -y
sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw enable
sudo ufw status

What do I need before this guide?

  • SSH access to a lab or production server you are authorised to change.
  • Console / cloud “serial” access in case a firewall mistake happens.
  • A second terminal open before you reload SSH or enable a firewall.

What does the hardening flow look like?

Linux hardening checklist order Update, lock SSH, firewall, remove unused services, then watch logs. Order matters so you do not lock yourself out. 1. Update packages 2. Harden SSH (keys, no root login) 3. Host firewall (allow SSH first) 4. Disable unused services 5. Permissions, logging, backups Smaller attack surface

Harden in order: update packages, lock SSH, allow SSH in the firewall before enabling it, remove unused services, then permissions, logs and backups.

  1. Patch known vulnerabilities in packages.
  2. Make remote login hard to brute-force (keys, no root login).
  3. Block unexpected inbound ports on the host itself (and keep cloud security groups tight too).
  4. Turn off software you are not using.
  5. Keep audit trails and a restore path.

How do I harden the server (checklist steps)?

Step 1 — Update and inventory

  1. Run the OS update command for your family (dnf / apt).
  2. Reboot if a new kernel was installed (schedule a window).
  3. List listening ports:
sudo ss -tlnp
  1. Write down which ports should remain public.

Step 2 — SSH hardening

  1. Confirm your key login works in a second session before disabling passwords.
  2. Set in sshd config (via a drop-in file when possible):

    • PasswordAuthentication no (after keys work)
    • PermitRootLogin no
    • Optional: allow only specific users
  3. Reload sshd.

  4. Keep cloud security group SSH limited to your IP / bastion — see related AWS guides.
  5. Optional: install fail2ban or use cloud WAF / rate controls for exposed services.

Step 3 — Host firewall (order matters)

  1. Install/enable firewalld (many RHEL-like) or ufw (Ubuntu).
  2. Allow SSH first.
  3. Allow only the application ports you need (80/443 common for web).
  4. Enable the firewall.
  5. Re-test SSH from your laptop before closing the console.

Ravindra Bagale's Tip

💡 On Ubuntu, if you run ufw enable before ufw allow OpenSSH, you can lock yourself out. Many students have done it. Same idea on Amazon Linux / security groups: allow access first, then restrict. Order = safety. Keep a second session and the cloud console ready.

Step 4 — Packages, users and permissions

  1. Remove unused daemons.
  2. Create named sudo users; avoid shared root passwords.
  3. For web roots, use the service user ownership with tight modes — do not use chmod 777.
  4. Find world-writable surprises carefully in lab:
# Lab only — review carefully before changing production
sudo find /var/www -perm -0002 -type f 2>/dev/null | head

Step 5 — Logging, time and backups

  1. Ensure chrony/ntp time sync (logs and TLS need correct time).
  2. Confirm journalctl / log files rotate; ship logs if you have a SIEM.
  3. Take an AMI / snapshot or other backup before big upgrades.
  4. Document open ports and owners in a short notes file.

How do I fix common hardening mistakes?

Ghabru naka 😅 — these are the usual ones:

Symptom Likely cause Fix
SSH times out after ufw enable SSH not allowed before enable Use provider console; ufw allow OpenSSH then reload
Permission denied (publickey) after reload Password auth off but key not installed Console access; fix ~/.​ssh/​authorized_​keys modes (700 / 600 style — not 777)
Website 403 after tightening perms Web user cannot read files Fix owner to the web user; dirs 755, files 644
Still scanned on unused ports Package still listening + SG open Stop service; remove package; close security group port
Time skew / TLS errors chrony not running Enable chrony/ntp; check timedatectl

Try it at home

On a lab VM only, complete and tick:

  1. Updated today:
  2. SSH keys only:
  3. Firewall status pasted:
  4. ss -tlnp reviewed:
  5. Snapshot taken:

Got it? Update, SSH keys, firewall (allow SSH first), unused services off, tight permissions, logs + backup. No chmod 777. Follow the checklist and the server stays much calmer. Next, see the Nmap lab guide with ethical framing.

Frequently asked questions

What does hardening mean?

Reducing attack surface: fewer open services, safer remote access, tighter permissions and better visibility.

Why allow SSH before enabling ufw?

Otherwise you can lock yourself out. Allow OpenSSH first, then enable.

Is chmod 777 ever recommended here?

No. It makes files writable by everyone on the system. Use proper ownership with tighter modes.

Do I still need cloud security groups?

Yes. Host firewall and cloud security groups are complementary layers.

Which OS commands does this cover?

Examples for Amazon Linux (dnf, firewalld) and Ubuntu (apt, ufw), plus shared SSH guidance.

Where are the deeper lessons?

Cyber Security Part 11 — Linux and network hardening.