Using UFW (Ubuntu/Debian)
Install and configure: apt install ufw -y ufw default deny incoming ufw default allow outgoing
Allow common ports: ufw allow 22/tcp ufw allow 80/tcp ufw allow 443/tcp
Enable: ufw enable ufw status verbose
Using firewalld (CentOS/AlmaLinux)
systemctl start firewalld systemctl enable firewalld firewall-cmd --permanent --add-service=ssh firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
Block an IP Address
UFW: ufw deny from 1.2.3.4 iptables: iptables -A INPUT -s 1.2.3.4 -j DROP
Important
Always allow SSH (port 22) before enabling the firewall or you will lock yourself out.