Securing an Ubuntu VPS: SSH, UFW and Fail2Ban Explained

A Linux VPS connected to the internet can receive automated connection attempts shortly after deployment. Bots routinely scan public IP addresses for exposed services, attempt common SSH credentials, and look for configuration weaknesses.

This does not necessarily mean someone is specifically targeting your server. It is simply part of operating infrastructure on the public internet.

For Ubuntu VPS administrators, three tools form a useful security foundation: OpenSSH, UFW, and Fail2Ban.

They address different aspects of server protection. Understanding how they work together is more important than installing all three without reviewing their configurations.

Why Ubuntu VPS Security Needs Multiple Layers

A common mistake is assuming that one security tool can protect an entire server.

For example, changing the SSH port does not prevent unauthorized access if weak authentication remains enabled. Installing Fail2Ban does not automatically protect every exposed application. And enabling a firewall does not guarantee that the services you allow are configured securely.

A practical security approach has three layers:

  1. SSH authentication: Determine who can access the server and how they authenticate.
  2. Firewall filtering: Control which network connections can reach server services.
  3. Login abuse protection: Detect repeated authentication failures and temporarily restrict suspicious sources.

These mechanisms complement each other, but none replaces system updates, application security, or reliable backups.

1. Secure SSH Authentication First

SSH is typically the primary administration interface for an Ubuntu VPS. Because it provides remote command-line access, its configuration deserves special attention.

Use SSH keys instead of relying only on passwords

SSH key authentication uses a private key held by the administrator and a corresponding public key configured on the server.

This reduces reliance on reusable account passwords and makes large-scale password guessing less effective.

However, the private key must also be protected. A stolen unencrypted private key can still create a security problem.

For everyday administration, a good starting point is a dedicated user account with authorized sudo access and a properly configured SSH key.

Review direct root access

Root has unrestricted administrative permissions. Allowing direct root login increases the importance of protecting that account.

Many administrators prefer connecting as a regular user and escalating privileges through sudo only when necessary.

Before restricting root login, verify that the alternative administrative account can connect and perform the required operations.

Validate SSH configuration changes

OpenSSH can load configuration from multiple files, including files inside /etc/ssh/sshd_config.d/.

This means changing one directive does not always produce the behavior you expect.

Two commands are particularly useful:

sudo sshd -t
sudo sshd -T

The first checks configuration syntax. The second displays effective settings, although user- and connection-specific rules may require additional evaluation.

Syntax validation alone does not prove that a remote login will succeed.

Always test a new SSH session before disconnecting the current one.

For the complete authentication and access configuration process, see How to Secure SSH on Ubuntu Server: Keys, Root Login and Access Settings.

2. Use UFW to Reduce Network Exposure

Even with secure SSH authentication, a VPS may expose services that do not need to be reachable from the internet.

A web application, development server, database, or monitoring interface can accidentally listen on a public address.

Ubuntu’s UFW, or Uncomplicated Firewall, provides a convenient interface for controlling network traffic without manually maintaining complex low-level firewall rules.

Allow only necessary services

For a simple website, publicly accessible services might include:

ServiceTypical portPurpose
SSHTCP 22 or a custom portRemote administration
HTTPTCP 80Web traffic
HTTPSTCP 443Encrypted web traffic

These are examples, not universal requirements.

A VPN server, mail server, or application with custom networking may need additional TCP or UDP ports.

The goal is not to close every port. It is to expose only the services required for the server’s intended workload.

The most dangerous firewall mistake

One of the easiest ways to lose access to a VPS is to enable a restrictive firewall before allowing the configured SSH port.

For example, suppose SSH is listening on TCP port 2222, but the firewall only permits TCP port 22.

Existing connections may behave differently from new connections, and your next login attempt could fail.

Before activating UFW, check the actual SSH listening port, configure the correct allow rule, and make sure a hosting provider console is available for recovery.

Check the actual firewall state

Useful diagnostic commands include:

sudo ufw status verbose
sudo ufw status numbered
sudo ss -lntup

UFW status displays the firewall policies and configured rules.

The ss command helps identify which applications are listening on network ports.

This distinction matters. A firewall can allow TCP port 8080 even when no application is listening there. Conversely, an application may be running correctly but remain unreachable because of a firewall or upstream network restriction.

IPv6 should also be included in your review when the server has IPv6 connectivity.

For installation, rules, logging, troubleshooting, and recovery examples, read How to Configure UFW Firewall on Ubuntu Server.

3. Use Fail2Ban to Respond to Repeated Login Failures

Even after enabling key authentication and restricting exposed ports, public SSH services may continue receiving automated login attempts.

Fail2Ban helps identify repeated failures by monitoring configured log sources.

When activity matches a configured rule, Fail2Ban can temporarily block the corresponding IP address through a supported firewall action.

How Fail2Ban works

Its basic process looks like this:

  1. A service records unsuccessful authentication attempts.
  2. Fail2Ban examines the configured logs or journal entries.
  3. A filter identifies failures matching its rules.
  4. When the configured threshold is reached, a ban action is triggered.
  5. The restriction expires according to the configured ban duration, unless a different ban policy applies.

Fail2Ban uses configurations called jails to define which services are monitored, which filters are applied, and how bans are handled.

A jail intended for SSH does not automatically protect a web application or administrative dashboard.

Do not assume installation means protection

Installing the package alone is not sufficient evidence that the correct jail is active.

After configuration, check the service and jail status:

sudo systemctl status fail2ban
sudo fail2ban-client status

The first command helps determine whether the service is running.

The second lists active jails. You can then inspect a specific jail to review its failures and banned addresses.

If an expected SSH jail is missing, investigate its configuration and log source rather than assuming the protection is enabled.

The complete walkthrough is available in How to Install and Configure Fail2Ban on Ubuntu Server.

How SSH, UFW and Fail2Ban Work Together

Imagine a VPS hosting a public website.

SSH is used for maintenance. Nginx serves web traffic, and the application database communicates only with local services.

In this environment:

  • SSH authenticates the administrator using an authorized account and key.
  • UFW permits required web traffic and administrative access while restricting unnecessary incoming connections.
  • Fail2Ban monitors configured authentication failures and responds when repeated attempts exceed defined limits.

Each layer addresses a different problem.

If the firewall blocks a connection entirely, the request may never reach the SSH service.

If the firewall permits the connection, SSH still needs to authenticate the user.

If repeated failed logins are detected by an active Fail2Ban jail, the source can be temporarily blocked.

This layered approach makes security controls easier to understand and troubleshoot.

Common Ubuntu VPS Security Mistakes

Changing everything at once

Disabling password authentication, changing the SSH port, and enabling a firewall in the same session can make troubleshooting difficult.

Instead, change one component, validate it, and test the result before moving to the next.

Believing a custom SSH port guarantees security

Moving SSH away from port 22 may reduce some automated scanning noise.

However, it does not replace strong authentication, access restrictions, or patch management.

Using unrestricted firewall allow rules

Allowing every connection from a trusted IP address may expose more services than intended.

When possible, specify both the required source network and the particular destination port.

Leaving administrative interfaces public

Monitoring dashboards, database management panels, and internal application interfaces should not automatically be exposed to every internet user.

Consider private networking, VPN access, localhost binding, or narrowly restricted firewall rules where appropriate.

Ignoring service and security logs

A server can appear operational while generating authentication failures, firewall blocks, or application errors.

Review logs periodically and investigate meaningful changes in activity rather than waiting for an outage.

A Practical Security Review Routine

After the initial configuration, server security requires ongoing maintenance.

A useful routine includes checking whether SSH authentication still works as intended, reviewing firewall exposure when applications are added, confirming Fail2Ban jails remain active, and applying supported security updates.

It is also worth reviewing which services start automatically and whether obsolete applications are still installed.

Keep configuration backups and verify that the hosting provider’s emergency console or recovery process is usable.

Security settings are most useful when the administrator can understand, verify, and recover them.

Final Thoughts

SSH, UFW, and Fail2Ban are three important building blocks for securing an Ubuntu VPS.

SSH determines how administrators authenticate. UFW controls network exposure. Fail2Ban helps respond to repeated authentication failures.

Used together, they provide a stronger security baseline than any single mechanism alone.

The safest approach is to configure these protections gradually, verify every significant change, and maintain a reliable recovery path.

A well-secured VPS should not only resist unauthorized access. It should also remain understandable and manageable for the person responsible for operating it.


Posted

in

by