Which command is used to add a new user to the directory?
If more than one person is using your Linux machine at home, or you are managing a server that provides access to multiple users, the Also, many of the services you use as a developer may require their own user accounts to function. So even as a solo dev on your own machine, you may find yourself reaching for these commands when you install MySQL or something similar. You can get a full overview of the
various options available to you by viewing the man page for the utility: But if that is overwhelming, here's a breakdown of some of the common options you might use when creating a user. The simple format for this command is For example This will create a user called test, but it's a limited operation and will not create other
useful things like their home directory or password! You then add a password for the test user by using the There is an option for adding an encrypted password via the Note that the In order to create a user with the default home directory use the following option: This user now has a /home/test directory. To change the home directory, you can pass an extra option to modify this, for example: By default, your created users will likely have the default login shell bin/bash or bin/sh, which will be defined in You can override this default with the To construct the whole command, you put the options in one after another - the order does not matter - and end with the username you wish to create. So creating a user with a home directory and a
customized shell would look like this: And then you would add a password for the user: Now that you've seen the basics of what this tool can do, hopefully the man page is a little more navigable. Learn to code for free. freeCodeCamp's open source curriculum has helped more than
40,000 people get jobs as developers. Get started Introduction Linux is a multiuser environment, and user provisioning is an essential responsibility in system management. System administrators add, delete, and manage users and groups on the system. The This article shows how to create and add users in Linux. Prerequisites
useradd Command SyntaxThe basic syntax for the
Running the command creates a new user account or updates an existing user according to the values in:
The default values vary between different systems. The rules limit the username to:
The regular expression for checking the username validity is:
Note: Debian follows a different set of rules for usernames. However, the provided rules are a good starting point for all systems to help avoid problems. In addition, the command also creates a group for the new user. Adding a new user requires sudo permissions to modify the files for storing user and group information. useradd Command OptionsThe
Creating New Users in LinuxCreating new users in Linux does the following: 1. Provides a unique UID and GID.
Note: Numbers vary between different operating systems. The example values are for Ubuntu. 2. Edits files that store account information.
3. Sets user permissions on the home directory through the group. Follow the examples below to see how to add users in Linux. Adding a User in LinuxTo add a user in Linux, run the following command in the terminal:
If prompted, enter the sudo password to continue. Without any options, the
The command prompts to enter and confirm the password. After creating a password, a new entry appears automatically in the /etc/passwd file. To see the information, view the file with the cat command and grep for the user:
The fields are in the following format:
The Adding a User in Linux and Creating Home DirectoryBy default, the To create a user and the home directory automatically, use the
Check if the directory exists with the ls command:
The directory contains initialization files copied from the /etc/skel directory. Adding a User with a Specific Home DirectoryTo add a user in Linux with a specific home directory, use the
The Adding a User without Home DirectoryIf the /etc/login.defs configuration To override the default settings and add a user without a home directory, use the
The command adds the user without creating a home directory, overriding the default settings. Adding a User with Specific User IDTo add a user with a specific user ID, use the
If the UID is not unique, the terminal outputs a message and does not add the user. Check the UID with the
Use a UID above 1000 to indicate a regular user. Adding a User with Specific Group IDTo create a user and add them to a specific group, use the
The group name or GID must exist. Otherwise, the command throws an error. Check the user's GID with:
The output prints the user's group ID. Adding a User to Multiple GroupsAdd a user to multiple groups with the
Check the user's groups with:
The groups must exist, and the list should not contain any spaces. The command doesn't add the user if any groups do not exist. Adding a User with a Specific Login ShellEach new user gets a default login shell (such as the Bourne shell or Bourne Again Shell). To explicitly define
the user's shell, add the
For example, to add a user and set Bash as the default login shell, run:
Check the /etc/passwd file to confirm the shell selection:
The final field shows the login shell for the new user. Adding a User with a Specific CommentTo add a user with a specific comment, run:
To view the comment, check the /etc/passwd file and grep for the user:
The comment is for descriptive purposes only and has no actual functionality. Adding a User with Account Expiry DateAdd a user with an account expiry date to automatically delete the account after provided date:
View the account's expiry information with:
The output prints the account expiry date
( Adding a User with a Deactivation PeriodIf a user has a password expiry set, the Use the
For example, to have an account deactivate three days after the password expires, run:
View the expiry information with:
The /etc/shadow file stores password information, including idle time (third to the last field). Use this method to deactivate users who don't change their password in the provided timeframe. The deactivation period is a good security measure, and the recommended duration is 35 days. Business requirements dictate what's the perfect duration before the account expires. If the value is too low, the consequences are costly for an administrator, whereas a high value impacts security. Adding a System UserPrograms and systems create system user accounts, which are different from regular users. Programs such as MySQL or Tomcat require a unique user account to work on the system, and daemons typically create system users during installation. To create a system user, use the
Check the user's information with:
The user has a UID lower than 1000, indicating it's a system user. The adduser CommandThe To add a user, run:
The command prints the user and group to the console. Next, the command asks for the following:
Press Y to complete the process. In case of a mistake, press N and reenter the correct information. Check the parameters from the
The command adds all the values entered during the command execution and the Bash shell by default. Add Multiple UsersThe For example, to add ten users, do the following: 1. Create a text file using a text editor:
2. Append usernames to the file, entering each on a new line. For example:
Save the file and close nano (CTRL+X, Y, then Enter). 3. Use a
4. Show the created users with:
To add passwords, exchange the command in the Conclusion After completing this guide, you know how to add new users to a Linux system with the Next, read about privileged access management and how it helps reduce security attacks and data breaches. What command is used to add a new user?To add/create a new user, you've to follow the command 'useradd' or 'adduser' with 'username'. The 'username' is a user login name, that is used by a user to login into the system.
What is the command used to add new user in Linux?useradd is a command in Linux that is used to add user accounts to your system.
How do I add a user to a specific directory?Creating a user with a custom home directory
By default, useradd will create the user's home directory under “/home”. To specify the home directory in a different location, use the flag “-d”. Note that the directory must exist beforehand. As always, use passwd to assign a login password for the new user.
What command would you use to create the directory new directory?First of all, create a directory and named it as New_directory, execute the mkdir command as follows: mkdir New_directory.
|