You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

connection closed or logout problem from ssh network connection

Hello,

I am using mac M2 (macOS Monterey) version from last couple of months. I need to use ssh network for my work, where I am facing the issue of broken connection or logout problem for being inactive for 5 minutes. After searching in the web, I tried to fix the problem by adding the following lines in my .ssh/config file:


ServerAliveInterval 3000
ServerAliveCountMax 1200
ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
ControlPersist 1000h


But, nothing helped. I have seen that the issue got solved for M1 using the above-mentioned method. Could anybody help me in this regard?


Thanks!


MacBook Air, macOS 12.5

Posted on Jan 4, 2023 3:43 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 4, 2023 1:23 PM

Unless you have an explicit need for a ControlMaster, remove the entries

  • ControlMaster
  • ControlPath
  • ControlPersist

They are fun, but they have some downside issues of their own. And if you are not hooked on the service they provide, just remove them as they are not important to maintaining your ssh connections.


Change ServerAliveInterval to 240 and change ServerAliveCountMax to 2 (you can use a larger value, but 2 should really be sufficient.

  • ServerAliveInterval 240
  • ServerAliveCountMax 2


The value of 3,000 for your ServerAliveInterval interval is too long (50 minutes). You need something shorter, so 240 seconds (4 minutes is a better choice, or make it shorter still if you like; I would not go below 60 seconds).


Second, DO NOT let your Mac go to sleep, as that will break any ssh connections you have active.


A Mac laptop will go to sleep as soon as the screen is turned off when you are not connected to power. So you can use System Preferences -> Battery -> Battery -> Turn Display Off After, and move the slider to the right so your screen stays on longer.


If you are plugged into power, then you can use System Preferences -> Battery (or it might be Energy Saver if on an iMac or Mac mini) -> Power -> [X] Prevent your Mac from automatically sleeping when the display is off


You can also look into utilities such as "Amphetamine" via the App Store that can be configured to keep your Mac from going to sleep.

NOTE: At work we use ssh extensively and the 240 and 2 values work great, plus making sure the Macs do not go to sleep while connected.


NOTE 2: At work I (and a lot of others) use tmux on our remote systems so that when our ssh connection is broken, the remote session is still alive. If you are more comfortable using the older but still capable GNU screen, that is also available for various Unix/Linux based operating system. In fact many Unix/Linux distributions actually include GNU screen. Only a few distributions include tmux, but I find it invaluable to keep my remote sessions up and running even when I have to disconnect (because of VPN into work shutting down once every 24 hours, or sleeping my Mac at the end of the day, or weather related network/power interruptions at home, etc...). tmux has been a life saver. Lots of YouTube videos on tmux if you are not familiar with it. tmux stands for Terminal Multiplexer.

4 replies
Question marked as Top-ranking reply

Jan 4, 2023 1:23 PM in response to Nihar_saha

Unless you have an explicit need for a ControlMaster, remove the entries

  • ControlMaster
  • ControlPath
  • ControlPersist

They are fun, but they have some downside issues of their own. And if you are not hooked on the service they provide, just remove them as they are not important to maintaining your ssh connections.


Change ServerAliveInterval to 240 and change ServerAliveCountMax to 2 (you can use a larger value, but 2 should really be sufficient.

  • ServerAliveInterval 240
  • ServerAliveCountMax 2


The value of 3,000 for your ServerAliveInterval interval is too long (50 minutes). You need something shorter, so 240 seconds (4 minutes is a better choice, or make it shorter still if you like; I would not go below 60 seconds).


Second, DO NOT let your Mac go to sleep, as that will break any ssh connections you have active.


A Mac laptop will go to sleep as soon as the screen is turned off when you are not connected to power. So you can use System Preferences -> Battery -> Battery -> Turn Display Off After, and move the slider to the right so your screen stays on longer.


If you are plugged into power, then you can use System Preferences -> Battery (or it might be Energy Saver if on an iMac or Mac mini) -> Power -> [X] Prevent your Mac from automatically sleeping when the display is off


You can also look into utilities such as "Amphetamine" via the App Store that can be configured to keep your Mac from going to sleep.

NOTE: At work we use ssh extensively and the 240 and 2 values work great, plus making sure the Macs do not go to sleep while connected.


NOTE 2: At work I (and a lot of others) use tmux on our remote systems so that when our ssh connection is broken, the remote session is still alive. If you are more comfortable using the older but still capable GNU screen, that is also available for various Unix/Linux based operating system. In fact many Unix/Linux distributions actually include GNU screen. Only a few distributions include tmux, but I find it invaluable to keep my remote sessions up and running even when I have to disconnect (because of VPN into work shutting down once every 24 hours, or sleeping my Mac at the end of the day, or weather related network/power interruptions at home, etc...). tmux has been a life saver. Lots of YouTube videos on tmux if you are not familiar with it. tmux stands for Terminal Multiplexer.

Jan 4, 2023 4:20 AM in response to Nihar_saha

Nihar_saha wrote:

I have seen that the issue got solved for M1 using the above-mentioned method.

Where did you see that? It is an extraordinarily bad idea to copy things from the internet if you don't understand what they do. Did you make any other changes? What did those do?


Usually this setting is defined by the server. You may be able to work around it with the ServerAliveInterval setting. However, if you are being disconnected after 5 minutes, then you should probably set this to something less than 300. Currently, you are sending a null packet after 50 minutes, which isn't helping.

connection closed or logout problem from ssh network connection

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.