The “useradd: Permission denied” error typically occurs when the user executing the useradd command does not have the necessary permissions to create a new user account.
To fix this error, you can try the following solutions:
Switch to the root user: If you have access to the root user, you can switch to the root user using the su command and then execute the useradd command. This grants you full administrative privileges. For example:
Use superuser privileges: Execute the useradd command with superuser privileges using the sudo command. This allows you to run the command with administrative rights, which should bypass any permission issues. For example:
sudo useradd username;
Contact the system administrator: If you are not the system administrator and do not have the necessary privileges, reach out to the system administrator or the person responsible for managing user accounts. They can assist in resolving the permission issue or perform the necessary actions on your behalf.
In this article, we will explore the details of three solutions.
Table of Contents
Understanding useradd command
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
The useradd command in Linux performs several actions to create a new user account.
To learn more about how useradd command works behind the scenes, you can refer to this article: Understanding the useradd Command in Linux
One of the steps is to add an entry to the /etc/passwd file, containing the user’s username, encrypted password (if provided), UID, default GID (group ID), home directory, and login shell information. This entry is used by the system to authenticate and manage user accounts.
Typically, only the root user or users with administrative privileges can modify this file. You may need to switch to the root user or use the sudo command to run the useradd command with appropriate privileges.
switch to root user and run useradd command
To switch to the root user in Linux, you can use the su command followed by the root user’s password. The steps to switch to the root user are as follows:
Open a terminal or command prompt.
Enter the following command:
You will be prompted to enter the root user’s password. Type the password and press Enter.
If the password is correct, you will be logged in as the root user. The command prompt will change to indicate that you are now the root user. It may look something like this:
Now you have switched to the root user and have administrative privileges.
Run the useradd command to create the new user. It should be good now.
Be cautious when performing actions as the root user, as it grants you unrestricted access to the system. It is generally recommended to switch back to a regular user account when you have completed the necessary administrative tasks. You can do this by typing exit or logout and pressing Enter.
use sudo to run useradd command
The sudo command allows users to execute commands with the privileges of another user, typically the root user, while providing an audit trail of the executed commands. It is commonly used in Linux systems to perform administrative tasks without having to switch to the root user permanently.
Let’s see how to configure sudo in Linux.
To configure sudo in Linux, you need to follow these steps:
1. Open a terminal or command prompt.
2. Switch to the root user by running the following command and providing the root password:
3. Once you are logged in as the root user, run the command to edit the sudoers file using a text editor. The sudoers file is usually located at /etc/sudoers or /etc/sudoers.d/:
Note: It’s important to use the visudo command instead of directly editing the sudoers file with a text editor. visudo performs syntax checking and prevents you from accidentally corrupting the file.
4. The sudoers file will open in the text editor. Scroll down to the section that configures user privileges.
5. To grant a user sudo privileges, add the following line to the file:
username ALL=(ALL) ALL
Replace “username” with the actual username of the user you want to give sudo access to. This line allows the user to run any command with sudo.
Alternatively, you can specify more granular permissions by using the following format:
username ALL=(ALL:ALL) command1, command2, ...
Replace “command1, command2, …” with the specific commands the user is allowed to run with sudo.
Or if you want to add a user to the sudoers file without a password, you need to add the following line to the file:
username ALL=(ALL) NOPASSWD:ALL
6. Save the sudoers file and exit the text editor.
7. The user should now have sudo privileges. They can test it by opening a new terminal window and running a command with sudo:
Replace “command” with the actual command you want to run with elevated privileges. For example, to create a new user howtouselinux, you can use: sudo useradd howtouselinux
The user will be prompted to enter their own password, not the root password, to authenticate.
8. If the password is entered correctly and the command is allowed, it will be executed.
Verify the new user in Linux
To verify if a new user exists in Linux, you can use the id command followed by the username. This command displays information about the user, including their user ID (UID) and group ID (GID). Here’s an example:
Replace “username” with the actual username of the new user you want to verify. When you run this command, it will show the user’s UID, GID, and other related information if the user exists.