Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Harden a New Server from Scratch

A guided project: take a fresh Linux VPS to a safe baseline with a sudo user, SSH keys, firewall, fail2ban, automatic updates, time sync, logging and a verification checklist.

This project ties the chapter together. You have a brand-new virtual server with a public IP address, root access and nothing else. Within the first hour of its life, scanners will find it. Your goal is to turn it into a defensible baseline: a named admin user, key-only SSH, a default-deny firewall, brute-force protection, automatic security updates, accurate time, persistent logs and a way to prove it all works.

The steps are for a real Ubuntu or Debian-style server (RHEL-family equivalents are noted). The site cannot run them, so they are read-only. The two runnable parts are Python stand-ins: a checklist scorer you can adapt to inspect captured command output, and a plan generator that prints the exact command sequence for your answers.

What you will build

flowchart TB
    A["1. Update and set hostname, time"] --> B["2. Admin user with sudo"]
    B --> C["3. SSH keys, harden sshd"]
    C --> D["4. Firewall default deny"]
    D --> E["5. fail2ban"]
    E --> F["6. Automatic updates"]
    F --> G["7. Logging and monitoring"]
    G --> H["8. Verify from outside, snapshot, document"]

Order matters: always keep a working login while you tighten the next layer.

Step 1: update, name and time

Terminal
sudo apt update && sudo apt full-upgrade -y          # dnf upgrade -y on RHEL family
sudo hostnamectl set-hostname web01
sudo timedatectl set-timezone UTC
sudo timedatectl set-ntp true                        # systemd-timesyncd or chrony
timedatectl status                                   # "System clock synchronized: yes"
sudo reboot                                          # if a new kernel was installed

Accurate time matters for log correlation, TLS certificates and TOTP codes.

Step 2: a named admin user

Never administer as root directly.

Terminal
sudo adduser alice                                   # RHEL: useradd -m -s /bin/bash alice; passwd alice
sudo usermod -aG sudo alice                          # RHEL family: group "wheel"
sudo -l -U alice                                     # confirm what alice may run

Step 3: SSH keys and a hardened daemon

On your own computer, create a key and install it (see SSH):

Terminal
ssh-keygen -t ed25519 -C "alice@laptop"
ssh-copy-id alice@SERVER_IP
ssh alice@SERVER_IP        # must work WITHOUT the password

Only after that works, on the server:

Terminal
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf >/dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowUsers alice
MaxAuthTries 3
X11Forwarding no
EOF
sudo sshd -t && sudo systemctl reload ssh            # service name "sshd" on RHEL family

Open a second terminal and log in again before you close the first. Details in SSH Hardening.

Step 4: firewall

Terminal
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp                            # only if this is a web server
sudo ufw enable
sudo ufw status verbose

On the RHEL family use firewall-cmd --permanent --add-service=ssh --add-service=https followed by --reload. See Firewalls.

Step 5: fail2ban and automatic updates

Terminal
sudo apt install -y fail2ban unattended-upgrades
printf '[DEFAULT]\nbantime = 1h\nmaxretry = 5\nignoreip = 127.0.0.1/8 YOUR_HOME_IP\n\n[sshd]\nenabled = true\n' | sudo tee /etc/fail2ban/jail.local
sudo systemctl enable --now fail2ban
sudo dpkg-reconfigure -plow unattended-upgrades

Step 6: logs, attack surface and monitoring

Terminal
sudo mkdir -p /var/log/journal && sudo systemctl restart systemd-journald   # persistent journal
sudo ss -tulpn                                      # anything listening that should not be?
sudo systemctl disable --now cups avahi-daemon 2>/dev/null || true
sudo apt install -y prometheus-node-exporter auditd

Restrict the exporter's port to your Prometheus server in the firewall, and add the audit rules from Auditing and Integrity. Review Reducing the Attack Surface for everything ss shows.

Python Generate your hardening plan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 7: verify from outside

From another machine you control:

Terminal
nmap -Pn -p- SERVER_IP                  # only 22 (and 80/443) should be open
ssh -o PubkeyAuthentication=no alice@SERVER_IP    # must be refused (permission denied)
ssh root@SERVER_IP                      # must be refused

On the server: sudo fail2ban-client status sshd, sudo ufw status, timedatectl, journalctl -u ssh --since today, sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'.

Python Score the captured results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In this sample, port 3306 is exposed. That is the finding the project is designed to teach: a database must bind to 127.0.0.1 or be firewalled.

Step 8: record it

Take a provider snapshot or image of the hardened baseline, store the exact steps in Git (better: as an Ansible playbook, see Ansible) and schedule a monthly review. A baseline you cannot reproduce is a baseline you cannot trust.

Stretch goals

  • Add two-factor authentication or SSH certificates.
  • Set up the AIDE baseline and ship logs to a central server.
  • Put sysctl hardening in /etc/sysctl.d/ and score the machine with Lynis before and after.
  • Write the whole procedure as an idempotent Ansible role and apply it to a second fresh server.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why test key-based login before disabling password authentication?

    Verify the replacement before removing the old method, and keep a second session open.
  2. 2. In the firewall step, what must be allowed before enabling ufw on a remote host?

    Default deny incoming would otherwise cut your own session.
  3. 3. Which command checks what an outsider can actually reach?

    An external scan shows the real exposure, including surprises such as published container ports.
  4. 4. Why record the hardening procedure as code?

    Reproducible configuration prevents drift and shortens rebuilds.

Summary

  • Order the work: update, admin user, SSH keys, hardened sshd, firewall, fail2ban, updates, logging and monitoring.
  • Keep a working session open and test every change from a second one.
  • Verify from outside with a scan and negative tests (root and password logins must fail).
  • Snapshot and automate the baseline so it can be reproduced and reviewed.

Next: Project: A Server Health Report