Think of your server as a building. Every port that a service listens on is a door. Your firewall is the guard standing in front of those doors. It does a good job, but guards make mistakes: a rule gets deleted by accident, the firewall is turned off “just for a quick test” and never turned back on, or a program adds its own rule that lets traffic in. When that happens, every service behind the firewall is exposed. But if no service is listening on a port, there’s nothing there to connect to, so a firewall mistake can’t expose it. That’s the goal of this guide: close the ports you don’t need, so your server depends less on the firewall being perfect.
The problem is that doors appear on their own. You install a package, it starts a service, and that service starts listening on the network. Nobody asks you. Most people never check what their server is actually listening on, so they have no idea how many doors are open.
This guide shows you how to list every listening port on your server, find out which program opened it, and then decide: close it, make it reachable only from the server itself, or keep it on purpose. It takes about 20 minutes, and you’ll do it on a small lab so you can see real results.
Important note: This guide uses commands for Debian-based distributions (Debian, Ubuntu, and derivatives). It was written for Ubuntu Server 26.04 LTS.
Critical safety note: If you’re connected to the server over SSH, do not stop, disable, or remove SSH during this exercise. Keep your current session open, and make sure you have another way in (a console in your virtualization tool, or your hosting provider’s web console) before you change anything. A firewall change can lock you out too, even while SSH itself keeps running.
What “listening” means
A service is “listening” when it’s ready to receive network traffic on a port. With TCP, it waits for someone to connect. With UDP, there’s no connection: it simply receives the messages sent to that port. Nginx waits on port 80 for HTTP clients, like web browsers or curl. SSH waits on port 22 for you. A database waits on port 3306 for the application that uses it.
But the port is only half of the story. The other half is the address the service listens on: it tells the server which of its own network addresses the service accepts connections on.
| Address shown | What it means |
|---|---|
0.0.0.0 | All of the server’s IPv4 addresses |
[::] | All of the server’s IPv6 addresses (some programs use it to accept IPv4 too) |
* | A wildcard: the service isn’t limited to one address |
127.0.0.1 or [::1] | Only programs running on the server itself |
127.0.0.53%lo | Only the server itself. %lo means “on the loopback interface”. On Ubuntu, this is where systemd-resolved listens: the local DNS resolver, which turns names like vigilnix.com into IP addresses |
A specific IP, like 192.168.1.10 | Only connections that arrive on that one address, which other machines can still reach |
Keep this table in mind. It’s the key to reading everything that follows. A database on 127.0.0.1:3306 is a door inside the building. The same database on 0.0.0.0:3306 is a door on the street.
One thing to be clear about: “listening on all addresses” doesn’t automatically mean “reachable from the internet”. Your firewall, your router, or your cloud provider’s network rules may still block it. But it does mean other machines may be able to reach it, as long as the network lets them. From then on, your firewall and network rules decide which connections reach the service, and the service’s own security (logins and permissions) decides who can use it and what they’re allowed to do. That’s why every service listening on an address other than 127.0.0.1 or [::1] deserves a look, including services listening on a specific IP like 192.168.1.10. A specific address is not the same as local-only.
Build the lab
A fresh Ubuntu install listens on very little, so there wouldn’t be much to look at. To make this guide useful, we’ll install four services first. Each one plays a role you’ll meet on real servers.
What you need:
- A test virtual machine running Ubuntu Server 26.04 LTS.
- A snapshot of that VM (or another way to reset it).
- A user with
sudorights. - Console access to the VM, in case SSH stops working.
- Optional: a second machine on the same network, for the outside check at the end.
Safety note: Do this on a test virtual machine, never on a production server. Take the snapshot before you start, so you can go back to a clean state when you’re done.
Save the “before” picture
Before installing anything, save the list of listening ports. Run this command:
sudo ss -H -tuln | awk '{print $1, $5}' | sort > ~/ports-before.txt
This writes the list of listening ports to a file called ports-before.txt in your home folder. Each part has a job:
-Hremoves the header line.awk '{print $1, $5}'keeps only two columns: the protocol (tcporudp) and the address and port, liketcp 0.0.0.0:22.sortputs the lines in a fixed order.
Why keep only two columns? ss adds spaces to line up its columns, and the number of spaces changes when a longer address appears. If you kept the full lines, every line would look different later, even the ones that didn’t change. With only two columns and no extra spaces, the comparison shows only real changes. (The other letters after ss are explained in the next section. For now, just run it.)
Install the four services
Here’s who’s joining the lab:
| Service | Role in the lab | What it listens on |
|---|---|---|
nginx | A web server you really need, because it serves your website | 0.0.0.0:80 and [::]:80 |
mariadb-server | A database that a bad tutorial opened to the world | 0.0.0.0:3306 (after the next step) |
rpcbind | A service you never use, often pulled in by file-sharing (NFS) packages | port 111 on all addresses, TCP and UDP |
vsftpd | An old FTP server someone installed and forgot | port 21 on all addresses |
Refresh the package list:
sudo apt update
Install the four packages:
sudo apt install nginx mariadb-server rpcbind vsftpd
Copy a common tutorial mistake
On Ubuntu, MariaDB listens only on 127.0.0.1 by default. That’s the safe choice. But many tutorials tell you to change it to 0.0.0.0 so “remote connections work”. Let’s make that mistake on purpose.
First, look at the line we’re going to change:
grep -n bind-address /etc/mysql/mariadb.conf.d/50-server.cnf
You should see one line that starts with bind-address and ends with 127.0.0.1, under the [mysqld] section of the file. If the line is missing or starts with #, the command below won’t change anything, so open the file with sudo nano and look before going further.
Now change it:
sudo sed -i 's/^bind-address.*/bind-address = 0.0.0.0/' /etc/mysql/mariadb.conf.d/50-server.cnf
This replaces the bind-address line in MariaDB’s configuration file with bind-address = 0.0.0.0.
Restart MariaDB to apply the change:
sudo systemctl restart mariadb
The lab is ready. Time to look at it.
List every open door with ss
The tool for this job is ss. It’s installed on every Ubuntu system and it shows every socket (network connection point) on the machine.
Run this command:
sudo ss -tulpn
Each letter has a job:
-tshows TCP ports.-ushows UDP ports.-lshows only ports that are listening, not connections already in progress (like your current SSH session).-pshows the program that owns each port.-nshows numbers (22) instead of names (ssh), so you see the real port.
Why sudo? Without it, ss still lists the ports, but it isn’t allowed to tell you which program owns a port when that program runs as another user. Most services run as other users, so the Process column would be mostly empty.
Here’s the real output from my lab VM:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Processudp UNCONN 0 0 127.0.0.54:53 0.0.0.0:* users:(("systemd-resolve",pid=601,fd=18))udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=601,fd=16))udp UNCONN 0 0 192.168.56.109%enp0s8:68 0.0.0.0:* users:(("systemd-network",pid=1120,fd=34))udp UNCONN 0 0 10.0.2.15%enp0s3:68 0.0.0.0:* users:(("systemd-network",pid=1120,fd=39))udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=4240,fd=5),("systemd",pid=1,fd=200))udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=2173,fd=4))udp UNCONN 0 0 [::]:111 [::]:* users:(("rpcbind",pid=4240,fd=7),("systemd",pid=1,fd=203))udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=2173,fd=5))tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=4486,fd=5),("nginx",pid=4485,fd=5),("nginx",pid=4484,fd=5),("nginx",pid=4483,fd=5),("nginx",pid=4482,fd=5),("nginx",pid=4481,fd=5),("nginx",pid=4480,fd=5),("nginx",pid=4479,fd=5),("nginx",pid=4477,fd=5))tcp LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=4240,fd=4),("systemd",pid=1,fd=199))tcp LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1517,fd=3),("systemd",pid=1,fd=176))tcp LISTEN 0 4096 127.0.0.54:53 0.0.0.0:* users:(("systemd-resolve",pid=601,fd=19))tcp LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* users:(("mariadbd",pid=5060,fd=28))tcp LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=601,fd=17))tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=4486,fd=6),("nginx",pid=4485,fd=6),("nginx",pid=4484,fd=6),("nginx",pid=4483,fd=6),("nginx",pid=4482,fd=6),("nginx",pid=4481,fd=6),("nginx",pid=4480,fd=6),("nginx",pid=4479,fd=6),("nginx",pid=4477,fd=6))tcp LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=4240,fd=6),("systemd",pid=1,fd=201))tcp LISTEN 0 32 *:21 *:* users:(("vsftpd",pid=4452,fd=3))tcp LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=1517,fd=4),("systemd",pid=1,fd=177))
Your PIDs will be different, and the order of the lines may be too.
Each line of this output is one entry: one port that a program is listening on. That’s a lot of text, but you only need three columns:
Netid:tcporudp.Local Address:Port: where the door is (use the table from the first section).Process: which program opened it, with its PID (process ID). Some programs show several PIDs on one line:nginxruns one main process plus several worker processes, and they all share the same port.
Read the line for port 3306 with those three columns: TCP, 0.0.0.0:3306, mariadbd. In plain words: the MariaDB database accepts connections on every IPv4 address the server has. That’s the tutorial mistake, and ss shows it in one line.
Note: ss shows what’s listening on the server. It doesn’t tell you whether the internet can actually reach it. We’ll check that from the outside at the end of the guide.
See exactly what the packages added
Remember the “before” file? Compare it with the current state:
diff ~/ports-before.txt <(sudo ss -H -tuln | awk '{print $1, $5}' | sort)
Lines starting with > are listening ports that exist now but didn’t before. Those are the doors the four packages opened, without asking you. You can ignore lines like 1a4,5: they only tell you where in the list each change is.
Don’t expect one line per service. One service can open several: rpcbind alone listens on TCP and UDP, over IPv4 and IPv6.
We left out -p for both lists on purpose. Process IDs change every time a service restarts, so leaving them out keeps the comparison clean.
Here’s what I got on my lab VM:
0a1,2> tcp *:21> tcp 0.0.0.0:1111a4,5> tcp 0.0.0.0:3306> tcp 0.0.0.0:803a8> tcp [::]:1114a10,11> tcp [::]:80> udp 0.0.0.0:11110a18> udp [::]:111
Four packages, eight new listening ports, and every one of them on all addresses. rpcbind alone accounts for half of them, for a service this server never uses.
The “before” picture: what’s normal
Before we start closing things, let’s look at what was already there before the lab. Your list may not be exactly the same as mine, because it depends on how Ubuntu was installed. On my VM, these were the entries, and they’re normal:
- SSH on port 22. This is how you log in. You may see
systemdlisted next tosshdin theProcesscolumn. That happens when SSH uses socket activation, which recent Ubuntu releases do by default: systemd holds the port and startssshdwhen a connection arrives. To check on your server, runsystemctl list-socketsand look forssh.socket. systemd-resolvedon127.0.0.53:53and127.0.0.54:53. The local DNS resolver, which turns names likevigilnix.cominto IP addresses. Local only.chronydon127.0.0.1:323. The service that keeps your clock correct. Local only.systemd-networkdon UDP port 68. If your server gets its IP address automatically (DHCP), this is what asks for it. You’ll see one line per network card: my VM has two, so there are two lines.
If you see something on your server that isn’t in this list, don’t assume it’s bad, and don’t assume it’s fine either. Identify it with the steps in the next section.
A simple rule: entries on 127.0.0.1, [::1], 127.0.0.53 or 127.0.0.54 are local-only, because only the server itself can connect to them. Every other entry deserves a question: 0.0.0.0, [::], *, or a specific IP like 192.168.1.10.
Note: If your test VM has a desktop, you’ll see a few extra entries, like cupsd (printing) and avahi-daemon (finding other devices on your network). They come from the desktop packages. A server install doesn’t have them.
Find out who opened each door
Sometimes the program name in the Process column is obvious. Sometimes it isn’t. Here’s how to go from a port to a service you can manage.
Take the PID from the Process column (for example pid=4452 on port 21) and ask which systemd service it belongs to:
sudo systemctl status 4452
The first line of the output names the service, here vsftpd.service. That’s the name you’ll use to stop it. If the program wasn’t started by systemd, this won’t show a service name; the next two commands still tell you what the program is.
Find the full path of the program behind that PID:
sudo readlink /proc/4452/exe
This prints something like /usr/sbin/vsftpd.
Then find which package installed that program:
dpkg -S /usr/sbin/vsftpd
The output starts with the package name, vsftpd. That’s the name you’ll use to remove it.
Note: Replace 4452 with the real PID from your own ss output. PIDs are different on every machine.
Decide: keep, make local, or remove
For each door that isn’t on a local-only address (127.0.0.1, [::1], 127.0.0.53 or 127.0.0.54), ask two questions, in this order:
- Do I need this service at all? If not, remove it.
- Do other machines need to reach it? If not, make it local-only.
If the answer to both is yes, keep it, and use your firewall to limit who can reach it.
Here’s how that plays out on the lab:
| Service | Do I need it? | Do other machines need it? | Decision |
|---|---|---|---|
nginx | Yes, it serves the website | Yes, visitors need it | Keep, and allow only ports 80 and 443 in the firewall |
| MariaDB | Yes, the website uses it | No, only the website on this server talks to it | Make local-only |
rpcbind | No | — | Remove |
vsftpd | No | — | Remove |
For nginx, the firewall side is covered in UFW vs iptables vs nftables. With UFW, sudo ufw allow 'Nginx Full' allows the web ports, 80 (HTTP) and 443 (HTTPS). It doesn’t remove rules you added before, so check them with sudo ufw status.
Now let’s close the doors we don’t need.
Close a door you don’t need
The socket trap
We’ll start with rpcbind, because it hides a trap.
Stop it now and stop it from starting at boot:
sudo systemctl disable --now rpcbind.service
Check port 111:
sudo ss -tulpn '( sport = :111 )'
The part in quotes is a filter: it shows only sockets whose local port is 111. It’s more precise than grep 111, which would also match a PID or any other number containing 111.
Port 111 is still listening.
Look at the Process column: the owner is now only systemd. The rpcbind program is gone, but systemd itself is still holding the door, through a second unit called rpcbind.socket. If anyone connects, systemd will simply start rpcbind again. systemd even warns you about this when you disable the service. Here’s what I got:
Synchronizing state of rpcbind.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install disable rpcbindRemoved '/etc/systemd/system/sockets.target.wants/rpcbind.socket'.Removed '/etc/systemd/system/multi-user.target.wants/rpcbind.service'.Disabling 'rpcbind.service', but its triggering units are still active:rpcbind.socketStopping 'rpcbind.service', but its triggering units are still active:rpcbind.socket
The line Removed '.../rpcbind.socket' can make you think the socket is gone. It isn’t. That line only means the socket won’t start at the next boot. Right now it’s still running, and that’s what the last four lines warn you about: rpcbind.socket is still active, so port 111 stays open until you stop it.
This is socket activation again, the same systemd feature SSH uses. Useful for SSH, sneaky here.
Disable the socket too:
sudo systemctl disable --now rpcbind.socket
Check again, this time without the header line:
sudo ss -H -tulpn '( sport = :111 )'
No output this time: nothing is listening on port 111 anymore.
Rule to remember: When a port stays open after you disable a service, look for a socket unit with systemctl list-sockets. Not every service has one. If yours does, the UNIT column shows its name, and you disable it too.
Remove the package
Disabling a service closes the door. Removing the package takes the door off the building: the program and its files are gone. But if you later install another package that needs it, APT can install it again. That’s one more reason to compare your listening ports after installing new software (we’ll set that up at the end).
Before removing anything, always simulate first:
sudo apt purge -s rpcbind
The -s flag means “simulate”. APT lists everything it would remove, without removing anything. That list includes other packages APT would have to remove because they need rpcbind to work. Read it carefully. If you see a package you need, like nfs-common on a server that mounts network shares, stop and reconsider.
If the list only shows what you expect, remove it for real:
sudo apt purge rpcbind
purge removes the package and its configuration files. remove would leave the configuration files behind.
Now do the same for the forgotten FTP server. Simulate:
sudo apt purge -s vsftpd
Then remove it:
sudo apt purge vsftpd
Finally, clean up packages that were installed only as dependencies and aren’t needed anymore. Simulate this one too:
sudo apt autoremove -s
Read the list. If it only contains packages from the lab, run it for real:
sudo apt autoremove
Make a service local-only
MariaDB is different. The website needs it, so we can’t remove it. But the website runs on the same server, so the database never needs to accept connections from outside.
The fix is to bind it back to 127.0.0.1. Run this command:
sudo sed -i 's/^bind-address.*/bind-address = 127.0.0.1/' /etc/mysql/mariadb.conf.d/50-server.cnf
Confirm the file changed:
grep -n bind-address /etc/mysql/mariadb.conf.d/50-server.cnf
You should see bind-address = 127.0.0.1.
Restart MariaDB to apply the change:
sudo systemctl restart mariadb
The file is only half of the proof. Another configuration file could override it, so ask MariaDB itself which address it’s using:
sudo mariadb -e "SHOW VARIABLES LIKE 'bind_address';"
The Value column should show 127.0.0.1.
Then check the port, which is the real proof:
sudo ss -tulpn '( sport = :3306 )'
The line now shows 127.0.0.1:3306. The door moved inside the building. The website can still use the database, and other machines can no longer connect to it through the server’s network cards.
The same idea works for other common services. On Ubuntu, these are already local-only by default. The danger is a tutorial, or a quick fix late at night, that changes them:
| Service | File | Safe setting |
|---|---|---|
| MySQL / MariaDB | /etc/mysql/mariadb.conf.d/50-server.cnf (MariaDB) | bind-address = 127.0.0.1 |
| PostgreSQL | /etc/postgresql/<version>/main/postgresql.conf | listen_addresses = 'localhost' |
| Redis | /etc/redis/redis.conf | bind 127.0.0.1 -::1 |
If a database really does need to be reached from another machine, don’t open it to everyone. You have three options, from safest to least safe:
- An SSH tunnel. The database stays on
127.0.0.1, and you reach it through your SSH connection. Nothing changes on the database side. - A VPN. Bind the database to the server’s VPN address instead of
127.0.0.1, and allow only VPN clients in the firewall. (A VPN alone isn’t enough here: a database on127.0.0.1can’t be reached through the VPN either.) - A firewall rule. Open the database port directly, but allow only that one client machine’s IP in the firewall. Use this only if the first two aren’t possible: the database port is then reachable on the network, and the firewall rule is the only thing keeping other machines out.
Check that the doors stay closed
A change that disappears after a reboot isn’t a fix. Reboot the VM:
sudo reboot
Log back in and list the ports again:
sudo ss -tulpn
On the lab, the only entries that aren’t local-only should now be:
- SSH on port 22.
nginxon port 80.systemd-networkdon UDP port 68, one line per network card. This is the DHCP client from “The before picture”, which gets the server its IP address. It’s normal, so leave it alone.
Everything else is local-only: on 127.0.0.1, [::1], 127.0.0.53 or 127.0.0.54. Compare that with the output from the start of the guide.
Look from the outside
ss shows the server’s point of view: what’s listening. To see what another machine can actually reach, scan the server from a second machine on the same network. Install nmap on that machine, then run:
sudo nmap -p- <server-ip>
Replace <server-ip> with your VM’s IP address. -p- tells nmap to check all 65,535 TCP ports. Without it, nmap only checks the 1,000 most common ones.
If the network and the firewall let the scan through, nmap should report ports 22 and 80 as open. On my lab, I ran the scan from a second VM on the same network, and that’s exactly what it showed: ports 22 and 80, nothing else.
Keep in mind what this scan does and doesn’t tell you:
- It checks TCP only. UDP scans (
-sU) exist, but they’re slow and less reliable, so we skip them here. - It shows which TCP ports the scanning machine can reach on your VM. A scan from your local network isn’t the same as a scan from the internet, where your router or cloud firewall also plays a role.
- If a firewall blocks a port,
nmapreports it as “filtered” or doesn’t list it, even if a service is listening behind it.
Note: Your VM must be reachable from the other machine for this to work, for example with a bridged or host-only network in your virtualization tool.
Save a baseline for next time
New doors will appear again the next time you install something. Save today’s clean state:
sudo ss -H -tuln | awk '{print $1, $5}' | sort > ~/ports-baseline.txt
After installing new software, compare:
diff ~/ports-baseline.txt <(sudo ss -H -tuln | awk '{print $1, $5}' | sort)
Any line starting with > is a new listening port. Decide about it right away, while you still remember what you installed.
Closing a port vs blocking it with the firewall
These two do different jobs, and you want both. The firewall blocks the door. Closing the port removes it. If the firewall rules are ever lost, changed by mistake, or bypassed, a closed port is still closed. Docker is a common example of the last case: it adds its own firewall rules for container ports, and those rules can let traffic in even when UFW is set to block that port. That gets its own post soon.
Recap
You now know exactly what your server is listening on, and you have a method to keep it that way:
List → Identify → Decide → Close or make local → Check after reboot → Check from outside.
On the lab, that took four packages that listened on all addresses down to one web server on port 80, one database that only the server can use, and SSH. On a real server, the first run of sudo ss -tulpn is often the most surprising five seconds of the day.
When you’re done with the lab, restore the snapshot you took at the start to get a clean VM back.
Want a professional to audit your server? Vigilnix offers server security audits. Get in touch.
In the next post, we’re adding two-factor authentication to SSH, so even a stolen key isn’t enough to get in. Stay tuned.





Leave a Reply