166

I’m running a Debian Squeeze web server. I’ve installed memcached on it, and configured memcached to listen on a Unix domain socket (at /tmp/memcached.sock), as it only needs to receive messages from the website, which lives on the same server.

It seems to be working fine, but I’d also like to communicate with memcached via the shell, to check that it’s doing what I think it’s doing.

memcached accepts messages via a simple ASCII protocol (if I understand correctly). If it was listening on TCP/IP, I could send messages to it via e.g. nc:

$ echo "stats settings" | nc localhost 11211

But I can’t figure out how to send that text to the domain socket instead.

On my laptop (which runs OS X Lion), both nc and telnet have options (-U and -u respectively) to use domain sockets. However, on my Debian Squeeze web server, these options aren’t present.

nopcorn
  • 9,559
  • 4
    Not exactly answering this question, but also if you need a special format on your requests, e.g., using REST, you can use other tools such as curl instead to get it done for you automatically. E.g.: curl --unix-socket /var/run/docker.sock http://localhost/images/json | jq – aderchox Nov 10 '21 at 08:23
  • @aderchox, I believe you meant HTTP(S). REST does not imply HTTP. – Kuchara Oct 27 '22 at 13:58

6 Answers6

142

With socat (a 'bidirectional data relay between two data channels') you can connect to the unix domain socket like this:

$ socat - UNIX-CONNECT:/tmp/memcached.sock
maxschlepzig
  • 57,532
121

With netcat-openbsd, there is a -U option. If you don't have it, you probably have netcat-traditional installed instead; I'd suggest switching.

Example command: nc -U /var/run/socket

Dej
  • 212
derobert
  • 109,670
  • 1
    so netcat will create a new file at /var/run/socket? Is there a way to reuse an existing file? – Alexander Mills Jun 04 '19 at 19:22
  • 6
    @AlexanderMills If you tell it to listen (-l), it'll create the file. Otherwise, it expects the socket to already exist (and already have something listening on it). So the example command in the answer does not create the file. – derobert Jun 05 '19 at 18:51
36

netcat-openbsd supports connecting to UNIX-domain sockets. Using this you can connect to either a UNIX-domain stream socket or a UNIX-domain datagram socket, and therefore you have to tell the socket's type to netcat.

for example, /dev/log file in Linux is a UNIX-domain datagram socket socket, thus nc -U /dev/log won't work. Instead use nc -uU /dev/log. Using -u along with -U tells netcat that it is a UNIX-domain datagram socket.

nc -U /tmp/socket  #Connect to UNIX-domain stream socket
nc -uU /tmp/socket #Connect to UNIX-domain datagram socket


Similarly, while using socat, use UNIX-CLIENT option. Using this option you can connect to both UNIX-domain stream and UNIX-domain datagram sockets. From its man page (man socat), "It first tries to connect and, if that fails, assumes it is a datagram socket, thus supporting both types".

socat - UNIX-CLIENT:/dev/socket #connect to UNIX-domain socket, irrespective of its type
  • 1
    Excellent detail, regarding the distinction of socket types. Adding -l will create it, e.g. sudo nc -luU /tmp/socket #create and listen to unix datagram socket. – Nagev Nov 11 '20 at 18:31
19

You can use socat on Debian. To install it:

# apt-get install socat
rahmu
  • 20,023
sendmoreinfo
  • 2,573
  • That looks pretty good. I don’t appear to have socat installed on my server, but it’s certainly available: http://packages.debian.org/squeeze/socat – Paul D. Waite Dec 13 '11 at 22:30
17

If you happen to be on a system that only has busybox installed, it may look like it doesn't support unix domain sockets (the -U option doesn't exist) but I found the following works:

$ echo "stats slabs" | nc local:/tmp/memcached.sock
STAT active_slabs 0
STAT total_malloced 0
END

This can be really helpful for creating health checks for alpine containers.

Greg Bray
  • 399
  • 1
    Could you add the required configuration options for this to work? Enabling CONFIG_FEATURE_UNIX_LOCAL is not sufficient. I also played with CONFIG_NC_EXTRA and CONFIG_NC_110_COMPAT but neither of that helped. – yman Apr 02 '20 at 08:17
  • 1
    @yman I was just using a precompiled binary in an Alpine container. I think the config settings were https://git.alpinelinux.org/aports/tree/main/busybox/busyboxconfig or https://git.alpinelinux.org/aports/tree/main/busybox/busyboxconfig-extras – Greg Bray Apr 02 '20 at 08:23
  • 1
    Seems both NC_EXTRA and NC_110_COMPAT config options need to be enabled, of course together with CONFIG_FEATURE_UNIX_LOCAL. Please consider extending the answer. – yman Apr 02 '20 at 09:32
9

Send the request "GET" to the socket "/tmp/server.sock" and print the result:

printf GET | socat UNIX-CONNECT:/tmp/server.sock -
ceving
  • 3,579
  • 5
  • 24
  • 30