9

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.

ahmet alp balkan
  • 601
  • 1
  • 8
  • 21

1 Answers1

11

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

Long answer: See ssh_config(5):

 StreamLocalBindUnlink
         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).
aude
  • 272
  • 3
  • 7
Will Crawford
  • 380
  • 2
  • 6
  • 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 02 '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 03 '18 at 19:53
  • In my case too, the server `sshd_config` does need the same config set. – huyz Jan 15 '23 at 15:35
  • 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 Jul 12 '23 at 08:10