SSH and Limits
I was tasked to look at why a limit wasn’t being applied to a shell when using ssh. To let you understand lets have a look at what was being seen:
[user@host]$ ssh test -l user user@test's password: [user@test]$ ulimit -n 1024 [user@test]$ cat /etc/security/limits.conf * soft nofile 4096 * hard nofile 4096
However switching to the same user you will see:
[user@test]$ su - user Password: [user@test]$ ulimit -n 4096
That the limit isn’t respect when logging in via SSH but when we switch user it is applied.
The reason for this is rather simple; the SSH is opening a shell that isn’t a login shell and therefore limits is not being applied. To correct this simply edit your sshd_config file and set it to use login shells.
[root@test]$ grep -i uselogin /etc/ssh/sshd_config #UseLogin no
You can see that the entry is set by default to no; so simply edit the line and alter it to yes:
[root@test]$ sed -i.bak 's/#UseLogin no/UseLogin yes/' /etc/ssh/sshd_config [root@test]$ grep -i uselogin /etc/ssh/sshd_config UseLogin yes
Now reload your sshd.
[root@test]$ /etc/init.d/sshd reload /etc/init.d/sshd reload Reloading sshd: [ OK ]
And test again:
[user@host]$ ssh test -l user user@test's password: [user@test]$ ulimit -n 4096
That’s it! Sometimes odd behaviour like this can be difficult to track down without having a good read of configuration files.