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 Shell script to terminate a user's session in Linux

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to use Shell script to terminate user session in Linux". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to use Shell script to terminate user session in Linux" can help you solve the problem.

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 KILLLUserUser sessions. Sessions. For i in binder usessiondo pkill $(w | awk'{if (NRTV session 1) {print $2}}'| tail-n + 2) usessiondo pkill-9-t $idone

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 KILLICUR specific user sessions. Sessions. Idone for i in $usessiondopkill-9-t $idone

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 end of the introduction on "how to use Shell script to terminate a user's session in Linux". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report