You are not logged in.

#1 2016-07-01 19:17:16

frangio
Member
Registered: 2015-11-20
Posts: 7

Directory for SSH control sockets in $XDG_RUNTIME_DIR

Hello everyone!

I'd like to keep my SSH control sockets in a subdirectory under $XDG_RUNTIME_DIR. However, the subdirectory must be created on every boot in order to be used because of the $XDG_RUNTIME_DIR semantics.

I've been trying to find a way to create this directory automatically. tmpfiles.d seems like it could work but I don't want to hardcode my UID in there and I don't see any other way. I'd thought a systemd service file with a RuntimeDirectory option would work but found out it needs an Exec(Start|Stop) setting.

Does anybody have any ideas or suggestions as to how this could be done? And I'd also like to hear opinions on whether it makes sense to want to keep my control sockets in this directory. I usually see something like ~/.ssh/control recommended for this purpose but it doesn't make sense to me since a network socket shouldn't be persisted through boots.

Why doesn't something like a user-specific ~/.local/lib/tmpfiles.d exist?

On the other hand one can't use environment variables in ssh_config files, so something like "/run/user/<uid>/" has to be hardcoded in place of $XDG_RUNTIME_DIR. I don't like this.

Thanks!

Last edited by frangio (2016-07-09 03:16:54)

Offline

#2 2025-03-02 18:00:37

drfire
Member
Registered: 2025-03-02
Posts: 1

Re: Directory for SSH control sockets in $XDG_RUNTIME_DIR

This is one of the first results that popped up while searching on the Internet, but it doesn't have an answer. Apologies if necro-ing a very old thread is against the rules or if this has already been answered before.

Manpages referenced:

I'm not sure if this was the case back in 2016, but nowadays there's a switch in systemd-tmpfiles for creating user-specific temporary files: --user. There's also a number of systemd services that you might want to look at enabling, like  Moreover, the documentation of tmpfiles.d has some other useful information:

  • You can place user-specific configs in: ~/.config/user-tmpfiles.d/*.conf

  • You can use specifiers to control the path, and there's a specifier for user runtime directories.

On top of that, the documentation for ssh_config does explain which fields support environment variables as well as how to incorporate them.

So, putting that all together...

# ~/.config/user-tmpfiles.d/ssh-control-sockets.conf
#Type Path                Mode    User    Group    Age    Argument
d     %t/ssh              0700    -       -        -      -
d     %t/ssh/control      0700    -       -        -      -
#     ^ %t specifier is user runtime dir
# ~/.ssh/config.d/30control.conf
Host *
	#ControlMaster auto # <-- should be present somewhere to even enable this
	ControlPath ${XDG_RUNTIME_DIR}/ssh/control/%C
	ControlPersist 30m

Then I enable and start the user systemd-tmpfiles-setup.service with systemctl --user enable --now systemd-tmpfiles-setup.service as well as the systemd-tmpfiles-clean.timer (not that it's necessary for this example, as there's no cleanup time specified for this).

NOTE: I haven't tested this fully, especially what happens around reboot/first log-in. But hopefully this is a better starting point for anyone finding this while searching. Of course, the simpler approach would be to use a directory under ~/.ssh/ for this like most places suggest. But, you know... this is more fun?

Offline

Board footer

Powered by FluxBB