A VPS with a public IP is visible to automated scanners within minutes of coming online. That is why learning how to configure VPS firewall rules is one of the first operational tasks after deployment - before an application, database, or control panel is exposed to the internet.
A firewall does not replace secure passwords, software updates, or application hardening. It gives those controls a useful boundary. The goal is straightforward: allow the traffic your server must receive, block everything else by default, and make changes without cutting off your own administrative access.
Start With a Service and Access Plan
Do not begin by copying a firewall command from a random setup guide. First, identify what the VPS actually needs to do and where administrators will connect from. A single WordPress site has a different exposure profile than a SaaS application, mail server, VPN endpoint, or reseller hosting environment.
For a typical Linux web server, inbound access often includes SSH on port 22, HTTP on port 80, and HTTPS on port 443. If SSH uses a custom port, allow that port instead. Database ports such as MySQL 3306, PostgreSQL 5432, and Redis 6379 should generally not be open to the public internet. If an application server needs database access, keep that traffic on a private network where available, or limit the rule to the application server's IP address.
Write down the required services, their ports, and their source addresses. This small step prevents the common mistake of allowing a port simply because software installed it. A listening service is not automatically a service that should be public.
Before changing firewall rules, keep two SSH sessions open to the VPS. Use one session to apply changes and leave the other untouched as a recovery path. If a rule blocks new connections, the existing session may still let you correct it. Out-of-band console access from your hosting control panel is also valuable when you need to recover from a configuration error.
How to Configure VPS Firewall Rules on Linux
Most Linux VPS deployments use either UFW, firewalld, or direct nftables and iptables rules. UFW is common on Ubuntu and Debian because its syntax is simple. firewalld is common on AlmaLinux, Rocky Linux, CentOS-derived systems, and Fedora. Use the tool supplied by your operating system rather than running several firewall managers at once.
Ubuntu and Debian With UFW
First, verify the service and current rules:
```bash sudo ufw status verbose ```
If you connect over SSH, allow SSH before enabling the firewall:
```bash sudo ufw allow OpenSSH ```
If your SSH daemon runs on another port, use that specific port instead:
```bash sudo ufw allow 2222/tcp ```
For a public website, allow web traffic:
```bash sudo ufw allow 80/tcp sudo ufw allow 443/tcp ```
Then set conservative defaults and enable UFW:
```bash sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw enable ```
The incoming default denies unsolicited connections. The outgoing default allows the server to retrieve package updates, resolve DNS, send application requests, and complete normal outbound connections. Restricting outbound traffic can be worthwhile for tightly controlled workloads, but it requires more planning because package managers, monitoring agents, backups, and third-party APIs may need exceptions.
For administrative access, limiting SSH to a known office, VPN, or home IP is stronger than allowing it from everywhere:
```bash sudo ufw allow from 203.0.113.25 to any port 22 proto tcp ```
Do not apply this restriction if your source address changes frequently unless you have a reliable VPN, console access, or a documented recovery method. Security that permanently locks out the operator is not useful security.
RHEL-Based Systems With firewalld
On a system using firewalld, check active zones first:
```bash sudo firewall-cmd --get-active-zones ```
Most VPS configurations place the public network interface in the `public` zone. Allow standard web services permanently, then reload the configuration:
```bash sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload ```
SSH is often included by default, but confirm rather than assume:
```bash sudo firewall-cmd --list-services ```
To allow a nonstandard port, add it explicitly:
```bash sudo firewall-cmd --permanent --add-port=2222/tcp sudo firewall-cmd --reload ```
For SSH limited to a specific source address, rich rules provide a clear approach:
```bash sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.25" port port="22" protocol="tcp" accept' sudo firewall-cmd --reload ```
Test from a second terminal after every meaningful change. Firewall syntax can be correct while the rule is attached to the wrong zone or network interface.
Configure Windows VPS Firewall Carefully
Windows Server includes Windows Defender Firewall with Advanced Security. On a Windows VPS, the most immediate concern is Remote Desktop Protocol. Before restricting inbound rules, confirm that RDP is allowed and that you have console access through the provider if needed.
Use the graphical firewall console for a small number of rules, or PowerShell for repeatable configuration. For example, the following creates inbound rules for web traffic:
```powershell New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow New-NetFirewallRule -DisplayName "Allow HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow ```
RDP normally uses TCP 3389. Leaving it open to every IP invites password-guessing attempts, even with strong credentials. Where practical, scope the RDP rule to trusted public IP addresses, use a VPN or remote access gateway, and require multi-factor authentication through your chosen access design. Changing the RDP port may reduce noise from basic scans, but it is not a substitute for source restrictions and account security.
Use Narrow Rules for Sensitive Services
The safest firewall rule is usually the narrowest rule that still supports the workload. That means specifying protocol, port, source, and sometimes destination rather than opening broad ranges.
A few examples illustrate the difference. Allowing TCP 443 from any source is appropriate for a public ecommerce site. Allowing TCP 3306 from any source is rarely appropriate for the same server. A backup agent may require a vendor-specific destination and outbound port, while an internal monitoring system may need inbound access only from a fixed monitoring IP.
Avoid opening broad port ranges to solve a connection problem without understanding it. First verify whether the application is listening on the expected interface, whether the service is healthy, whether DNS points to the correct address, and whether a host firewall is actually the layer causing the issue. Opening ports unnecessarily creates work later when services change or vulnerabilities emerge.
Account for the Network Firewall Too
Your VPS may have more than one filtering layer. The operating system firewall controls traffic after it reaches the virtual server. A provider-level firewall, cloud security group, DDoS filtering platform, load balancer, or reverse proxy may filter traffic before that point.
This layered approach is useful, but it can make troubleshooting less obvious. If port 443 is allowed in UFW but a provider-level rule denies it, visitors still cannot reach the website. Conversely, an open provider rule does not help if the operating system firewall blocks the connection. Document which layer owns each policy so that future changes are deliberate.
For public workloads, DDoS protection can reduce the impact of common volumetric attacks, but it does not eliminate the need for application rate limits, patched software, and sensible firewall exposure. Each control addresses a different part of the problem.
Test, Monitor, and Keep a Recovery Path
After configuring rules, test from outside the VPS. Check public web ports from a separate connection, verify that SSH or RDP works from approved addresses, and confirm that prohibited services are not reachable. Test the application itself as well. A firewall that permits port 443 cannot fix an expired certificate, a failed web service, or a reverse-proxy error.
Review firewall logs when troubleshooting, but be selective. Internet-facing servers generate constant background scan traffic, and logging every denied packet can consume disk space and obscure meaningful events. Log suspicious or rate-limited traffic where it supports your operations, then rotate logs and monitor capacity.
Finally, treat firewall changes like application changes. Record the reason for each exception, remove temporary access when maintenance ends, and review rules when you add a control panel, deploy a new service, or retire an old one. A clean firewall policy is easier to operate on a low-cost VPS and just as valuable as the server grows into a larger production environment.
