Home  >  Article  >  System Tutorial  >  How to log out other SSH users in Linux

How to log out other SSH users in Linux

WBOY
WBOYforward
2024-02-09 15:03:221299browse

Today we will briefly introduce how to kick out other users who connect to the system through ssh.

在Linux中如何注销其他 SSH 用户

Pixelated word Linux made from cubes, mosaic pattern

If you log in to a remote Linux system through ssh, just use the exit command to exit ssh. So what if you want to log out other users who are logged into the system via ssh?

Logout user in ssh session

First, check the list of logged-in users in the Linux system. There are several ways to do this, here we use the who command, along with the -u option, which will display the process ID of the shell session logged into the system:

who -u

The output is as follows:

root@localhost:~# who -u
abhishek pts/0        2021-04-05 09:25 00:01       31970 (223.180.180.107)
prakash  pts/1        2021-04-05 09:26   .         32004 (223.180.180.107)
root     pts/2        2021-04-05 09:26   .         32039 (223.180.180.107)

Next, assume that our task is to kick user prakash from the ssh session, and the process ID of his shell session is 32004. The user will be disconnected when completed.

To do this, you can use the kill command to send a SIGHUP signal, which is used to report that the user's terminal has been disconnected, and can also effectively disconnect all processes in the session from the controlling terminal.

sudo kill -HUP 32004

Of course, to perform this operation, you need to be the root user or have sudo permissions.

在Linux中如何注销其他 SSH 用户

As a relatively friendly habit, it is best to send a message to a user before kicking him. To send information, you can use the write command, as shown below:

echo "Your session will end in 2 minutes. Save your work!" | write prakash pts/2

So, what if sending the SIGNHUP signal doesn't work? You can also force terminate the ssh session.

Force terminate ssh session

If the SIGHUP signal does not work, the SIGKILL signal can be sent.

sudo kill -9 32004

If you have multiple ssh sessions, you can log out the user from the selected session

If a unified user logs in from multiple systems or terminals, the above operation will only affect the session we want to terminate, it will not kick out all the user's sessions.

For example, when the ssh session hangs up due to some reasons (such as network disconnection), we can terminate the terminal and open another ssh session from a new terminal. In this case, we can see that we are logged in to system twice.

At this time, we need to close the session that is no longer active.

In the output of the who command, we can determine which user to kick based on the login time. Of course, you may be kicked out due to wrong judgment, but it doesn't matter, you can log in again.

在Linux中如何注销其他 SSH 用户

The above is the detailed content of How to log out other SSH users in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete