How to connect to a Unix socket with Netcat

Local daemons sometimes expose a Unix-domain socket instead of a TCP host and port, so a normal nc 127.0.0.1 9000 probe cannot reach them. Netcat can connect to the socket pathname directly when the installed implementation supports the -U option.

OpenBSD Netcat from netcat-openbsd on Ubuntu 26.04 accepts this command form. A one-shot listener creates /tmp/netcat-demo.sock, a client sends one test line to that pathname, and -N closes the client side after the piped input reaches end-of-file.

The command form uses a stream Unix socket. Add -u only when the target service documents a datagram socket, and avoid sending state-changing protocol messages to a production socket unless the service owner has confirmed the request is safe.

Steps to connect to a Unix socket with Netcat:

  1. Check that the active Netcat build supports Unix-domain sockets.
    $ nc -h
    OpenBSD netcat (Debian patchlevel 1.234-1)
    usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
    ##### snipped #####
    		-U		Use UNIX domain socket
    ##### snipped #####

    If the help text does not show -U, install OpenBSD Netcat before continuing.

  2. Start a one-shot Unix socket listener in the first terminal.
    $ nc -lU /tmp/netcat-demo.sock

    The listener creates /tmp/netcat-demo.sock and waits for one client connection. Leave this terminal open while sending the test line from another terminal.

  3. Confirm that the socket file appeared from the second terminal.
    $ ls -l /tmp/netcat-demo.sock
    srwxr-xr-x 1 root root 0 Jun  7 23:58 /tmp/netcat-demo.sock

    The leading s in the file mode marks the path as a socket, not a regular file.

  4. Send a test payload to the socket from the second terminal.
    $ printf 'unix socket check\n' | nc -N -U /tmp/netcat-demo.sock

    -N lets the listener finish after printf reaches end-of-file.

  5. Confirm that the first terminal printed the payload.
    $ nc -lU /tmp/netcat-demo.sock
    unix socket check

    The matching line confirms that Netcat reached the Unix-domain socket path.

  6. Remove the temporary socket file.
    $ rm -f /tmp/netcat-demo.sock

    Do not remove a production service socket. Deleting its pathname can break local clients until the daemon recreates it.