How to Check Open Ports on a Linux VPS with ss and nmap

You can check the ports on a Linux VPS from two places. On the server, ss lists the sockets the kernel holds. It shows the same kind of information as netstat. From another machine, nmap shows which of those ports a remote client can reach.

nmap calls a port open when an application is accepting TCP connections, UDP datagrams or SCTP associations on it. A firewall between the scanner and the server can hide that same port, so the two lists do not always match. Run both tools, then compare the results.


Check Listening Ports Locally with ss

ss is part of the iproute2 project. If you run it with no options, it lists only non-listening sockets with established connections. A plain ss never shows the ports your services are waiting on. The -l flag shows only listening sockets.

To list every TCP socket and every UDP socket:

1ss -t -a
2ss -u -a

-a shows both listening and non-listening sockets. For TCP, the non-listening sockets are established connections, so on a busy web server this output is long. For a port audit, you want the listeners. Combine the flags into one command:

1sudo ss -tulnp
  • -t and -u select TCP and UDP sockets.
  • -l keeps only listening sockets.
  • -n stops ss from resolving service names, so ports show as numbers.
  • -p shows the process using each socket.

Run it with sudo. Without root, -p shows only your own processes, and sockets owned by other users carry no process info. Daemons such as sshd then show up with an empty process column.

To check address families separately, add -4 or -6. They are aliases for -f inet and -f inet6. Run sudo ss -tlnp -4, then sudo ss -tlnp -6, and look over the IPv4 and IPv6 listeners one at a time.

Two more flags help when you script the check. -H removes the header line, so ss -tlnH | wc -l counts TCP listeners without an extra line for the header. -e adds detailed socket information, including the user ID the socket belongs to and its inode number. The user ID is useful when a socket belongs to a service account and you want to know which one.


Filter ss Output by Port and State

A filter goes after the options. The sport and dport predicates compare the source or destination port against a value. The operator can be <, <=, =, !=, >= or >, and comparisons follow normal arithmetic rules. Each operator also has a word alias, such as ge for >=. If you leave the operator out, ss assumes =.

Put the filter in quotes so the shell does not read > as a redirect:

1ss -tln 'sport >= :1024'

That lists TCP listeners on port 1024 and above.

To show established SSH sessions:

1ss -o state established '( dport = :ssh or sport = :ssh )'

state established limits the output to established sockets, and -o adds timer information. The parentheses group two predicates joined by or. sport = :ssh matches sessions into this server's SSH daemon. dport = :ssh matches outgoing SSH connections from the VPS to other hosts.

* works as a wildcard for either the address or the port. An IPv6 address has to go inside [ and ] so ss can tell it apart from the port separator:

1ss -tln 'src 127.0.0.1:*'
2ss -tln 'src [::1]:*'

These two commands list TCP listeners bound to the IPv4 and IPv6 loopback addresses. Keep this list nearby when you scan from outside, because these are the ports you expect nmap not to see.

For a quick count before reading any detail, ss -s prints summary statistics.

Do not use -K in a checking command. It tries to forcibly close sockets, and on a live server a mistyped -K with a broad filter would cut active connections.


Scan Your VPS from Outside with nmap

Scan only machines you own or have written permission to scan. Getting written authorization from the target network's owners before any scan is the safest course. Port scanning without authorization is sometimes against a provider's acceptable use policy (AUP). That applies to the network you scan from as well as the one you scan. To try a scan first, use scanme.nmap.org, which you have permission to scan. Run no more than a dozen scans against it per day.

Run the scan from a second machine on a different network, such as another VPS or a home connection. Everything nmap reports comes from packets returned by the target or by firewalls in front of it. The scan has to take the same path a remote client's packets would.

Start with the practice host. -v turns on verbose mode:

1nmap -v scanme.nmap.org

Once that works, replace the target with your VPS's public address. Below, 203.0.113.10 stands in for it:

1nmap -v 203.0.113.10

SYN scan versus connect scan

By default, nmap runs a SYN scan. If the user cannot send raw packets, which on Unix requires root, nmap runs a connect scan instead. Most scan types need privileges. An unprivileged user can run only connect scans and FTP bounce scans.

The two scans behave differently on the target. A SYN scan never completes a TCP connection. A SYN/ACK reply means the port is open, and an RST (reset) means nothing is listening. If no reply arrives after several retransmissions, nmap marks the port filtered. A connect scan uses a system call that completes the connection to each open port. Because of that, the target is more likely to log it.

Run nmap with sudo to get the SYN scan. OS detection with -O also needs root. To pair it with a SYN scan against one VPS:

1sudo nmap -sS -O 203.0.113.10

Choosing ports

By default, nmap scans the 1,000 most common ports for each protocol. When you audit your own server, scan every port: -p- scans ports 1 through 65535. -F goes the other way and cuts the scan to 100 ports.

1nmap -p- 203.0.113.10
2nmap -F 203.0.113.10

-p also takes a list with protocol qualifiers: U: marks UDP ports and T: marks TCP ports. If you are not sure how your shell will expand the -p argument, put it in quotes.

Identify services and save results

-sV turns on version detection. nmap tries to work out the service protocol (such as SSH or HTTP), the application name (such as Apache httpd) and the version number. A port number alone does not tell you the service, because services run on unusual ports too. To check a set of ports on one VPS:

1nmap -sV -p 22,53,110,143,4564 203.0.113.10

nmap runs version detection on each of those ports that it finds open. Replace the port list with the TCP ports sudo ss -tulnp showed on the server, so each TCP listener gets checked from outside. This command sends TCP probes only. The UDP listeners need -sU and a U: port list, shown in the UDP section.

Three output options help with an audit. --reason shows why each port got its state. --open shows only hosts with at least one open, open|filtered or unfiltered port, and only ports in those states. -oN writes normal output to a file you can compare against later scans:

1sudo nmap -sS -sV -p- --reason -oN vps-scan.txt 203.0.113.10

That command leaves out --open on purpose. --open hides closed and filtered ports, and when you compare against ss, a filtered result is one of the answers you need to see.

By default, nmap only port-scans hosts it finds to be up. If your firewall drops nmap's discovery probes, nmap treats the VPS as down and skips the port scan. -Pn skips host discovery entirely:

1sudo nmap -Pn -sS -p- 203.0.113.10

Read nmap Port States

nmap sorts ports into six states: open, closed, filtered, unfiltered, open|filtered and closed|filtered. Each state describes how nmap sees the port from where the scan ran. A scan from the target's own network can show 135/tcp as open. A scan with the same options at the same time from across the Internet can show that port as filtered.

Four of the states come up in most audits:

  • open: an application is accepting connections or datagrams on the port.
  • closed: the port is reachable and answers nmap's probes, but no application is listening.
  • filtered: packet filtering keeps the probes from reaching the port, so nmap cannot tell whether it is open.
  • open|filtered: nmap cannot tell open from filtered, because the scan type gets no response from open ports. UDP scans sort ports this way.

Scanning UDP Ports

DNS, SNMP and DHCP are three of the most common UDP services. They use the registered ports 53, 161/162 and 67/68. UDP scanning needs privileges, like every scan type except connect and FTP bounce.

nmap runs one scan method at a time, with one exception. A UDP scan (-sU) and one SCTP scan type can be combined with one TCP scan type. That lets you check TCP and UDP in a single run, with a mixed -p list:

1sudo nmap -sS -sU -p 'U:53,111,137,T:21-25,80,139,8080' 203.0.113.10

Replace both lists with what sudo ss -tulnp showed. The U: ports are the UDP listeners and the T: ports are the TCP listeners.

Silence is harder to read over UDP. If a UDP port sends no reply after retransmissions, nmap marks it open|filtered. Adding -sV helps separate the open ports from the filtered ones.

UDP scans are also slow against Linux. The Linux 2.4.20 kernel limits destination unreachable messages to one per second. At that rate, a scan of all 65,536 ports takes more than 18 hours. To speed up UDP scans, scan more hosts in parallel, start with the popular ports, or use --host-timeout to skip slow hosts.

For a single VPS, start with the ports you know. sudo ss -ulnp already shows the UDP ports in use, so scan those first and leave the full range for later.


Compare the Two Views

Put the ss list next to the nmap results, port by port:

ss on the VPSnmap from outsideWhat it means
ListeningopenRemote clients can reach the service. Make sure you meant to expose it.
ListeningfilteredPacket filtering stops probes before they reach the port.
Not listeningclosedThe port is reachable, but nothing is listening on it.

An open result for a service that only needs local clients is the one to fix first. Look for databases, admin panels and anything that ss -tln 'src 127.0.0.1:*' did not list as loopback-only. Use the -p column from sudo ss to find the program behind the port.

For UDP, a silent port shows as open|filtered rather than filtered, so a UDP listener with no reply can still be reachable. Each nmap result also holds only for the network you scanned from, as the 135/tcp example shows: a port that is filtered from your home connection can be open from another network. After you change a firewall rule, scan again from the same machine and compare the new -oN file with the old one.


References

Posts in this series