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.

1 Answer 1


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). Commented Jan 2, 2018 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. Commented Jan 3, 2018 at 19:53
  • 1
    In my case too, the server sshd_config does need the same config set.
    – huyz
    Commented Jan 15, 2023 at 15:35
  • 2
    I think I figured it out. If you do remote forwarding (-R), you need it on the server, while the ssh_config setting affects local forwarding.
    – andsens
    Commented Jul 12, 2023 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .