- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
LinuxOpen SourceOperating System
';
Introduction
Secure Shell (SSH) is a commonly used protocol for secure remote access to Linux servers. When multiple users are connected to a Linux server via SSH, it can be useful to list all connected SSH sessions for administrative or monitoring purposes. In this article, we will discuss how to list all connected SSH sessions on Linux using various command line tools.
List of SSH sessions connected with the who command
The who command is a simple and widely available command-line tool for listing logged in users on a Linux system. To list all connected SSH sessions, you can use the “who -a” command. The -a option shows all users, including those who are not logged in through the system console. The result will show your username, terminal, and login date and time.
$ who -aroot pts/0 2020-12-19 14:20 (10.0.0.1)user1 pts/1 2020-12-19 15:25 (10.0.0.2)user2 pts/2 2020-12-19 14:30 (10.0.0.3)
In this example, the output shows that there are three users currently connected: "root", "user1" and "user2", along with the endpoint they connected to and the IP addresses they connected from.
List of SSH sessions connected with the w command
Another command-line tool that can be used to list connected SSH sessions is “w”. This command displays information about users currently logged on to the system, and also displays the process each user is running. To list all connected SSH sessions, you can use the “w -h” command, which omits the header and shows only the process.
$ w -hroot pts/0 14:20 2.00s 0.00s ssh 10.0.0.1user1 pts/1 14:25 1.00s 0.00s ssh 10.0.0.2user2 pts/2 14:30 1.00s 0.00s ssh 10.0.0.3
In this example, the output shows that the three users are currently connected, the terminal they are connected to, and the IP addresses they are connecting from.
List of SSH sessions connected with the last command
The last command is used to display users who have recently logged on to the system. This command can also be used to list connected SSH sessions using the latest “-i” command.
$ last -iroot pts/0 10.0.0.1 Sun Dec 19 14:20 - 14:25 (00:05)user1 pts/1 10.0.0.2 Sun Dec 19 14:25 - 14:30 (00:05)user2 pts/2 10.0.0.3 Sun Dec 19 14:30 - 14:35 (00:05)
In this example, the output shows the three users currently connected, the endpoint they connected to, the IP addresses they connected from, and the length of their session.
List of SSH sessions connected with the who -u command
You can also use the “who -u” command to list all connected SSH sessions. This command displays the user, terminal, and login date and time.
$ who -uroot pts/0 2020-12-19 14:20 (10.0.0.1)user1 pts/1 2020-12-19 14:25 (10.0.0.2)user2 pts/2 2020-12-19 14:30 (10.0.0.3)
The “-u” option shows users and their idle time, but does not show IP addresses. So, if IP information is important to you, you can combine this command with the “-i” option which will show the IP addresses.
$ who -u -iroot pts/0 2020-12-19 14:20 (10.0.0.1) . . . . . . . . . . . . . . . user1 pts/1 2020-12-19 14:25 (10.0.0.2) . . . . . . . . . . . . . . . user2 pts/2 2020-12-19 14:30 (10.0.0.3) . . . . . . . . . . . . . . .
List of SSH sessions connected with the ss command
Another command that can be used to list connected SSH sessions is ss. This command is similar to netstat but more efficient and is used to dump socket statistics. To list all connected SSH sessions, you can use the “ss -t -a” command.
$ ss -t -a | grep sshtcp ESTAB 0 0 10.0.0.1:ssh 10.0.0.1:46754 users:(("sshd",pid=9987,fd=3))tcp ESTAB 0 0 10.0.0.2:ssh 10.0.0.2:47754 users:(("sshd",pid=9987,fd=3))tcp ESTAB 0 0 10.0.0.3:ssh 10.0.0.3:48754 users:(("sshd",pid=9987,fd=3))
The “-t” option shows only TCP connections and the “-a” option shows all sockets. The grep command is used to filter the output and shows only ssh connections.
Conclusion
In this article, we have discussed several command-line tools for listing connected SSH sessions on Linux. Each command has its own specific options and output format, so it's up to you to choose the one that best suits your needs. It is always recommended to consult the man pages of each command before using it, to ensure that you are using the correct options and getting the expected result.
Pradeep Jhuriya
Updated on: 20-Jan-2023
31K+ Views
- Related Articles
- How to list running screen sessions on Linux?
- How to Record Linux Terminal Sessions?
- How to Find All Failed SSH login Attempts in Linux?
- How to remove connected remote desktop user sessions using PowerShell?
- Keeping SSH session alive on Linux
- Installing Java on Linux using SSH
- How to resume a partially transferred file over ssh on Linux?
- How to Enable SSH on Raspberry Pi {Linux, Mac OS, Windows}?
- How to list down all the available commands and aliases on Linux?
- How to Change SSH Port in Linux?
- How To List All Group in Linux ?
- How to Increase SSH Connection Timeout in Linux
- How to Disable SSH Root Login in Linux?
- How to Brute-Force SSH in Kali Linux?
- How to list all users in a Linux group?
Kickstart Your Career
Get certified by completing the course
Get Started
Advertisem*nts
';