Skip to main content

Limit The Number Of SSH Logins Per User/Group/System

particular user tries to SSH into the system more than the allowed SSH logins, he/she will be denied access.

Limit The Number Of SSH Logins Per User Or Group On Linux

The limits.conf file is usually located under /etc/security/ directory on RPM and DEB-based systems.

Go to your remote system and edit /etc/security/limits.conf file with sudo or root user:

$ sudo nano /etc/security/limits.conf

To limit the number of concurrent SSH sessions for a specific user, for example ostechnix, Add the following line at the end:

ostechnix	hard	maxlogins	1

set number of concurrent SSH logins for user or group in limits.conf file in linux

Here,

  • ostechnix – Name of the user in my CentOS 8 server.
  • maxlogins 1 – Maximum number of logins for this user. In my case it is 1. You can increase this value to allow maximum no. of active SSH sessions per user.
  • hard – Enforcing hard limits.

If you want to limit the number of SSH connections for a group of users, add this:

@mygroup	hard    maxlogins	1

Replace @mygroup with your group name. In this scenario, all users in the mygroup can’t login via SSH more than once.

If you want to limit SSH sessions of all users (global) in the system, add this line instead:

*	hard    maxlogins	1

Here, the asterisk means global (i.e. all users in the system). Once you defined the maximum number of SSH sessions per user/group or the whole system, save and close the file.

Now try to SSH to the system with the same user more than one time. You will see an error message like below.


Too many logins for 'ostechnix'.
Last login: Tue Feb 18 17:12:09 2020 from 192.168.225.37
Connection to 192.168.225.52 closed.

Limit The Number Of SSH Logins Per User On Linux System

As you see in the above output;

  1. First, I SSH into the remote CentOS 8 server as root user via SSH and connection was successfully established.
  2. Then, I opened a new tab and SSH into the same server as normal user “ostechnix” and the connection was successful.
  3. Finally, I tried to SSH into the same server with the same user(i.e. ostechnix) and this time the connection was denied. The user “ostechnix” can not SSH into the system more than once. Because his maximum number of allowed SSH session for this user is only one time. Hence, SSH connection is denied for this user.

In this example, we restricted the total number of active SSH sessions for a user or group or all users in the system. It is also possible to limit the SSH sessions per system-basis. For example – we can allow only one SSH login from a system, not per the user or group.

Limit total number of SSH session Per System

To limit the total number of active SSH connections per system, add the following line in /etc/security/limits.conf file:

*       hard    maxsyslogins    1

Here, maxsyslogins refers the maximum number of logins on the system, not per-user or per-group.

Try to SSH into the system and you can’t access the system more than the maximum number of allowed logins on the system.

Limit total number of SSH session Per System

See? The user “ostechnix” can’t even SSH for the first time. Because, the total number of allowed SSH login is only one for the system. This time the SSH connection made by the root user has also been taken into account. So the maximum number of SSH logins has crossed the defined limit and the user “ostechnix” was denied access. Hope you understand.

Please note that this restriction applies for the normal users only. The root user can still able to login via SSH any number of times.

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...