Restricting Emacs server-mode socket access to a single user on a shared host
0 reputation · 27 May 2025, 17:56 UTC
0 reputation · 27 May 2025, 17:56 UTC
On a multi-user Linux host, I want to run Emacs with server-mode enabled so that emacsclient works for my account, while guaranteeing that no other local user can connect to my editing session.
As far as I can tell, the server creates a Unix domain socket under /tmp/emacs<UID>/server, and Emacs itself provides no built-in authentication or access control list for it. Access control appears to depend entirely on filesystem permissions of the socket directory. Enabling server-use-tcp would make things worse, since TCP mode has no encryption or authentication either.
I am unsure whether the default permissions on the socket directory are already restrictive on modern Emacs versions, or whether they vary by platform and umask. I also do not know if there is a supported way to relocate the socket directory (for example into a private $XDG_RUNTIME_DIR path) without breaking emacsclient discovery.
server-socket-dir to a private directory the recommended approach, and does emacsclient honor the same variable automatically?chmod 700 on the socket directory for desktop integrations or systemd user services?31775 reputation · 28 May 2025, 03:13 UTC
1. Default directory permissions
/tmp/emacs<UID> with mode 0700 and the socket file inside it with the same restrictive mode. This is hard‑coded in server.el and not affected by umask.
2. Moving the socket directory
server-socket-dir is fully supported. Setting it to a path such as $XDG_RUNTIME_DIR/emacs is the recommended way to keep the socket out of /tmp.
emacsclient does not read your Emacs init file. To make the client connect to the new socket you must either:
--socket-name on the command line, e.g. emacsclient --socket-name=$XDG_RUNTIME_DIR/emacs/server.
EMACS_SOCKET_NAME with the same value before invoking emacsclient.
server-socket-dir, restart the Emacs server (or restart the user session) so the new directory is created.
3. Side effects of chmod 700
chmod 700 is redundant.
emacs --daemon.
ls -ld /tmp/emacs$(id -u) /tmp/emacs$(id -u)/server should show drwx------ and srwx------.
sudo -u otheruser emacsclient --socket-name=/tmp/emacs$(id -u)/server -e "(message \"test\")" → should fail with Permission denied.
emacs --eval "(setq server-socket-dir \"$XDG_RUNTIME_DIR/emacs\")" --daemon.
EMACS_SOCKET_NAME=$XDG_RUNTIME_DIR/emacs/server emacsclient -e "(message \"ok\")" → should succeed.
These behaviors are consistent across recent Emacs releases, but if you are using a very old version (pre‑27) the directory may have been created with less restrictive defaults. If you encounter unexpected permission errors, check emacs --version and consult the NEWS entry for your release. Also ensure that /tmp/emacs<UID> does not already exist and is owned by another user, as Emacs will refuse to reuse it.
With the default 0700 permissions and the optional relocation to $XDG_RUNTIME_DIR, your Emacs server socket is effectively single‑user on a shared host. No additional hardening is required unless you enable server-use-tcp, which exposes a network port and should be avoided for local‑only access.
Could you confirm the exact Emacs version you are running? That will help verify the permission behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.