Linux lock user account

How to enable or disable a user?

I’m uing ubuntu 12.04 desktop. I have 3 users: user1(administrator), user2(standard) and guest. I wanted to disable user1 and enable user2 which auto logs on with no password but after I did that I can’t login to user1 and user2 accounts except the guest session user. I’m striped off every administrative privileges. I don’t know which options are available to me and how do I enable root or user which is an administrator?

5 Answers 5

Expire Account

Let the account expire to disallowing a user from logging in from any source including ssh:

# disallow peter from logging in sudo usermod --expiredate 1 peter 

This is how you can reenable that account:

# set expiration date of peter to Never sudo usermod --expiredate "" peter 

Lock a Password

To disable / lock the password of user account use below command. This will not disallow ssh-access on Ubuntu. This prepends a ! to the password hash so that no password will match it anymore.

# take away peters password sudo passwd -l peter 
# give peter back his password sudo passwd -u peter 

Expire a Password

This command seems to differ across platforms. On Ubuntu, expiring a password will command the user to make up a new one once he logs in. This is not suitable for disabling an account.

# make peter think of a new password on login sudo passwd -e YYYY-MM-DD peter 

You should merge locking a password and expiring an account into one command as explained in @vorburger’s answer (usermod -L -e 1 [username]) and (usermode -U -e «» [username])

Use this to lock an account:

sudo usermod -L -e 1 [username] 

and this to unlock an account so locked:

sudo usermod -U -e "" [username] 

(Disabling and locking a user account both mean the same thing.)

To disable / lock the user account use below command:

sudo passwd -l [user_name] e.g. sudo passwd -l samual 

To put an expire date to an user account so that it automatically gets disabled / locked.

sudo passwd -e YYYY-MM-DD [user_name] e.g. sudo passwd -e 2013-05-31 samual 

To re-enable a disabled user, issue the passwd command with the -u option.

sudo passwd -u [username] e.g. sudo passwd -u training 

To enable the root account and change the root password use below steps.

Читайте также:  Linux mint поиск драйверов

Enter the new password for root account and then exit. if this does not solve the issue let me know.

Please feel free to let me know if you need anything else or any further clarification.

Источник

UNIX / Linux : How to lock or disable an user account

There are several ways in which user account can be locked or disabled. The posts discusses few of the most widely used ways.

1. Lock the password

To lock a users account use the command usermod -L or passwd -l. Both the commands adds an exclamation mark (“!”) in the second field of the file /etc/passwd. For example :

2. Expire the user account

The commands passwd -l and usermod -L are ineffcient when it comes to disable/lock user accounts. These commands will not disallow authentication by SSH public keys (or other PAM modules other than pam_unix that may be enabled). Additionally, changing the shell to /bin/false or /sbin/nologin is unsatisfactory to us since this only affects interactive logins. So to completely disable user accounts you can user the command chage -E0. For example :

Expiring an account via use of the 8th field in /etc/shadow (using “chage -E”) will block all access methods that use PAM to authenticate a user.

3. Changing the shell

We can also change the default shell of the user to /sbin/nologin so that the user do not get any login shell when he tries to login into the system.

# usermod -s /sbin/nologin [username]

You can check for the 7th and last field in /etc/passwd for the change of shell to /sbin/nologin.

Verify if the account is locked or disabled.

For each of the methods described above, you can verify if the user account is locked/disabled using below methods.

1. Check if the user account is locked.
Check for the flag *LK* in the below command output which indicates that the account is locked.

# passwd --status root root *LK* 2017-07-19 0 45 7 -1 (Password set, SHA512 crypt.)

2. Check if the account has an expire date.
Check for the lines Password expires and Account expires to check if the account password is expired.

# chage -l root Last password change : Jul 19, 2017 Password expires : Sep 02, 2017 Password inactive : never Account expires : Sep 02, 2017 Minimum number of days between password change : 0 Maximum number of days between password change : 45 Number of days of warning before password expires : 7

3. Check for non-interactive shell
Check if the user shell has been changed to a non-interactive shell like /sbin/nologin.

# grep ^root /etc/passwd root:x:0:0:root:/root:/sbin/nologin

Источник

Читайте также:  Восстановить флешку linux mint

How to Lock and Unlock User in Linux

How do you lock a user account in Linux? Even more importantly, how do you unlock the user in Linux? Learn various ways of locking and unlocking users in Linux command line.

3 ways to lock and unlock user accounts in Linux

There could be a number of reasons why you would want to disable a user in your multi-user Linux environment. Perhaps an employee left the organization and instead of deleting the user altogether, lock the account for archival purpose.

In this tutorial, I’ll show you three ways to lock a user in Linux command line. I’ll also discuss how to unlock the user.

Method 1: Lock and unlock users with passwd command

The passwd command in Linux deals with passwords of a user account. You can also use this command to lock a user account.

The command basically works on the /etc/passwd file. You may manually modify this file but I advise against it.

To lock a user with the passwd command, you can use the option -l or –lock in this manner:

Verify the status of a user with passwd command

You can learn whether a user is locked or unlocked using the option -S or –status of passwd command.

passwd -S user_name

Look at the second field in the output. Here’s what it means:

– P or PS: password is set (user is unlocked)
– L or LK: User is locked
– N or NP: No password is needed by the user

Here’s a sample output of the passwd command:

standard P 10/14/2019 0 99999 7 -1

To unlock the user with passwd command, you can use the option -u or –unlock:

On Ubuntu, you’ll see an output like this for both locking and unlocking the user:

passwd: password expiry information changed

What about login via SSH?

There is a major problem with locking users this way. Since it only works with the /etc/passwd file, the locked user will still be able to log in via SSH keys (if login via SSH key is set). I’ll show you how to deal with it in the next section.

Method 2: Lock and unlock users with usermod command

You can also use the usermod command. The command is primarily used for modifying user accounts in Linux. You can also modify the state of a user by locking or unlocking with usermod.

Читайте также:  Standard commands in linux

To lock the user, you can use the -L option in this manner:

To unlock the user, you can use the -U option:

How do you verify if the user is locked or not? The usermod command also works on the /etc/passwd file so you can use the passwd -S user_name command to check the status of the user.

But usermod also works with the /etc/passwd file so this means the locked user could still be able to login via SSH keys, right? That’s right. But there are ways to overcome this problem.

For example, you can change the shell of the user to nologin and this will not allow the user to login to a shell.

Another method is to lock the user and provide an expired date in the past. What it does is that it disables the account on a past date and then locks it.

Make sure that the past date is between 1970-01-02 and the current date.

usermod -L --expiredate 1970-01-02 user_name

You can reverse it with this command:

usermod -U --expiredate '' user_name

There is a similar way to lock the user in Linux with chage command. Let’s see it in the next section.

Method 3: Lock and unlock users with chage command

The chage command is used for changing the user password expiry information. It can be used to automatically lock an inactive user after certain number of days of inactivity.

Basically what you did with the usermod command in the previous section can be achieved with chage command like this:

Basically, you have set the expired date to 1970-01-02. You can see the details like this:

[email protected]:~$ sudo chage -l standard Last password change : Nov 07, 2019 Password expires : never Password inactive : never Account expires : Jan 02, 1970 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7

You can remove the expiry date and thus unlock the user in this manner:

As always, there are various ways to accomplish a task in Linux command line. I have shown here three methods to lock and unlock users in Linux. Do you know a better way or do you have some best practices suggestion for locking users? Do share it in the comment section.

Источник

Оцените статью
Adblock
detector