UnderHost
Knowledgebase Docs

Setting up a firewall on your VPS

Configure UFW on Ubuntu/Debian or firewalld on AlmaLinux to protect your VPS from unauthorized access. Includes essential port rules and SSH lockdown guidance.

On this page

A firewall controls which network connections are allowed to reach your server. On a fresh VPS exposed to the internet, a firewall is one of the first things you should configure. Always allow SSH before enabling the firewall to avoid locking yourself out.

Allow SSH first-always

Add your SSH allow rule BEFORE enabling the firewall. If you enable the firewall without allowing port 22, you'll lock yourself out of the server. Use the emergency console in CustomerPanel to recover if this happens.

UFW on Ubuntu/Debian

UFW (Uncomplicated Firewall) is the recommended firewall tool for Ubuntu and Debian:

Ubuntu/Debian · bash
# Allow SSH (CRITICAL-do this first)
ufw allow 22/tcp

# Allow web traffic
ufw allow 80/tcp
ufw allow 443/tcp

# Enable UFW
ufw enable

# Check status
ufw status verbose

# Block a specific IP
ufw deny from 192.168.1.1

# Remove a rule
ufw delete allow 80/tcp

firewalld on AlmaLinux/Rocky

AlmaLinux/Rocky · bash
# Start and enable firewalld
systemctl start firewalld
systemctl enable firewalld

# Allow SSH, HTTP, HTTPS
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

# Apply changes
firewall-cmd --reload

# List all rules
firewall-cmd --list-all

Common port reference

PortServiceOpen by default?
22SSHRequired for management
80HTTPRequired for websites
443HTTPSRequired for websites
21FTPOptional-use SFTP (22) instead
25SMTP (outbound)Blocked by default-open a ticket to request
587/465SMTP SubmissionOpen if running a mail server
110/995POP3Open if running a mail server
143/993IMAPOpen if running a mail server
3306MySQLBlock-only open if needed remotely
7800aaPanelOpen only if running aaPanel

SSH security hardening

Additional steps to secure SSH beyond the firewall:

  • Change the SSH port-Move from 22 to a non-standard port (e.g. 2222) to reduce automated attacks
  • Disable root password login-Use SSH key authentication only; disable PasswordAuthentication in /etc/ssh/sshd_config
  • Install fail2ban-Automatically blocks IPs after repeated failed login attempts
  • Allow only specific IPs-If your IP is static, restrict SSH to just your IP: ufw allow from YOUR.IP.HERE to any port 22

Related: How to connect to your VPS via SSH | How to manage your Cloud VPS | Updating packages on your Linux VPS | How to use the VPS emergency console in CustomerPanel

Was this article helpful?

Need a Cloud VPS?

Launch an UnderHost Cloud VPS when you need root access, dedicated resources, custom software, or more control than shared hosting.

Related articles

Back to Cloud VPS