Unix sockets are one of the forms of communication between processes that are most used in Linux.
For this reason, it is often very useful when making diagnostics to
obtain open or listening Unix sockets
.
This will be the objective of this tutorial, which explains how to
obtain a list with all open Unix sockets
and for this we will only need access to the Linux terminal.
How to get Unix sockets open on Linux from the console.
I know that many of you already know in depth the Unix sockets and that you are only interested in how to list them. Therefore, I show you in the first place the command that we will use and then we will make a brief explanation of different concepts for those who are new in the world of Unix sockets.
Command to list the Unix sockets open in the system:
netstat -xl
zeokat@ubuntu:~$ sudo netstat -xl
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 16209 public/flush
unix 2 [ ACC ] STREAM LISTENING 10818 /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 10822 /run/systemd/fsck.progress
unix 2 [ ACC ] STREAM LISTENING 16436 /run/user/1000/systemd/private
unix 2 [ ACC ] SEQPACKET LISTENING 10833 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 16218 private/smtp
unix 2 [ ACC ] STREAM LISTENING 10836 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 16221 private/relay
unix 2 [ ACC ] STREAM LISTENING 16224 public/showq
unix 2 [ ACC ] STREAM LISTENING 16184 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 16180 public/pickup
unix 2 [ ACC ] STREAM LISTENING 16187 public/qmgr
unix 2 [ ACC ] STREAM LISTENING 15597 /var/run/mysqld/mysqld.sock
unix 2 [ ACC ] STREAM LISTENING 16251 private/maildrop
unix 2 [ ACC ] STREAM LISTENING 16254 private/uucp
unix 2 [ ACC ] STREAM LISTENING 16257 private/ifmail
unix 2 [ ACC ] STREAM LISTENING 16260 private/bsmtp
unix 2 [ ACC ] STREAM LISTENING 16263 private/scalemail-backend
unix 2 [ ACC ] STREAM LISTENING 13362 /run/uuidd/request
unix 2 [ ACC ] STREAM LISTENING 13363 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 16266 private/mailman
The explanation of the command is very simple, we use the netstat tool with the following options:
-
x
: Filter the list to show only Unix sockets.
-
l
: shows only the sockets that are listening, also called servers.
Sometimes it is interesting to
know PID of the process or program that owns the socket
, for this we just have to add the
-p
option, so that the command would be:
sudo netstat -xlp
zeokat@ubuntu:~$ sudo netstat -xlp
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 16209 1224/master public/flush
unix 2 [ ACC ] STREAM LISTENING 10818 1/init /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 10822 1/init /run/systemd/fsck.progress
unix 2 [ ACC ] STREAM LISTENING 16436 1233/systemd /run/user/1000/systemd/private
unix 2 [ ACC ] SEQPACKET LISTENING 10833 1/init /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 16218 1224/master private/smtp
unix 2 [ ACC ] STREAM LISTENING 10836 1/init /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 16221 1224/master private/relay
unix 2 [ ACC ] STREAM LISTENING 16224 1224/master public/showq
unix 2 [ ACC ] STREAM LISTENING 16184 1224/master public/cleanup
unix 2 [ ACC ] STREAM LISTENING 16180 1224/master public/pickup
unix 2 [ ACC ] STREAM LISTENING 16187 1224/master public/qmgr
unix 2 [ ACC ] STREAM LISTENING 15597 1031/mysqld /var/run/mysqld/mysqld.sock
unix 2 [ ACC ] STREAM LISTENING 16251 1224/master private/maildrop
unix 2 [ ACC ] STREAM LISTENING 16254 1224/master private/uucp
unix 2 [ ACC ] STREAM LISTENING 16257 1224/master private/ifmail
unix 2 [ ACC ] STREAM LISTENING 16260 1224/master private/bsmtp
unix 2 [ ACC ] STREAM LISTENING 16263 1224/master private/scalemail-backend
unix 2 [ ACC ] STREAM LISTENING 13362 1/init /run/uuidd/request
unix 2 [ ACC ] STREAM LISTENING 13363 1/init /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 16266 1224/master private/mailman
In this case I used sudo to execute the command, but surely we do not have sufficient permissions to see all the PIDs.
Introduction to Unix sockets.
To get a little deeper into the world of
Unix sockets
, let's try to make a simple and affordable explanation. For this,
we must start by explaining what a socket is and we could define it as an abstract method that allows data flow
. Generally the concept of socket is associated with that of a plug that contacts two programs.
Now we can define as
a Unix socket a method that allows communication between two processes of the same equipment
. In other words, it allows the flow of data between the two processes in an orderly and reliable way within the operating system.
Communication can be
bidirectional
when so-called
stream sockets
are used and
unidirectional
when
datagram sockets
are used.
A very important peculiarity of UNIX sockets is that they
use the file system as a namespace address
, so a UNIX socket is identified by its path name.
This allows us to use the file permission control system and use them in conjunction with the socket to set permissions.
Finally, I propose as an example of a UNIX socket when we configure a Nginx or
Apache
server with
PHP-FPM
, where we usually use a socket for the server and the PHP processing module to communicate.