Linux list open ports to the outside

Solution 1:

You may not be finding an easy answer to this, because it's a more complex question than you might realize. There's at least 3 possible points of interest to look at:

  1. The ports being listened for on the server. netstat -an | grep LISTEN will give you a general idea. Look for source addresses of 0.0.0.0 or specific "outside" interfaces (don't forget IPv6 addresses if applicable).

  2. Server firewall (commonly iptables). iptables -l will give some idea of what traffic is being allowed. But also, it should show you any NATs/port redirects being done at the server level. For example, your port 3309 might be redirected to 127.0.0.1:3309. So even though your mysql might only be listening on localhost, it WOULD be technically accessible from the "outside" world.

  3. Your edge firewall. This is your internet router/gateway. Easiest thing to do here is to go to a server in the "outside world" (whether that's the open internet, somewhere else on your network, etc. - that wasn't clearly defined in the question), and run nmap <your-external-ip> and see what it reports as open. The downside of doing this is if you don't have a dedicated IP for your server, you're probably going to see a lot of stuff that doesn't apply to your specific server.

Those 3 things will answer most of your questions, but I'm sure others will have more ideas/suggestions.

Solution 2:

Hi and welcome to ServerFault.

How about this?

ss -tulpen | grep -vEe "\s+127[.]|::1"

This drops 127.0.0.0/8 and ::1 addresses from the output.

While this does list all TCP and UDP ports listening on addresses reachable on this box, it doesn't tell you if they are actually reachable. A firewall (IPTables/Netfilter, external device) or security policy (SELinux, SystemD, tcpwrappers) might still block access.

edit If you want to know that ports are indeed accessible from the outside world, place a host in said "outside world" (e.g. the Internet) and run a port scanner (like nmap) on that host targeting your server.

Sample for nmap:

sudo nmap -sS -p1-65535 <your_server's_ip>