I have a local unix socket tunneled to another unix socket on a remote instance over SSH:

ssh -N -L $HOME/my.sock:/var/run/another.sock

however, when I terminate ssh gracefully (i.e. ctrl+C or SIGTERM), the $HOME/my.sock remains. It looks like this is not cleaned up properly. Is there an option/flag for this?

This is problematic because if I run the command for the second time, it fails due to existing socket file. (I can't see a "reuse" flag/option either that’ll overwrite the existing socket file.) And I much rather don’t add a rm -f $HOME/my.sock.


Short answer, you can control this with a command line flag: -o 'StreamLocalBindUnlink=yes'

Long answer: See ssh_config(5):

         Specifies whether to remove an existing Unix-domain socket file for local or
         remote port forwarding before creating a new one.  If the socket file already
         exists and StreamLocalBindUnlink is not enabled, ssh will be unable to forward
         the port to the Unix-domain socket file.  This option is only used for port for‐
         warding to a Unix-domain socket file.

         The argument must be yes or no (the default).
  • 1
    Not had a chance to try it, but you might need to check whether the remote end needs the same configuration (but in sshd_config). – Will Crawford Jan 2 '18 at 21:21
  • No, the remote doesn't need the same config. This is good enough, although it doesn’t clean up the socket upon terminating ssh client, which would be really nice too. – Ahmet Alp Balkan Jan 3 '18 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.