A friend asked me how to automatically start directly via 's configuration file. Here's how to do it:

Add an entry to ~/.ssh/config similar to this one:

Host screen_on_server
RequestTTY yes
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 , too, maybe different options.

Cc @climagic

Β· Β· Web Β· 4 Β· 5 Β· 7

@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.

@xtaran @climagic or do it on the destination server instead of the source, which has the advantage that it works from whereever you log in:

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 for autostarting/logging into tmux I use:

Put this in .profile

[ -z "$TMUX" ] && exec tmux new -DA -s shell

@xtaran @climagic P.S. This is in .profile of the server, not the originating computer.

@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

Sign in to participate in the conversation – a Fediverse instance for & by the Chaos community