Skip to main content
  1. Braindump/

Show connections and open sockets/ports

2 mins
Marcel Ryser
Author
Marcel Ryser
Software architect and tech enthusiast from Berne, Switzerland

When developing or using services it’s often handy to list all listening sockets or connections on the machine. We have a handy tool at our disposal on most distributions:

On older distributions there was the netstat tool, but on newer distribution it’s been replaced by ss.

$ ss

Netid    State    Recv-Q   Send-Q                     Local Address:Port       Peer Address:Port                 
u_seq    ESTAB    0        0                                 @0001f 101956                * 101957               
u_seq    ESTAB    0        0                                 @0001c 36327                 * 36328                
u_str    ESTAB    0        0                                      * 605506                * 605507               
u_str    ESTAB    0        0        /var/run/dbus/system_bus_socket 40972                 * 39126                
u_str    ESTAB    0        0                                      * 39939                 * 39938                
u_str    ESTAB    0        0                                      * 29377                 * 27430                
u_str    ESTAB    0        0                                      * 620557                * 620556               
u_str    ESTAB    0        0                                      * 602348                * 602349               
u_str    ESTAB    0        0                                      * 34852                 * 32327                
u_str    ESTAB    0        0                     @/tmp/.X11-unix/X0 27347                 * 33301                
u_str    ESTAB    0        0                     /run/user/1000/bus 34057                 * 34056                
u_str    ESTAB    0        0                    @/tmp/dbus-RiJ4OUll 1096107               * 1100088              
u_str    ESTAB    0        0                                      * 681622                * 681621               
u_str    ESTAB    0        0                                      * 107612                * 106793               
u_str    ESTAB    0        0                                      * 36029                 * 34234                
u_str    ESTAB    0        0            /run/systemd/journal/stdout 30269                 * 35283                
u_str    ESTAB    0        0                                      * 35859                 * 32413                
....

a few of my most used options:

       -n, --numeric
              Do not try to resolve service names.

       -a, --all
              Display both listening and non-listening (for TCP this means established connections) sockets.

       -l, --listening
              Display only listening sockets (these are omitted by default).

       -p, --processes
              Show process using socket.

       -t, --tcp
              Display TCP sockets.

       -u, --udp
              Display UDP sockets.

       -x, --unix
              Display Unix domain sockets (alias for -f unix).

Examples:

  • Show all listening tcp sockets (use numeric instead of service names for ports):

    ss -altn
  • Show all processes with listening tcp sockets (use numeric instead of service names for ports):

    ss -altnp

Be aware that you’ll have to execute with sudo for some operations, like showing processes.