What is a firewall and how to protect the ports of a Linux server

Published on 2026-10-20 by frfrand

Learn what a firewall filters, how to use UFW without losing SSH access, and why opening only the necessary ports reduces risks.

What is a firewall and how to protect the ports of a Linux server

Short answer: a firewall decides which network traffic is allowed or blocked based on IP, protocol, and port rules. On a Linux server it is used to make only the necessary ports public —for example, 80 and 443 for the web— and close or restrict the rest. With UFW, allow SSH access first and only then activate it, so you don't get locked out.

A firewall is a mechanism that allows or blocks network traffic according to defined rules. On a Linux server it is used to reduce which services remain accessible from the Internet or from a private network. It does not eliminate application vulnerabilities, but it limits an important part of the attack surface.

If your VPS hosts a website, a database, and an internal panel, you probably don't want anyone to be able to connect directly to all three. The firewall helps establish that difference.

What does a firewall check?

Rules can take into account source or destination IP addresses, protocols such as TCP and UDP, ports, and connection state. Some more advanced products inspect additional information, but not all firewalls understand the content of a web request.

On Linux, the kernel's Netfilter subsystem participates in filtering. Tools like nftables or iptables allow managing rules, while ufw offers a simpler interface for common tasks.

Think of it like the reception of a building: you can decide who enters through which door. That does not guarantee that an authorized person will never do something improper inside.

A concrete example with three services

Imagine you have:

  • Nginx listening on ports 80 and 443 for the public web.
  • SSH listening on port 22 for administration.
  • PostgreSQL listening on port 5432 for the application.

A reasonable configuration might allow 80 and 443 from the Internet, restrict SSH to an administrative network, and prevent PostgreSQL from being publicly accessible. The application and the database can communicate via a local interface or private network.

The exact decision depends on your architecture. Do not copy a port list from another project without understanding which ones you need.

How to check the firewall status with UFW?

On Ubuntu Server, UFW is usually available as a simple administration tool. First, inspect the status:

sudo ufw status verbose

If the firewall appears inactive, do not activate it blindly while connected via SSH. You could block your own access if there is no appropriate rule.

Before changing anything, check the actual SSH port, your administration route, the provider's rules, and whether you have a recovery console available.

An example of rules, not a universal recipe

For a server with SSH on the standard port and a public web, these commands illustrate the concept:

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status numbered

Only after verifying access and the environment could you consider enabling UFW with sudo ufw enable. If you manage SSH from a fixed IP, it is preferable to study a rule that limits the source, for example sudo ufw allow from 203.0.113.10 to any port 22 proto tcp (that address is only a documentation example, not your real IP).

A frequent mistake is allowing 22/tcp from the entire Internet because it is convenient and forgetting to check who needs that access.

What about Docker and published ports?

Containers add a layer of networks and rules that can modify how traffic is exposed. Depending on the configuration, publishing a Docker port can interact with firewall rules in unexpected ways.

Do not assume that UFW alone will restrict all ports published by containers. Review your container engine documentation, test from another machine, and check which interfaces are listening.

For an internal application, it is usually better to bind the service to a private interface or to localhost and publish it only through the appropriate proxy.

Server firewall and provider firewall

A VPS can have rules at several layers: within the operating system, in the provider's infrastructure, and in an ingress load balancer or proxy.

These layers can complement each other, but they can also complicate diagnosis. If a connection fails, check the service, its listening interface, host rules, and external rules. A port allowed in UFW is not necessarily accessible if another firewall blocks it.

Does a firewall prevent DDoS attacks or SQL injection?

Not by itself. A host firewall can filter connections, but a volumetric attack can saturate the link before traffic reaches the server. For that, upstream network defenses are usually needed.

It also does not automatically detect a malicious SQL query within an allowed HTTPS request. Application security requires validations, parameterized queries, access controls, and other measures.

You can expand on this topic with what is a DDoS attack and why it is not advisable to use root for everything.

A defensive check worth repeating

After each major deployment, check which services are listening, on which interfaces, and which ports remain exposed. On Linux, ss -tulpen can help you inspect sockets, although to verify external accessibility you need a test from another authorized location.

Document the essential ports and delete rules that are no longer used. A well-configured firewall is one that reflects real services, not a collection of accumulated exceptions.

What about ArduMaker?

On an ArduMaker VPS, the layers mentioned above are clearly visible. The VPS is a container within a shared host and does not receive Internet traffic directly: only the domains you publish arrive — through the host's nginx, which terminates HTTPS — and the TCP or UDP port forwarding rules you create. Each forwarding rule can be limited to certain IPs, or exclude some, from the VPS IP policy, which is applied on the host firewall and can only be changed by company administrators. By default, forwarding rules accept any IP.

SSH access also goes through a forwarding rule, which can be opened with an expiration date so that it closes automatically (a new VPS starts with SSH open for 6 hours). Therefore, rules like ufw allow 22/tcp work as an additional defense within the VPS, not as the only barrier. Managing a deployment from a platform does not replace reviewing which ports each service needs and closing forwarding rules that are no longer used.

In summary: start by identifying the services that must receive traffic, allow only necessary accesses, and check the rules without losing the administration connection.

Frequently asked questions

What ports should I open on a web server? Normally 80 and 443 for the web, and SSH restricted to whoever administers it. Databases and internal panels should not be public.

Does UFW block the ports published by Docker? Not always: Docker adds its own iptables rules and can bypass UFW's rules. It needs to be reviewed and tested from outside.

How do I see which services are listening? With sudo ss -tulpen, which shows open sockets; to know if they are accessible from the Internet, they must be tested from another authorized location.

Official sources: Firewall on Ubuntu Server · Netfilter in Ubuntu documentation · Docker networks and port publishing.

What is a firewall and how to protect the ports of a Linux server
  • Linux
  • firewall
  • seguridad
  • servidores

Keep reading

All articles · VPS plans