What is a Unix socket? Creating a temporary .sock file and observing how it differs from TCP ports

Linux・CLI・DevOpsカテゴリを表すパンダのイラスト Linux / CLI / DevOps

About this article
This article is created using an automated generation workflow utilizing generative AI. Based on Linux Unix domain socket documentation and TCP specifications, it outlines a procedure to observe the true nature of .sock using only a temporary directory, without touching production PHP-FPM sockets.

Verification Status: 📘 Official information verified, local execution unverified

The .sock entries found in Nginx and PHP-FPM configurations are not necessarily standard configuration files. Unix domain sockets are sockets that can be used for inter-process communication within the same host, and pathname sockets use a path on the file system as a name instead of an IP address and port.

Get It Running

Do not touch production /run/php/*.sock./tmp Use a dedicated temporary directory for

with only the Python standard library.

TMPDIR="$(mktemp -d)"
SOCK="$TMPDIR/demo.sock"
export SOCK

echo "[START] socket path=$SOCK"

python3 - <<'PY'
import os, socket
path = os.environ['SOCK']
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(path)
server.listen(1)
print('[LISTEN]', path, flush=True)
conn, _ = server.accept()
data = conn.recv(1024)
print('[RESULT] recv =', data.decode(), flush=True)
conn.sendall(b'ACK_UNIX')
conn.close()
server.close()
print('[SUCCESS] server finished', flush=True)
PY

[LISTEN]Terminal 1:SOCK Once output appears, connect from Terminal 2. Insert the path displayed in Terminal 1 into

export SOCK='/tmp/表示されたディレクトリ/demo.sock'

python3 - <<'PY'
import os, socket
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(os.environ['SOCK'])
client.sendall(b'HELLO_UNIX')
print('[RESULT]', client.recv(1024).decode())
client.close()
PY

.unix(7)Linux Unix domain sockets are described in

man7.org — unix(7)

Check this

ls -l "$SOCK"

Verify from another terminal while the server is listening.

flowchart LR
    C[Client process] --> U[Unix domain socket path]
    U --> S[Server process]
    C -. IP/portを使わない .-> S

You can observe that the initial file type differs from a regular file. Although the path is visible on the filesystem, it is not treated as a standard text file for storing content.

How is it different from TCP localhost?127.0.0.1:9000With TCP, communication endpoints are typically represented by an IP address and port, such as [::1]:9000 or /run/php/php-fpm.sock. With pathname Unix sockets, a path like

serves as the communication endpoint.

RFC Editor — RFC 9293 Transmission Control Protocol

While Unix sockets may be used in configurations communicating exclusively within the same host, do not simplify by assuming they are "always faster than TCP" or "always the right choice." Operations, permissions, and container boundaries are also design factors.

Change One Thingdemo.sockChange only the socket path name from app.sock to

SOCK="$TMPDIR/app.sock"

. This reconfirms that the name is a path, not an IP/port.

Cleanup

rm -f "$SOCK"
rmdir "$TMPDIR" 2>/dev/null || true
echo '[CLEANUP] temporary socket removed'

Remove the temporary socket and directory after the server shuts down.

Because abnormal process termination can leave the socket path behind, production systems must be designed to handle cleanup before startup and upon termination.

What should I check in PHP-FPM?listenThere are configurations where the PHP-FPM

  • points to a pathname socket, and the Nginx FastCGI configuration points to the same socket. When troubleshooting, examine the following separately:

  • Whether the socket path exists

  • Owner, group, and mode

  • Whether the PHP-FPM process is listening

  • Whether Nginx is referencing the same path

Do not delete production sockets for testing purposes.sockThis demo does not alter production settings; it solely reproduces the aspect where

serves as the communication endpoint.

  • /runBest Practices for Production Use

  • Never arbitrarily delete production

  • sockets

  • Avoid recklessly setting socket permissions to 777

  • Verify the service user and group

Verify path-sharing prerequisites in containers or cross-host communication

man7.org — unix(7)

RFC Editor — RFC 9293.sockConclusionlsPathname Unix domain sockets allow communication partners on the same host to be represented by a path on the filesystem. By creating a temporary

Document information

Article title
What is a Unix socket? Creating a temporary .sock file and observing how it differs from TCP ports
Published
Updated
Source
https://papanda925.com/?p=15368&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL