How to attach to tmux session over SSH

Attach to the tmux session over SSH to continue your work.

You will receive an error the first time you try to reattach to existing tmux session.

This error is easily identifiable and the solution will apply to other screen-based applications
$ ssh 192.0.2.125 tmux attach
open terminal failed: not a terminal

The solution is to simply force pseudo-terminal allocation.

$ ssh -t 192.0.2.125 tmux attach
[...]

Define RequestTTY in OpenSSH SSH client configuration file to make this permanent.

$ cat ~/.ssh/config 
Host 192.0.2.125
  RequestTTY yes

A will give you a tip, use -d parameter to detach any other clients attached to the session. It will ensure that tmux session window will use all the available space.

$ ssh -t 192.0.2.125 tmux attach -d
[...]

Single or multiple jump hops are not an issue as you need to request a TTY on the target host only.

$ ssh -A -J [email protected] 192.0.2.125 tmux ls
0: 1 windows (created Sun Nov  3 22:54:40 2019) [179x63] (attached)                                                                                                                
Connection to 192.0.2.125 closed
$ ssh -A -J [email protected] 192.0.2.125 tmux attach -t 0 -d
[...]

Additional notes

Excerpt from the manual page.

RequestTTY

Specifies whether to request a pseudo-tty for the session. The argument may be one of: no (never request a TTY), yes (always request a TTY when standard input is a TTY), force (always request a TTY) or auto (request a TTY when opening a login session). This option mirrors the -t and -T flags for ssh(1).

ssh_config manual page.