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. Use a dedicated temporary directory under /tmp 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

Once the [LISTEN] output appears in Terminal 1, connect from Terminal 2 using the displayed path:

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

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

Observing the Socket File

Verify the file type from another terminal while the server is listening:

ls -l "$SOCK"

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.

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

Comparison with TCP Localhost

How is it different from TCP localhost? With TCP, communication endpoints are typically represented by an IP address and port, such as 127.0.0.1:9000 or [::1]:9000. With pathname Unix sockets, a path serves as the communication endpoint.

The current specification for TCP itself is defined in RFC 9293 (Transmission Control Protocol).

Best Practices and Design Factors

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.

For example, changing the socket path name from demo.sock to app.sock:

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 to Check in PHP-FPM and Nginx

There are configurations where the PHP-FPM listen 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

Best Practices for Production Use:

  • Never arbitrarily delete production sockets under /run
  • Avoid recklessly setting socket permissions to 777
  • Verify the service user and group
  • Design cleanup procedures for stale sockets

Official and Primary Sources:

Conclusion: Pathname Unix domain sockets allow communication partners on the same host to be represented by a path on the filesystem. By creating a temporary .sock file, we can safely observe their fundamental behaviors.

この記事の更新履歴

この記事は、生成AIを活用した自動レビュー・更新フローにより内容を見直し、必要な修正を反映しています。

2026年9月23日

  • 変更文章の一部が不自然に切り離されていた見出しや段落構造を正しく修正しました。

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