Typically, if you are logged into another system via SSH, you can easily logout by issuing exit command on your terminal. But many times system administrators may need to logout other users from SSH connection. This may be because they are having unauthorized access or you want to disconnect users for maintenance. In this article, we will learn how to disconnect user from SSH connection.
How to Disconnect User from SSH Connection
Here are the steps to disconnect user from SSH connection.
1. List All Logged In Users
First you need to get a list of all users that have logged into your system. You can do so with the following command.
$ who -u root@localhost:~# who -u ubuntu pts/0 2022-04-05 09:25 00:01 31970 (188.8.131.52) test_user pts/1 2022-04-05 09:26 . 32004 (184.108.40.206) root pts/2 2022-04-05 09:26 . 32039 (220.127.116.11)
The above output shows that there are 3 users logged into your SSH system. Each user’s session is displayed as a separate process with PID. If you want to logout a user, say, test_user, you need to simply kill the process associated with that user. In this case, we will terminate process with PID 32004. When you kill the login session of user, they will be logged out.
2. Logout User
You can use kill command to terminate the login session of user.
$ sudo kill -HUP 32004
Please note, you need to have root or sudo permissions to be able to kill a process. Also, if the above SIGHUP command does not kill the user session, use the SIGKILL signal.
$ sudo kill -9 32004
If the logged in user is genuine and has required permissions, then it is advisable to send a message before logging them out. You can do so using echo and write command. Write command can be used to send messages to other users.
$ echo "Your session will end in 2 minutes. Save your work!" | write test_user pts/2
Sometimes a user may have multiple SSH sessions going on at the same time. In such cases, you will find multiple entries for the same user when you run who -u command.
$ who -u root@localhost:~# who -u ubuntu pts/0 2022-04-05 09:25 00:01 31970 (18.104.22.168) test_user pts/1 2022-04-05 09:26 . 32004 (22.214.171.124) test_user pts/1 2022-04-05 09:36 . 32040 (126.96.36.199) test_user pts/1 2022-04-05 09:46 . 32050 (188.8.131.52) root pts/2 2022-04-05 09:26 . 32039 (184.108.40.206)
In such cases, you need to terminate all login processes associated with the user in order to completely log them out. In the above example, there are 3 sessions associated with test_user. Here is the command to terminate them all.
$ sudo kill -9 32004 32040 32050
In this article, we have learnt how to forcibly logout other SSH connections from your system.
How to Get HDD Temperature in Linux
How to Install New Fonts in Linux
How to List GPG Keys in Linux
How to Find Oldest File in Directory
How to Show Disk Usage for Top Level Directory