2

In my ~/.ssh/config, I have the ControlMaster auto setting enabled. It works pretty well for me, but there is one behaviour that irritates me that I’d like to eliminate.

Let’s say I have two terminal emulator windows A and B. In window A, I invoke ssh to connect to a remote host, then I do the same in window B. Next, I type exit in window A to close the session. Although I have disconnected, the ssh process in window A does not exit to the local session because it has to wait for the process in window B to end its remote session as well: it has to keep the control socket running.

I would rather have it so that when I disconnect in window A, I am immediately returned to the original local session from which I invoked ssh, while the session in window B keeps running. You may ask: well, which process should manage the control socket then? Ideally, I’d like it to be a dedicated third process running under the supervision of the user systemd instance, that automatically disconnects when there are no more sessions running. But I’d also accept a more hacky approach with a lingering background process.

And I would rather avoid launching the control socket master manually. It should start automatically as soon as I open the first session.

Is this possible to do?

2
  • 2
    I honestly have never seen that behaviour, it just works out of the box. Maybe because I have ControlPersist set to a timeout value, so it knows from the start that it needs to fork? Mar 27, 2021 at 18:36
  • 1
    @UlrichSchwarz ControlPersist is exactly what I was looking for. Oct 27, 2021 at 10:39

1 Answer 1

1

A command such as

ssh -f -N remote_host

will create an ssh session to the remote host but not run a command (-N) and background itself (-f). This command could be used as the master.

If you want to tunnel things like X then this could also be done on this command

ssh -X -f -N remote_host

If you find the idle session disconnecting (e.g. because of NAT timeouts) then adding something like

-o ServerAliveInterval=30 

(or equivalent in your config file) will try to keep the session active.

1
  • Yes, but I don’t want to run a separate command for that. I want it to automatically start as soon as I open the first session. Mar 27, 2021 at 13:40

You must log in to answer this question.

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