SaferComputing.com

Reference · Network exposure

Open-port self-audit: what listens on your computer and what the internet can reach

List every listening port with ss -tulpn, lsof -i -P -n and Get-NetTCPConnection -State Listen, read 0.0.0.0 against 127.0.0.1, check ufw status numbered and Windows firewall rules, then test your public IP from outside.

Published
Updated
Last checked
Applies to
Windows 11, macOS, Ubuntu and other Linux systems, home routers
Technical specification
Target artifactEvery TCP and UDP port the computer listens on, and the firewall and router rules that decide whether other machines can reach it.
Default behaviourPrograms open listening ports as they need them. A port on 0.0.0.0, :: or * listens on every interface, and the internet can reach it when a public address or a router port forward leads to it and no firewall stops it.
Safe stateOnly services you meant to offer listen on network addresses, everything else on 127.0.0.1; the inbound firewall default is deny; no router port forwards you did not create; a test from outside reaches only the ports you meant to publish.
Check on Linuxsudo ss -tulpn
Check on macOSsudo lsof -nP -iTCP -sTCP:LISTEN
Check on WindowsGet-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess | Sort-Object LocalPort
Check from another deviceTest-NetConnection -ComputerName PUBLIC_IP -Port PORT in PowerShell, or nc -vz -w 5 PUBLIC_IP PORT, from a different network
Fixbind each service you do not need on the network to 127.0.0.1 or stop it, then make the inbound default deny, for example sudo ufw default deny incoming on Ubuntu

Step 1: list what listens

Linux
Linux, as a user with sudo
sudo ss -tulpn

-t and -u show TCP and UDP, -l only listening sockets, -p the program that owns each one, and -n numbers instead of names. Without sudo, the program column is empty for other users' services.

macOS
macOS Terminal
sudo lsof -nP -iTCP -sTCP:LISTEN
sudo lsof -nP -iUDP

-n and -P print addresses and port numbers instead of names, which is faster and clearer. lsof -i -P -n without the filters lists every network connection, listening or not.

Windows
Windows PowerShell
Get-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess, @{ Name = 'Process'; Expression = { (Get-Process -Id $_.OwningProcess).ProcessName } } | Sort-Object LocalPort
Get-NetUDPEndpoint | Select-Object LocalAddress, LocalPort, OwningProcess | Sort-Object LocalPort

Windows normally shows some system services, such as file sharing on port 445, unless its Server service is turned off, and remote procedure calls on port 135. What matters is whether the firewall lets anyone reach them, which Step 3 checks.

Step 2: read the addresses

Local addressWho can connect
127.0.0.1, ::1This computer only
0.0.0.0, ::, [::], *Any network the computer is on, subject to the firewall
A specific address, such as 192.168.1.20 or a public addressAnyone who can reach that address

For each port on a network address, ask whether you meant to offer that service, and to whom. If not, set it to 127.0.0.1 in the program's settings, or stop it.

Step 3: check the firewall

Ubuntu with UFW
Ubuntu, as a user with sudo
sudo ufw status verbose
sudo ufw status numbered

The verbose output should include Default: deny (incoming). In the numbered list, delete an ALLOW rule you did not mean with sudo ufw delete RULE_NUMBER. Ports published by Docker are not covered by these rules; see Docker published ports.

Windows Defender Firewall
Windows PowerShell
Get-NetFirewallProfile -PolicyStore ActiveStore | Select-Object Name, Enabled, DefaultInboundAction
Get-NetFirewallRule -Direction Inbound -Action Allow -Enabled True | Select-Object DisplayName, Profile | Sort-Object DisplayName

Every profile should be enabled. -PolicyStore ActiveStore reads the settings in force, Group Policy included; without it, the command reads only the locally stored settings. The second command lists the inbound rules that let traffic in; look for programs and ports you do not recognize, especially on the Public profile.

macOS
macOS Terminal
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

The macOS firewall is off until you turn it on in System Settings. It decides per application rather than per port.

Step 4: test from outside

A listening port and a firewall rule describe what should happen; a test from outside shows what does.

  1. Find your public address: on the router's status page at home, or in the hosting provider's control panel for a server.
  2. Look at the router's port forwarding and UPnP pages. UPnP lets programs open ports on the router by themselves; turn it off unless something you rely on needs it. The checks for the router itself are on Home router check, and those for a NAS behind it on Synology and QNAP NAS on the internet.
  3. From a different network, for example a phone's hotspot, test each port you care about:
Windows PowerShell, on a computer on another network
Test-NetConnection -ComputerName PUBLIC_IP -Port 3389
macOS or Linux terminal, on another network
nc -vz -w 5 PUBLIC_IP 3389

TcpTestSucceeded : True, or a line ending in "succeeded!" from nc, means the port is reachable from the internet. Test from outside your own network: a test of your public address from inside it can pass or fail for reasons that have nothing to do with the internet.

Note IPv6

A router's address sharing hides IPv4 devices behind one public address, but with IPv6 each device can have its own public address. If a service listens on :: and the router's IPv6 firewall allows inbound traffic, it can be reachable directly. Check the router's IPv6 firewall setting, and test the computer's IPv6 address as well.

Sources