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 lock and unlock user account in Linux

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Linux in how to lock and unlock user accounts, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Method 1: how do I use the passwd command to lock, unlock, and check the status of a given user account in Linux?

The passwd command is one of the commands frequently used by Linux administrators. It is used to update the user's authentication token in the / etc/shadow file.

Use the-l switch to run the passwd command to lock out the given user account.

# passwd-l daygeek Locking password for user daygeek. Passwd: Success

You can check the locked account status through the passwd command or by getting a given user name from the / etc/shadow file.

Use the passwd command to check the user account lockout status.

# passwd-S daygeek or # passwd-- status daygeek daygeek LK 2019-05-30 7 90 7-1 (Password locked.)

This outputs short information about the password status of a given account.

LK: password is locked

NP: no password set

PS: password is set

Use the / etc/shadow file to check the status of locked user accounts. If the account is locked, the password will be preceded by two exclamation marks.

# grep daygeek / etc/shadow daygeek tGvVUhEY$PIkpI43HPaEoRrNJSRpM3H0YWOsqTqXCxtER6rak5PMaAoyQohrXNB0YoFCmAuh506n8XOvBBldvMy9trmIV00:18047:7:90:7:::

Use the-u switch to run the passwd command to unlock a given user account.

# passwd-u daygeek Unlocking password for user daygeek. Passwd: Success

Method 2: how do I use the usermod command to lock, unlock, and check the status of a given user account in Linux?

The usermod command is also frequently used by Linux administrators. The usermod command is used to modify / update account information for a given user. It is used to add users to specific groups, and so on.

Use the-L switch to run the usermod command to lock the given user account.

# usermod-lock daygeek or # usermod-L daygeek

You can check the locked account status through the passwd command or by getting a given user name from the / etc/shadow file.

Use the passwd command to check the user account lockout status.

# passwd-S daygeek or # passwd-- status daygeek daygeek LK 2019-05-30 7 90 7-1 (Password locked.)

This outputs short information about the password status of a given account.

LK: password is locked

NP: no password set

PS: password is set

Use the / etc/shadow file to check the status of locked user accounts. If the account is locked, the password will be preceded by two exclamation marks.

# grep daygeek / etc/shadow daygeek tGvVUhEY$PIkpI43HPaEoRrNJSRpM3H0YWOsqTqXCxtER6rak5PMaAoyQohrXNB0YoFCmAuh506n8XOvBBldvMy9trmIV00:18047:7:90:7:::

Use the-U switch to run the usermod command to unlock the given user account.

# usermod-unlock daygeek or # usermod-U daygeek

Method-3: how do I use the usermod command in Linux to disable and enable SSH access to a given user account?

The usermod command is also a command often used by Linux administrators. The usermod command is used to modify / update account information for a given user. It is used to add users to specific groups, and so on.

Instead, locking can be done by assigning nologin shell to a given user. To do this, you can run the following command.

# usermod-s / sbin/nologin daygeek

You can check the locked user account details by giving a user name from the / etc/passwd file.

# grep daygeek / etc/passwd daygeek:x:2240:2243::/home/daygeek:/sbin/nologin

We can enable ssh access for users by assigning back to the original shell.

# usermod-s / bin/bash daygeek

How do I use shell scripts to lock, unlock, and check the status of multiple user accounts in Linux?

If you want to lock / unlock multiple accounts, you need to find a script.

Yes, we can write a small shell script to do this. To do this, use the following shell script.

Create a list of users. Each user information is on a separate line.

$cat user-lists.txt u1u2u3u4u5

Use the following shell script to lock out multiple user accounts in Linux.

# user-lock.sh #! / bin/bash for user in `cat user- lists.txt` do passwd-l $user done

Set the user-lock.sh file to executable permissions.

# chmod + user-lock.sh

Finally, run the script to achieve the goal.

# sh user-lock.sh Locking password for user u1. Passwd: Success Locking password for user u2. Passwd: Success Locking password for user u3. Passwd: Success Locking password for user u4. Passwd: Success Locking password for user u5. Passwd: Success

Use the following shell script to check locked user accounts.

# vi user-lock-status.sh #! / bin/bash for user in `cat user- lists.txt` do passwd-S $user done

Set user-lock-status.sh executable permissions.

# chmod + user-lock-status.sh

Finally, run the script to achieve the goal.

# sh user-lock-status.sh U1 LK 2019-06-100 99999 7-1 (Password locked.) U2 LK 2019-06-100 99999 7-1 (Password locked.) U3 LK 2019-06-100 99999 7-1 (Password locked.) U4 LK 2019-06-100 99999 7-1 (Password locked.) U5 LK 2019-06-100 99999 7-1 (Password locked.)

Use the following shell script to unlock multiple users.

# user-unlock.sh #! / bin/bash for user in `cat user- lists.txt` do passwd-u $user done

Set user-unlock.sh executable permissions.

# chmod + user-unlock.sh

Finally, run the script to achieve the goal.

# sh user-unlock.sh Unlocking password for user u1. Passwd: Success Unlocking password for user u2. Passwd: Success Unlocking password for user u3. Passwd: Success Unlocking password for user u4. Passwd: Success Unlocking password for user u5. Passwd: Success

Run the same shell script user-lock-status.sh to check that these locked user accounts are unlocked in Linux.

# sh user-lock-status.sh U1 PS 2019-06-100 99999 7-1 (Password set, SHA512 crypt.) U2 PS 2019-06-100 99999 7-1 (Password set, SHA512 crypt.) U3 PS 2019-06-100 99999 7-1 (Password set, SHA512 crypt.) U4 PS 2019-06-100 99999 7-1 (Password set, SHA512 crypt.) U5 PS 2019-06-100 99999 7-1 (Password set, SHA512 crypt.) Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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