When connecting to a server using SSH on macOS, it always automatically disconnects. I used to manage with screen before, but recently I found a solution:
Edit the ~/.ssh/config
file with the following content:
Host *
ServerAliveInterval 30
ServerAliveCountMax 3