Unix domain sockets, often called local sockets, provide inter-process communication on the same host. Unlike network sockets that use IP addresses and ports, they use filesystem paths for addressing. While mkfifo
creates named pipes, we need different tools for creating sockets.
Surprisingly, there isn't a direct command-line tool like mkfifo
for creating standalone domain sockets. This is because sockets are typically created and bound by server processes during runtime rather than being pre-created in the filesystem.
Here are practical ways to create Unix domain sockets from the command line:
1. Using socat (Socket Cat)
The socat
utility is the most flexible solution:
# Create and listen on a socket socat UNIX-LISTEN:/tmp/mysocket,fork - # Connect to the socket in another terminal socat - UNIX-CONNECT:/tmp/mysocket
2. Using netcat (nc)
Some netcat
implementations support Unix sockets:
# OpenBSD netcat example nc -lU /tmp/mysocket # Connect with nc -U /tmp/mysocket
3. Using Python One-liners
For systems without socat or suitable netcat:
# Create socket (Python 3) python3 -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/mysocket')" # Test connection python3 -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.connect('/tmp/mysocket')"
Since sockets appear as files, manage permissions carefully:
# Set appropriate permissions after creation chmod 600 /tmp/mysocket chown user:group /tmp/mysocket
Here's how to create a simple echo server:
# Server side socat UNIX-LISTEN:/tmp/echo.sock,fork exec:'/bin/cat' # Client side echo "Hello Socket" | socat - UNIX-CONNECT:/tmp/echo.sock
Remember to remove socket files manually when done:
rm -f /tmp/mysocket /tmp/echo.sock
- Ensure the parent directory exists and is writable
- Check for existing sockets with the same path
- Be aware that sockets persist after program termination
- Consider abstract sockets (Linux) for temporary use
For systemd systems, you can create socket-activated services:
# Example systemd socket unit [Unit] Description=My Socket [Socket] ListenStream=/tmp/mysocket SocketUser=myuser [Install] WantedBy=sockets.target
While mkfifo
creates named pipes (FIFOs) for inter-process communication, Unix domain sockets offer more advanced features like bidirectional communication and connection-oriented modes. For creating sockets directly from the command line, we need different approaches.
The most straightforward method is using socat
, a powerful networking utility:
# Create a listening socket
socat UNIX-LISTEN:/tmp/mysocket.sock,fork -
# Connect to the socket from another terminal
socat - UNIX-CONNECT:/tmp/mysocket.sock
When socat isn't available, Python provides a quick solution:
python3 -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/pysocket.sock')"
Netcat (nc) can also create sockets, though with some limitations:
# GNU netcat version
nc -lU /tmp/ncsocket.sock
# BSD netcat version
nc -Ul /tmp/ncsocket.sock
Remember these important aspects when working with named sockets:
- Socket files don't persist after system reboots
- Permissions must be set correctly (use
chmod
) - Clean up sockets after use (
rm /tmp/mysocket.sock
) - Sockets appear in filesystem but don't consume disk space
Here's how to create a socket with specific permissions:
# Create restricted socket
socat UNIX-LISTEN:/tmp/secure.sock,fork,umask=0177 -
# Verify permissions
ls -l /tmp/secure.sock
# Output should show: srw-------