The TMUX(1) method is that the client process ( tmux ) connects to the server process ( tmux also, but not tied to TTY), as shown below: ps output:
PID TTY STAT TIME COMMAND 19229 pts/1 S+ 0:00 tmux 19231 ? Ss 0:00 tmux
This shows that the client actually starts in front of the server (we can assume that it deploys it).
After disconnecting / reconnecting, the ps command outputs:
PID TTY STAT TIME COMMAND 19231 ? Ss 0:00 tmux 19290 pts/1 S+ 0:00 tmux attach
This shows the tmux client as tmux attach , so it is a little easier to understand.
Now, if we look at the output of pstree in both cases, we get in both cases (ignoring the pid change for tmux attach ):
pstree -p init(1)โโฌโacpid(1824) โโcron(1859) โฎ โโsh(14146)โโโtmux(19229) โโtmux(19231)โโโsh(19233)โโโpstree(19234)
It is clear that the commands typed ( pstree in this case) in the client process (PID 19229 ) are executed by one server (PID 19231 ), which allows them to continue without SIGHUP if the client terminal is lost (for example, via ssh).
Now, to the question, O.P. asked: what happens when tmux returns failed to connect to server: Connection refused , is that the server process (pid 19231 in our case) is unavailable, regardless of the reason (maybe because that the server process is dead, and also because the user running the tmux client does not have access rights to the tmux socket, etc.)
The solution in this case is grep for tmux processes (for example, via ps ) and I pray that you do not get this error because the server is dead (so you can attach to it using lsof to find out which sockets it is listening to). Otherwise, it is impossible to connect to the server, because it is dead, as after a reboot.
TL; DR:
This error can be set for several reasons: from an error to a critical failure (the program died). In short, use the UNIX tools at your disposal to determine which socket tmux uses if it is still working (there must be at least two processes if your tmux client is running - this happens after calling tmux or tmux attach from the shell), and thus, if you have lost the session or not.
Note. As pointed out in other answers, if the cause of this error is a socket error, you can use the -L flag to tell tmux use a specific socket.