Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep SSHD open when out of memory

I'm on EC2 t2.small instance (2GB RAM) and keep getting locked outside the server without the ability to ssh into it.
error message is ssh_exchange_identification: read: Connection reset by peer
Knowing the application, I assume it leaks memory and that SSHD is blcoked due to that.

Is there a way to allow SSH connection to a linux box that has its RAM full?
Can I someone have SSHD reserve enough memory for new connections?

like image 232
Tal Avatar asked Feb 24 '15 14:02

Tal


People also ask

How do I set the Ssh Keep Alive option?

To set the SSH keep alive option on the server: 1 Log in as root 2 Edit the file at /etc/ssh/sshd_config 3 Add this line to the file: ClientAliveInterval 60 4 Save the file 5 Restart sshd on the server More ...

How do I keep SSH sessions running in the background?

Using nohup command to Keep Running SSH Sessions If you are not that familiar with screen or tmux, you can use nohup and send your long running command to background so that you can continue while the command will keep on executing in background. After that you can safely log out.

How do I prevent ssh from timing out?

Here’s how to temporarily prevent SSH from timing out. Usually what happens is that your connection to the server is reset when you’ve been idle for a while, typically producing the error: Connection reset by peer. To circumvent this, you need to set a Keep Alive option on either the client or the server. Option 1) Server Side Keep Alive

Why does my SSH session timeout?

If you spend a lot of time at the command line you may have run into an annoying issue where your session times out after a relatively brief period of inactivity. While this is desirable from a security perspective, it can cause problems when you’re trying to perform a long running operation. Here’s how to temporarily prevent SSH from timing out.


2 Answers

When server runs into out-of-memory, it usually kills several applications which is control by OOM(out of memory) Killer. In your case when your server goes into out-of-memory it kills SSH process to free ram. We can avoid this by disabling OOM killer for ssh Process :

Disabling OOM killer for any process :

  echo -17 > /proc/`pidof Process`/oom_adj

Disabling OOM Killer for ssh all process:

 pgrep -f "/usr/sbin/sshd" | while read PID; do echo -17 > /proc/$PID/oom_adj; done

To automate this we need to set crontab for 1 min

   */1 * * * * root pgrep -f "/usr/sbin/sshd" | while read PID; do echo -17 > /proc/$PID/oom_adj; done

You can read more about the Linux OOM killer here.

like image 67
Rupesh Avatar answered Oct 24 '22 09:10

Rupesh


While you can't pre-allocate memory for sshd, you can set the memory limit for the application in such a way that it can't eat all the memory. See man ulimit how to do that.

If it's a Java application, try -Xmx to set the maximum amount of memory which it can allocate.

like image 39
Aaron Digulla Avatar answered Oct 24 '22 11:10

Aaron Digulla