Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use a Shell script to terminate a user session in Linux

Shulou Source: shulou.com Published: 2022-06-01 07:31:41 09月13日 Update

How to use Shell script to terminate a user's session in Linux, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Terminate all user sessions

The following shell script terminates all active users' sessions by getting the tty value of the user's session and terminating it with the pkill command:

[root@localhost ~] # vim kill-user-sessions.sh #! / bin/bash usession=$ (w | awk'{if (NReneway1) {print $2}}'| tail-n + 2) for i in $usession do pkill-9-t $I done

Add executable permissions to the script:

[root@localhost ~] # chmod + x kill-user-sessions.sh

The above script uses the w command to list the users logged in to the system, uses the awk command to list lines with line numbers not equal to 1, and prints the second column, which is the line of the terminal name. Use the tail command to list lines 2 through the last line.

How do I terminate a session for a specified user?

In the following example, running the script followed by a user name terminates the user's login.

[root@localhost ~] # cat kill-specific-user-sessions.sh #! / bin/bash usession=$ (w $1 | awk'{if (NRFT) 1) {print $2}}'| tail-n + 2) for i in $usession do pkill-9-t $I done

Run the script test: here is a list of logged-in users who want to terminate the user01 session.

Then run the script and add the user name to the following parameters:

[root@localhost ~] #. / kill-specific-user-sessions.sh user01

Then use the w command to see that the user01 session has been terminated.

This is the answer to the question about how to use the Shell script to terminate a user's session in Linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: User script command question login run content more user name help answer easy easy one line that is parameter name instance boy partner Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Shulou Technology Microsoft Linux