brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ssh StreamLocalBindUnlink fixes a forwarded socket that is already there

Network and access
~/tips/tip-ssh-streamlocalbindunlink

ssh -o StreamLocalBindUnlink=yes -L /tmp/app.sock:/run/app.sock host

Lets ssh replace a leftover Unix socket file when forwarding to a socket again.

SSH can forward a Unix socket, not only a TCP port. It works nicely until the first connection dies and leaves its socket file behind. The next attempt then fails, and this option is the fix.

ssh -o StreamLocalBindUnlink=yes -L /tmp/app.sock:/run/app.sock host

The man page lists local_socket:remote_socket as a form of -L. The example creates a socket file in /tmp on your side and connects it to /run/app.sock on the server. Tools that speak to a Unix socket can then use the local file.

If that file already exists and StreamLocalBindUnlink is not enabled, ssh can’t forward the port to the socket file. With the option set to yes, ssh removes the existing file before it creates a new one. The default is no.

Remote forwarding to a socket works the same way, with -R remote_socket:local_socket. There the server does the binding, so the setting that counts is the StreamLocalBindUnlink option in sshd_config, which also defaults to no.

Host app
    StreamLocalBindUnlink yes

One caveat: the option only applies to forwarding to a Unix-domain socket file. For ordinary TCP port forwards it does nothing. Also, it deletes whatever is at that path, so keep the socket in a folder of its own so you never point it at a file you care about.

Further reading: ssh_config(5), sshd_config(5)

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link