This module ensures you can safely access the server remotely without locking yourself out.
sudo systemctl is-enabled ssh || sudo systemctl enable --now sshEdit the SSH daemon configuration:
sudo nano /etc/ssh/sshd_configRecommended initial baseline:
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yesIMPORTANT
Keep
PasswordAuthentication yesat this stage. This guarantees you cannot lock yourself out while hardening SSH.
sudo sshd -t
sudo systemctl restart sshsystemctl status sshThe steps below must be followed in sequence.
ssh-keygen -t ed25519 -C "homeserver"Copy the key to the server:
ssh-copy-id <username>@<server-ip>Test login without logging out of your current session:
ssh <username>@<server-ip>You should be logged in without entering the server password.
Edit SSH config:
sudo nano /etc/ssh/sshd_configAdd or change:
Port 2222Apply changes:
sudo sshd -t
sudo systemctl restart sshTest from a new terminal:
ssh -p 2222 <username>@<server-ip>❗ DO NOT DO THIS UNTIL:
- SSH key login works
- New SSH port works
Edit config:
sudo nano /etc/ssh/sshd_configSet:
PasswordAuthentication noApply:
sudo sshd -t
sudo systemctl restart ssh- Password logins are disabled
- SSH access is key-only
Install Fail2Ban:
sudo apt install -y fail2banCreate local jail configuration:
sudo nano /etc/fail2ban/jail.localAdd:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
[sshd]
enabled = true
port = 2222Enable and start:
sudo systemctl enable --now fail2banVerify:
fail2ban-client status
fail2ban-client status sshd- Root login disabled
- Password authentication disabled
- SSH keys required
- Non-standard port in use
- Automatic IP banning enabled