screen when logging in on the remote host (executed on your local machine):
ssh -t user@host screen
In the screen session, on the remote host, to detach and log out (which would end the SSH session unless you are using a persistent connection using a control socket (i.e. using the
ControlMaster setting with
Later, to log in and re-attach:
ssh -t user@host screen -r
Note that you have to use
ssh, forcing pseudo-terminal allocation, to start
screen in this way.
The alias on the remote host would be for
screen -d, maybe
detach='screen -d' or something similar.
On the local host, you could have a section in your
~/.ssh/config file saying
RemoteCommand screen -d -R
Host myhost myhost-screen
Then, whenever you use
screen session would be created (or re-attached to) on the specified host, while
would not use
RemoteCommand was added in OpenSSH 7.6 (2017-10-03).
tmux new-session -A -s "%n" in
~/.ssh/config instead of
screen -d -R.
- Use prefixd or
tmux detach-client or
tmux detach to detach.