Add an entry to ~/.ssh/config similar to this one:
RemoteCommand screen -RD
and then just call
$ ssh screen_on_server
and you'll get connected to an existing screen session if present, otherwise you'll a new new one.
Should work with #tmux, too, maybe different options.
@xtaran This configures it from the client, but if you want to do this from server side you could also add "screen -RD" to your ~/.bashrc or ~/.bash_profile (or whatever startup script your preferred shell uses). That way it doesn't matter where you connect in from, you can keep the one persistent session.
To autostart screen, put this in .bashrc:
autostart screen on first logon
if [ $(tty) == "/dev/pts/0" ]; then echo "Autostarting screen" screen -DR fi
(or whatever tty1 you use, check with echo $(tty))
@xtaran @climagic my ~/.bashrc contains
# reattach tmux on SSH login
if [[ -n "$SSH_CONNECTION" ]] && [[ -z "$TMUX" ]] ;then
ID="$( tmux ls | cut -d: -f1 )" # get the id of first session
if [[ -z "$ID" ]] ;then # if not available create a new one
tmux new-session; exit
tmux attach-session -t "$ID"; exit # if available attach to it
could be shorter if I hardcode the ID
chaos.social – a Fediverse instance for & by the Chaos community