I have limited no. of tcp connections through iptables to limit total number of ssh connections. Now when i am adding a rule with maximum limit of tcp connections lesser than currently opened ssh sessions, all the current ssh sessions got hang. I wasn't able to write anything. Please tell me how do I overcome this.

  • Are you sure you want to limit the number of concurrent connections via iptables and not via sshd's dedicated setting for that (MaxSessions)? You could even use pam_limits to limit the number of connections per user, with different limits for different users.
    – n.st
    Commented Jul 1, 2015 at 22:36
  • MaxSessions can be used only to limit maximum number of multiplexed sessions per tcp connection. So it cannot be used in this case and neither pam_limit can be used since it is used to limit no. of connections per user as you said. I want to limit total number of ssh connections. Btw thanks for replying. Commented Jul 2, 2015 at 4:53

1 Answer 1


I would identify which ssh process was yours, assuming you are doing this remotely that is, then kill all the other sessions.

Perhaps you could send a broadcast to allow other users to gracefully log out:

shutdown -k 5 &

Then kill any other ssh sessions that you need to kill, and make your firewall changes.

  • Actually what I want is that all the opened sessions remain open and no other sessions can be created in this case. You know any way how to do that. Btw thanks for answering. Much appreciated. Commented Jul 2, 2015 at 4:55
  • So using a fake shutdown command like the one above that uses -k actually prevents anyone from logging in via ssh until the time that you set is complete, in this case it is 5 minutes. When the time is complete the command exits with a 'Shutdown Canceled' message printed to stdout in the session where the job has been backgrounded. I think this command might work for you.
    – 111---
    Commented Jul 2, 2015 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .