This is a common problem that can be caused by several factors, e.g. SSH config, network instability, client power-saving settings. Possible solutions are:
- Add the following settings to ~/.ssh/config on the client that keeps losing the connection:
TCPKeepAlive=no ServerAliveInterval=120 ForwardX11Timeout 596h
- Use a tool such as screen or tmux which allows you to detach from and re-attach to a terminal session on the target server.
- (MacOS only) Start your ssh session using caffeinate which will prevent your Mac from sleeping during the session, e.g: caffeinate -i /usr/bin/ssh
- (MacOS only) Check the energy saver settings and set:
- On: Prevent computer from sleeping automatically when the display is off
- Off: Put harddisks to sleep when possible