Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report