This article title may sound awkward to you, but it’s useful. You may ask, why would we want to create a user without a password. I thought that too, but my friend faced this question in one of the interviews.
So, I want to share this with users who are looking for an answer to this question. Practically it may not be useful, but you should know the answer.
This article shows you how to create a user account without a password OR an empty password on Linux.
What is passwd command?
The passwd command is one of the most frequently used command by Linux administrator’s to update users’ authentication tokens in the
/etc/shadow file by calling the Linux-PAM and Libuser API’s.
What is useradd command?
Linux is a multi-user operating system that allows more than one user to interact with the system at the same time. The “useradd” command is used to create new user accounts. When executed, “useradd” command creates a new user account as per the options specified in the ‘/etc/default/useradd file’ & ‘/etc/login.defs’ file.
Now let’s delve into some of the methods used to create a user account without password:
Method-1: Creating a user without password using passwd command
You can create a user without a password on Linux using the “passwd” command as follows :
Run the useradd command to create a user account as shown in the example below.
# useradd mageshm
Note: we can use the commands “useradd” or “adduser” interchangeably to create a user in Linux.
useradd is native binary compiled with the system. But, adduser is a perl script which uses useradd binary in back-end. adduser is more user friendly and interactive than its back-end useradd. There’s no difference in features provided.
Once you have created the user, use the passwd command to remove the user’s password.
# passwd -f -u mageshm Unlocking password for user mageshm. passwd: Success
-f or --forceForce operation
-u or --unlockUnlock the password for the named account (root only)
Method-1.a: Using passwd comamnd
Alternatively, you can use the
-d option with the passwd command to remove the password for a user.
# passwd -d magi Removing password for user magi. passwd: Success
-d or --deleteDelete the password for the named account (root only)
Now, you’ve created a user with “disabled password”. But when you try to access it, it will ask for the password at the same time not allowing you to login.
You might get an error message saying “Access denied”.
That’s the expected behavior and you will not receive an error message such as “No password, you cannot login”.
Method-2: Creating a user without password using chpasswd command
You can create a user with an empty password on Linux using the “chpasswd” command.
Run the below command to create a user account:
# adduser mageshm
Once you have created the user, use the below command to remove the user’s password. The
-e option expects a hashed password, but you are given a simple password, which is why it is not taken.
# echo "mageshm:pass123" | chpasswd -e
-e or --encryptedSupplied passwords are encrypted
Method-3: Creating a user without password using useradd command
You can create a password-free user on Linux using the “useradd” command.
-p option expects a hashed password, but you are provided with a simple password, which is why it is not taken.
# useradd mageshm -s /bin/bash -p 'pass123'
-p or --passwordEncrypted password of the new account
Method-3.a: Using /sbin/nologin shell
User account can be created with the
nologin shell option. This shell usually does not allow the user to log in to the computer.
# useradd -s /sbin/nologin mageshm
You will receive the following message when you attempt to switch the account from the root.
# su - mageshm This account is currently not available.
-s or --shellLogin shell of the new account
Method-3.b: Changing the shell to false
We can use the
/bin/false option with the “useradd” command to create a new user without password. ‘/bin/false’ is just a binary that immediately exits, returning false, when it’s called. The user logs in and immediately sees the login prompt again.
This shell works exactly like the one above, and it does not allow the user to log in to the computer.
# useradd -s /bin/false mageshm
When you try to switch the account from the root you get nothing.
# su - mageshm
Method-3.c: Changing the shell to /dev/null
‘/dev/null’ is a simple device which is implemented in software and doesn’t correspond to any hardware device on the system.
dev/null looks empty when you read from it, whereas data written to this device simply “disappears.”
User can be disabled by changing the shell to
/dev/null as shown below:
# usermod -s /dev/null magesh
You learnt different ways to create a user without a password in Linux.
If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!