maxuru ~ % cat memcached/bind-to-localhost
Bind Memcached to localhost
/etc/memcached.conf-l 127.0.0.1-l ::1sudo systemctl restart memcachedOn RHEL and Fedora the same setting goes in OPTIONS in
/etc/sysconfig/memcached; in a container it goes in the command:.
Why this is the first control
Section titled “Why this is the first control”Because in memcached’s default build there is nothing behind it. No password, no ACL, no protected mode. A TCP connection to 11211 is a complete grant:
# From anywhere that can reach the portprintf 'stats items\r\n' | nc target 11211 # what keys existprintf 'get session:abc123\r\n' | nc target 11211 # read a valueprintf 'flush_all\r\n' | nc target 11211 # drop the entire cacheNone of that requires a credential, because there is no credential to require. Authentication is available — see require authentication — but it is off by default and, for SASL, needs a binary built for it. Until then the listening address is the access control.
The upstream default is INADDR_ANY, which is every interface the host has. The
help text says so plainly: -l, --listen=<addr> interface to listen on (default: INADDR_ANY).
Loopback, or a private address
Section titled “Loopback, or a private address”If the only clients are on the same host — the common case for a single-server application — loopback is correct and complete:
-l 127.0.0.1-l ::1Include the IPv6 line. A host that resolves localhost to ::1 will fail
to connect if only the IPv4 loopback is bound, and the usual reaction to that
failure is to widen the binding rather than add one line. The Debian package
ships both for this reason.
If clients are on other hosts, bind the specific private address — never
0.0.0.0:
-l 10.0.1.20Binding a private address is not a substitute for a firewall. It means the
daemon ignores traffic arriving on other interfaces; it does not stop anything
that can already route to 10.0.1.20. On a cloud network where the whole VPC
can reach that address, you still need a security group.
The Unix socket option
Section titled “The Unix socket option”If every client is genuinely local, memcached can skip the network entirely:
-s /var/run/memcached/memcached.sock-a 0770The help text is explicit that -s disables network support — there is no
port at all, and access is decided by filesystem permissions with -a as the
octal mask. This is the strongest available answer for a single-host deployment,
and it removes the exposure rather than narrowing it.
It rules out remote clients completely, and not every client library speaks Unix sockets, so check yours first.
Related
Section titled “Related”- Require authentication — the layer that should be behind the binding.
- Exposed to the internet — what an open 11211 has actually been used for.