Adding user in linux with home directory

15 useradd command examples in Linux [Cheat Sheet]

We all know that we can have multiple users in the Linux systems. But, do you know how to add a new user in the Linux system?

useradd is a simple command-line tool in Linux for adding a new user to the system. useradd is a symbolic link to adduser command. Both the commands have similar functions. System administrators can use this command to manage users in the system.

Different examples to use useradd command

Only root users or users having sudo permissions can execute the useradd command. When the useradd command is successfully executed, it does the following things:

  1. Adds details of a new user to files such as /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow.
  2. Creates a new directory for a new user in the /home.

In this tutorial, you will find the different examples to create a new user in the Linux system by using useradd command.

1. Create a new user using useradd command

This is the most basic command to create a new user in the Linux system. It only requires the name of a user. The name of a new user must be unique from other users in the system.

Sample Output:

useradd command to create a new user

You can check the details of a new user added in /etc/passwd file using grep command. In the above output, you can see the details of a new user elliot. The details include the following things:

  • Username: The username should be between 1-32 characters. It is also the login name for the user.
  • Password: The user password is stored in encrypted format (x) in /etc/shadow file.
  • User ID: Every user has a unique User Identification Number. 1243 is user ID of elliot.
  • Group ID: It shows the primary group ID of a user. The group IDs are stored in /etc/group file. In our case, 1245 is the primary group ID of elliot.
  • User detail: In between two colons, the comment or detail added for a new user will be displayed if any.
  • Directory path: The path for the new user’s directory is displayed. The default location is the home directory.
Читайте также:  Скомпилировать код c linux

2. useradd command to add a new user and user’s directory in home directory

In most Linux systems, the user’s directory is not created in the home directory. You can use -m option to force the useradd command to create the user’s directory in /home.

Sample Output:

useradd command to add new user and user home directory

As you can see in the above output, the directory for user rohan is created but the previously added user elliot has no directory.

3. useradd command to create a new user without directory

In some Linux distributions, useradd command creates a new user and its directory in the home directory. If you do not need the user’s directory, use -M option so the useradd command only creates a new user.

Sample Output:

useradd command to create a new user without directory

4. useradd command to create a new user with custom home directory

You can use -d option to create user’s directory in another location instead of the home directory. That directory must be present in the system.

$ sudo useradd -d /directory_name/user_name user_name

Sample Output:

useradd command to create a new user directory in specific directory

5. Create a new user with specific user ID with useradd command

In the Linux systems, every user has unique user name and user ID. By default, useradd command assigns the next available user ID to a new user. -u or —uid option allows you to create a new user with specific user ID.

$ sudo useradd -u user_ID user_name

Sample Output:

useradd command to add user with specific user id

6. Create a new user with specific group name with useradd command

This command allows you to assign the different group name to a new user. Usually, useradd command provides the same group name as username and the same group ID as the user ID. -g option allows you to assign different group name to a new user. The group name must be already present in the system.

You can check group name of user using id -gn user_name command.

$ sudo useradd -g group_name user_name

Sample Output:

useradd command to add new user with specific group name

The above output shows the user eliza has group linux but the previously added user harry has group harry.

You can also create a new user by using specific group ID instead of group name with useradd command.

$ sudo useradd -g group_ID user_name

Sample Output:

useradd command to create new user using specific group ID

7. Create a new user and assign multiple groups using useradd command

In Linux systems, there are two types of group: Primary group and Secondary group. Every user can associate to only one primary group. But they can associate to none or multiple secondary groups.

-G option specifies the secondary groups to a new user.

$ sudo useradd -G secondary_group1,secondary_group2

Sample Output:

useradd command to create a new user with secondary groups

In the above output, sam is the primary group, and student and computer are the secondary groups.

8. useradd command to add a new user with an expiry date

By default, useradd command does not set the expiry date of a user. -e option allows you to specify the expiry date for a new user. System administrators can use this command to create temporary accounts in the system. The user expiry date can be viewed by using chage -l command.

Читайте также:  Free space linux check

The date must be provided in YYYY-MM-DD format.

$ sudo useradd -e YYYY-MM-DD user_name

Sample Output:

useradd command to create a user with an expiry date

9. Create a new user with comment using useradd command

This command allows you to create a new user with a brief comment. You can add any comment using -c option. This is a useful command to store a short information about the user.

$ sudo useradd -c "comment" user_name

Sample Output:

useradd command to add a user with comment

10. useradd command to create “system user”

Generally, system users are created during the installation of OS and new packages. You can create a new system user using -r option with useradd . The ID of system users and groups ranges from 100-999.

Sample Output:

useradd command to create a new system user

11. Create a new user with custom login shell using useradd command

The default login shell is /bin/sh or bin/bash in most of the Linux distributions. -s option allows you to set the specific login shell for a new user.

Sample Output:

useradd command to create a new user with login shell

12. Declare default values for useradd command

You can view the default useradd values using -D option. It also allows you to edit the values. These values are stored in /etc/default/useradd. To edit the default login shell, you can use:

$ sudo useradd -D -s new_shell

Sample Output:

useradd command to change default login shell

The default login shell of useradd is changed.

useradd command to view the default useradd values

13. useradd command to create a new user with an unencrypted password

-p option allows you to specify an unencrypted password for a new user. You can check the password stored in /etc/shadow file. You will need root permission to access the file.

$ sudo useradd -p password user_name

Sample Output:

useradd command to specify unencrypted password for a new user

14. useradd command to create a new user with duplicate user ID

Generally, every user has a different user ID but -o option allows you to create a new user with duplicate user ID.

$ sudo useradd -o -u user_ID user_name

Sample Output:

useradd command to add new user with duplicate user ID

In the above output, both users elliot and lucy have same user ID.

15. Disable account once password expires after n number of inactive days

You can create a new user by defining a time period after which the user account will be disabled if there is no activity from that user assuming the password of the user has expired. Use —inactive or -f to define the period after which account will be disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

$ sudo useradd --inactive NUM user_name

Conclusion

This article presents the most used useradd command in Linux. Now, you can easily add a new user to the Linux system. You can use these commands in Ubuntu, RHEL, CentOS, Fedora, Debian and any other Linux distributions. useradd command is easy to use and helpful for creating new users in the system.

What’s Next

Further Reading

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Adding New Users in Linux With Useradd Command

The useradd command lets a superuser create a new user account on Linux. Here’s how to use the useradd command with various options.

Читайте также:  Linux port listen process

The useradd command lets a superuser create a new user account in Linux.

It is a low level utility that doesn’t do a lot of things by default but provides several options to create users with various configuration.

Here’s the syntax of useradd command:

Let’s see how to use useradd command.

Useradd command examples

Add User Account In Linux

Keep in mind that you need to be root or a sudo user in order to use this command.

Add a new user in Linux with useradd command

You can use the useradd command without any options like this:

It will create the user account but:

  • the user’s home directory won’t be created
  • the password has to be set separately
  • the default shell for the user will be sh

You can also create home directory while creating the user.

Add a user with home directory with useradd command

The -m option of useradd command allows to copy all files from your system skeleton directory (/etc/skel) to the newly created home directory.

In other words, you can create a user with home directory using the -m option as well.

You can also specify an existing directory as the home directory of the newly created user with option -d.

useradd -d Path_to_Existing_Directory new_username

Add a new user with different shell

The default shell for a user created with useradd command is sh. These days sh shell is hardly used when there is bash and zsh.

A user can change his/er default shell but you can also create the user with a different default shell with the option -s.

For example, if you want the new user to use bash as the default shell, here’s what you can do:

useradd -s /bin/bash new_username

Add a new user with different group

Usually, when you create a new user, a group with the same name as the user is created. The new user is added as the member of this group.

With the option -g, you can add a new user to an already existing group as its default group.

useradd -g Existing_Group_Name_or_ID new_username

Suppose you are creating an account for a developer. Adding all the developers to a ‘dev group’ could be a strategy.

You can also add the user to additional (existing) groups with option -G.

useradd -G group_1 group_2 new_username

So if you are creating a sudo user, you can simply add the user to the sudo group while creating it.

Add a new user with specific user ID (UID)

You may also create a new user with a specific user ID with the option -u of useradd command:

useradd -u uid new_username

Bonus Tip: Combine multiple options to create a new user with different parameters

You can combine multiple options together to create a new user in Linux with a predefined configuration.

useradd -d /home/abhishek -s /bin/bash -g my_group 

As you can see, the useradd command by default doesn’t add much. This is why some people prefer to use the adduser command. You can read about difference between useradd and adduser, if interested.

I hope you found the useradd command examples useful. You may also want to learn to delete users with userdel command. Questions and suggestions are welcome.

Источник

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