back to docs
server-setup

Security Setup

Secure your server with SSH hardening and fail2ban

This guide walks through securing your server with SSH hardening and Fail2Ban. You will:

  1. Configure SSH for secure remote access
  2. Set up Fail2Ban for intrusion prevention
  3. Verify your security configuration

1. SSH configuration

a. Configure the SSH daemon

Edit the SSH daemon configuration:

nano /etc/ssh/sshd_config
Tip
Common security settings include changing the default port, disabling root login, and enabling public key authentication only.

Disable SSH socket if enabled:

systemctl stop ssh.socket
systemctl disable ssh.socket

Restart SSH service to apply changes:

systemctl restart ssh

b. Configure public key authentication

Edit the SSH configuration file:

nano /etc/ssh/sshd_config.d/*.conf

Add or modify these settings:

PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin prohibit-password
Important
Set up public key authentication before disabling password authentication so you don't lock yourself out.

c. Verify SSH status

Check that SSH is running correctly:

systemctl status ssh

2. Fail2Ban setup

a. Install Fail2Ban

sudo apt update
sudo apt install fail2ban -y

b. Enable and start the service

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

c. Configure Fail2Ban

Warning

Do not copy jail.conf. Keep custom configuration minimal and place it in jail.local or jail.d/*.conf.

Create or edit the local configuration file:

sudo nano /etc/fail2ban/jail.local

Minimal recommended configuration:

[DEFAULT]
bantime  = 365d
findtime = 10m
maxretry = 5

Explanation:

  • bantime: how long an IP is banned
  • findtime: time window to count failures
  • maxretry: number of failures before banning

On Debian-based systems, the SSH jail is already enabled by default via the distro configuration, so no extra SSH configuration is needed here.

d. Restart Fail2Ban

sudo systemctl restart fail2ban

e. Verify Fail2Ban setup

Verify your Fail2Ban installation with these commands:

Check overall status:

sudo fail2ban-client status

Expected output should include at least sshd in the jail list.

Check specific jail details:

sudo fail2ban-client status sshd

Monitor logs in real-time:

sudo tail -f /var/log/fail2ban.log

Check service status:

systemctl status fail2ban

3. Additional security recommendations

  • Regularly update your system with sudo apt update -qy && sudo apt upgrade -qy
  • Use strong passwords or passphrase-protected SSH keys
  • Consider implementing a firewall (UFW or iptables)
  • Monitor logs regularly for suspicious activity
  • Keep your SSH port non-standard (security through obscurity)
Command Palette
Search for a command to run...