Append to your {.bashrc, .zshrc, etc.}:

# Only tries to create / attach to a tmux session on SSH connection.
# Useful guard to have so you can include this in your local .rc file
# and scp it over anytime you need to setup a new remote machine.
if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then
      # Attaches to the session "main", or creates it if it does not exist yet.
      tmux attach-session -t main || tmux new-session -s main
fi