Auto Attach to tmux session on SSH

To start tmux and attach to existing connection on SSH in, edit file

vi ~/.bashrc

Add

if [[ "$TMUX" == "" ]] && [[ "$SSH_CONNECTION" != "" ]] && [ "$SSH_TTY" != "" ]; then
    WHOAMI=$(whoami)
    if tmux has-session -t $WHOAMI 2>/dev/null; then
        tmux -2 attach-session -t $WHOAMI
    else
        tmux -2 new-session -s $WHOAMI
    fi
fi

Method 2

You can use following command when connecting to remote sevrer.

ssh  USER@SERVER_IP -t tmux a

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *