Is there a file for each socket?

A socket is a file. But not all files have names. Here are a few examples of files that don't have names:

  • Any file that used to have a name, and is now deleted, but is still opened by a program.
  • An unnamed pipe, such as one created by the | shell operator.
  • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)

Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.


What and where is file per each socket?

"Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.

As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.

So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().


man 7 unix:

The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is independent of the file system.

I.e. not every socket can be seen as a file (in the sense of "no file without a file name").

But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.