The Wayback Machine - https://web.archive.org/web/20241122102755/https://www.geeksforgeeks.org/useradd-command-in-linux-with-examples/
Open In App

How to add User in Linux | useradd Command

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the background. It makes changes to the following files:

  • /etc/passwd
  • /etc/shadow
  • /etc/group
  • /etc/gshadow
  • creates a directory for new user in /home

Syntax of `useradd` command in Linux

The basic syntac for the `useradd` command is as follows.

useradd [options] [User_name]

Working with `useradd` Command

1. How to Add a User in Linux

This command will add the user named “test_user”.

sudo useradd test_user

2. How to Add User by Specifying a home directory path for the new user

To give a home directory path for new users, we use the following command in Linux.

sudo useradd -d /home/test_user test_user

This will set the home directory of the us”/home/test_user”.

3. How to Create a User with a Specific User ID (UID)

To create a new user with a custom UID, we use the following command.

sudo useradd -u 1234 test_user

This will create a new user with the user-id “1234” and the name “test_user”.

4. How to Create a User with a Specific Group ID (GID)

To create a new user and assign a specific group ID, use the following command

sudo useradd -g 1000 test_user

This will create a new user with the group id “1000” and the name “test_user”.

5. How to Create a User Without a Home Directory

To create a user without a home directory, we use the following command.

sudo useradd -M test_user

This will create the user with the name “test_user” and that too without a home directory.

6. How to Create User with an Expiry Date

To set an expiry date for a user account, we use the following command.

sudo useradd -e 2020-05-30 test_user

This will create the user named “test_user” with the expiry date of 30th May 2020.

7. How to Create User with a Comment

To add a comment or description for a user, we use the following command.

sudo useradd -c "This is a test user" test_user

This will create a user with a short comment or description of the user.

8. How to Create a User with Changed Login Shell

To create a user with a different login shell, we use the following command.

sudo useradd -s /bin/sh test_user

This will create a user named “test_user” with the default shell /bin/sh.

9. How to Set an Unencrypted Password for the User

To set an unencrypted password for the user, we use the following command.

sudo useradd -p test_password test_user

This will create a new user with the name “test_user” and an unencrypted password “test_password”.

10. Displaying Help

To display help, we use the following command.

sudo useradd --help

This command will display the help section of the useradd command.

Conclusion

In this article we have discussed the `useradd` command in Linux which is a powerful tool for creating user accounts with various customizations. Overall, we can say that by understanding its options and syntax, system administrators can efficiently manage user accounts on their Linux system.

How to add User in Linux | useradd Command – FAQs

How to add a user in Linux?

To add a new user in Linux, use the useradd command followed by the username. For example, to add a user named “newuser”:

sudo useradd newuser

After creating the user, set a password for the new user:

sudo passwd newuser

What is the command for adduser in Linux?

The adduser command is a more user-friendly way to add a new user and is often a symbolic link to useradd. To add a user named “newuser”:

sudo adduser newuser

This command will prompt for additional information and set up the user’s home directory and password.

How to set user ID in Linux?

To set a specific user ID (UID) while creating a new user, use the -u option with the useradd command. For example, to create a user “newuser” with UID 1001:

sudo useradd -u 1001 newuser

How to set username in Linux?

To set or change a username in Linux, you generally need to rename the user. Use the usermod command to change the username. For example, to change the username from “olduser” to “newuser”:

sudo usermod -l newuser olduser

Ensure to update the home directory if necessary:

sudo usermod -d /home/newuser -m newuser

How to list users in Linux?

To list all users in Linux, you can view the /etc/passwd file, which contains user account information:

cat /etc/passwd

For a cleaner list of usernames, you can cut the first field (username) from the /etc/passwd file:

cut -d: -f1 /etc/passwd

Alternatively, use the getent command to list users:

getent passwd


Previous Article
Next Article

Similar Reads

Manually Add a User Without Tools Like useradd and adduser in Linux
Adding a user manually in Linux without using tools like useradd or adduser involves modifying system files directly. This method is not recommended for regular use, as it requires careful editing of system files and can lead to errors if not done correctly. However, in some situations, such as when these tools are not available or not working, it
9 min read
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user pa
8 min read
How to Delete User in Linux | userdel Command
Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel' command, a powerful tool in Linux for removing u
6 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 min read
How to Add User to a Group in Linux
A group in Linux is a way to put users with similar access and permissions in a collection. By using groups, an administrator can define access control and permissions for all the users belonging to that group. Without groups, the administrator would have to define roles for individual users however, with groups those roles can be given to a group
7 min read
GithubFind3r - Fast Command Line Repo/User/Commit Search Tool
GithubFind3r is an automated tool developed in Node JS which is a CLI-based tool used to get information about the Repositories, Commits, and information about the Users from GitHub. GithubFind3r is available on GitHub, it's free and open-source to use. The GithubFind3r tool displays the results in a more sophisticated and more attractive manner. N
2 min read
Configure Passwordless Sudo For A Specific User in Linux
Linux has a tough permission rule. Being a root user you are permitted to do anything however the normal user has only some allowed permissions. To run many other commands, they required to ask permissions from the superuser, using keyword sudo after which they need to enter the password. This requirement of the password after sudo command can be r
1 min read
Anonymity and Privacy For Linux User
Today, more than 3 billion of the world's population is connected to the virtual world known as the internet in some way or another. And the size of the hard-wares and data centers is on the increase every year due to insufficient users' information holding capacity. The lack of awareness and knowledge behind the proper functioning of the internet
6 min read
Deleting a User in Linux using Python Script
Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't th
2 min read
Creating a User With an Expiry Date in Linux
Linux is a multi-user system and thus, it allows more than one person to interact with the system at the same time. There are numerous ways in which we can create new users in Linux. The system administrator has the responsibility to manage different users of the system. For types of users in Linux refer Users in Linux System Administration. User a
2 min read
How to Block or Disable Normal User Logins in Linux?
Here we will see how to block or disable normal user logins in Linux. This is a good idea to prevent Normal users from connecting to your system. We will see how to block Normal user logins using /etc/nologin file. We are going to tell the users that what is actually happening by showing them a message. The main function of /etc/nologin file is to
2 min read
Scylla – Phone Number & User Information Gathering Tool in Kali Linux
Scylla is a free and open-source tool available on Github. Scylla is based upon the concept of Open Source Intelligence (OSINT). This tool is used for information gathering. Scylla is written in python language. You must have python language installed in your Kali Linux in order to use the Scylla tool. Scylla is an advanced tool that allows it's us
4 min read
How to Force User to Change Password at Next Login in Linux
For security reasons, you must frequently change the password of accounts on the system. In this article, we are going to see how we can force the user to change the password on the next login in Linux. We can do that by two methods, one is using `passwd` command and the other is by using the `chage` command. Using passwd commandTo force a user to
3 min read
User Space Debugging Tools in Linux
In this article, we will discuss debugging tools in Linux. Debugging Tools are those programs that allow us to monitor, control, and correct other program's error while they execute. Some of the debugging tools are as follows:- 'print Statement'Querying (/proc, /sys etc)Tracing (strace/ltrace)Valgrind (memwatch)GDB1. 'print' Statement The 'print' s
3 min read
Get Username by User ID in Linux
The need to get user names by User ID is frequently encountered by system administrators in the Linux world. This task is necessary to manage permissions, diagnose, or simply identify users of the Linux system for different management purposes. Fortunately, Linux provides several methods to achieve this objective, each with its different advantages
3 min read
How to list all locked user accounts in linux?
Linux is great for managing administration tasks, which include managing access, authorizations, and file permissions in a system. While managing user accounts and groups you might encounter some accounts which need to be blocked or locked in order to ensure safety. But once you lock an account how can you see which all accounts are locked or banne
4 min read
How to Grant Admin Privileges to a User in Linux
Linux is an operating system that is open source operating system. There are many distros available in Linux distribution like Ubuntu, Kali Linux, Arch Linux, and Red Hat Linux. In this article, we will understand how to give admin privileges to non-admin users in the Ubuntu Operating system. Introduction to Permissions, Users, and GroupsLinux is a
5 min read
What is umask and how to set umask of Apache user in linux
In the Linux environment, the user mask or umask plays the most important role in specifying default permission for newly created files and directories. Umast filters or subtracts permission bits from the default permissions, ensuring it is a more secure and controlled environment. In Apache, configuring the umask is important for managing the secu
4 min read
How to Change The Primary Group of a User in Linux?
In the field of Linux system administration, learning the modification of the primary group is essential. In this article, we are going to deliver concise details about user groups and how to manage them. What is a Primary Group?In Linux, every user is associated with a primary group. The primary group is a fundamental concept in Unix-like operatin
7 min read
How to Check the Groups a User Belongs to in Linux?
This article shows how to check the groups a user belongs to in Linux operating systems. We introduce some concepts related to the topic and then describe how to do so using the groups command available on the terminal. Groups in LinuxAll Linux operating systems are designed as multi-user operating systems. This means that they provide the capabili
8 min read
How to restrict a user's login to certain times of the day or week in linux?
In this article, we explore the significance of time-based user login restrictions in Linux as an important security measure. By leveraging Pluggable Authentication Modules (PAM) and time configuration files, administrators can define specific periods when users are allowed or denied access. This detailed article outlines the step-by-step process,
4 min read
How to use User Quotas in Linux
The control of user quotas in Linux is a very effective facility that provides system administrators with the ability to limit disks usage of particular users or groups of users in order to protect against disk overloading. When quotas are set, it will be easier to stop any user or even a group from using most of the disks hence causing a DOS. Thir
7 min read
Introduction to Graphical User Interface of RedHat Linux Operating System
Red Hat Linux-based operating system from Red Hat designed for business. It's one of the highly used Enterprise Operating System in the Linux market. It's an open-source Operating System. Feasible to work on desktops, on servers, or on the cloud.  Red Hat and Its community-supported counterpart. Fedora, are among the most widely used Linux distribu
3 min read
How to create a Shared Folder between two Local User in Linux?
This article shows how to set-up a shared folder between two local users in Linux. The shared directory/folder will be accessible to both the users, they will be able to read/write each other's file. Let us create shared directory /home/shareFolder for user Bob and Alice and add them to a common group named projectA. Note: You can create the uses B
2 min read
User Management in Linux
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID
5 min read
How to unlock a locked user account in linux?
To regulate access and maintain security, Linux/Unix systems must have the ability to lock and unlock user accounts. It offers the adaptability required to handle a range of security and operational scenarios while ensuring that only authorized users have access. In this article, we’ll explore why this functionality is important and cover some of t
7 min read
How to Add User to Sudoers in Ubuntu
Sudo is a command line tool in a Unix-based system that allows a regular user to run a command as the superuser or another user. Only the super user is allowed to modify the majority of the System-wide settings and configurations. It is not advisable to run any Linux machine as a super user, so always create a normal user for day-to-day usage. To u
5 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
three90RightbarBannerImg