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
Need help with Linux Server or WordPress? We can help!

Leave a Reply

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