Add user in linux with home directory

15+ adduser command examples in Linux [Cheat Sheet]

adduser is a command-line utility to create a new user in the Linux system. It is a friendlier frontend to the low-level tool ‘useradd’. When adding a new user, it also creates the user directory under the /home directory.

How adduser command works

The default configuration file for adduser is /etc/adduser.conf. adduser command will work according to the default value specified in adduser.conf file.

adduser command adds the details of the new user to the following files.

  • /etc/passwd — It stores user account information.
  • /etc/shadow — It contains the password information of users. The passwords are stored in an encrypted format.
  • /etc/group — It stores group information.

Different examples to use adduser command

You can use adduser command to add a normal user, system user, or user group in the Linux system. Only users with root privileges can add a user to the system. So, you will need to log in as a root user or use sudo to execute adduser command.

The general syntax of adduser command is:

Now, let’s see some practical examples of adduser command to add a user or group in the Linux system.

1. adduser command to add a new user

With this command, you can add a new user to the system. When adding a new user, you will be asked to enter some information.

Sample Output:

adduser command to add a new user

/etc/passwd contains the user account information in the following format.

username:password:userID:groupID:comment:user_directory:shell
  • Username: It is the login name for the user. The username should be between 1-32 characters.
  • Password: It contains the user password in an encrypted format (x).
  • User ID: Every user has a unique User Identification Number (UID). 1001 is the user ID of deepak.
  • Group ID: It shows the primary group ID of a user. 1001 is the primary group ID of deepak.
  • User detail: In between two colons, a GECOS or comment of a user will be displayed if added.
  • Home Directory: The user’s home directory. The default location is the /home directory.
  • Login shell: The login shell for the user. The default login shell is /bin/bash.
Читайте также:  Vipnet pki client linux установка

2. Add a system user with adduser command

You can add a new system user using —system option with adduser command.

Sample Output:

adduser command to add a system user

3. adduser command to create a new user group

—group option allows you to create a new user group in the system. The group is created without any users.

Sample Output:

ubuntu@golinux:~$ sudo adduser --group linux Adding group `linux' (GID 1002) . Done.

4. Add a system group with adduser command

We can use —system and —group option together to create a system group in the system. A system user with the same group name is also created.

$ sudo adduser --system --group group

Sample Output:

adduser command to add a system group

5. adduser command to add a new user in different primary group

adduser command adds a new group for a user with the same username. To add a user in a different group, we can use —ingroup option. The group should be already present in the system.

$ sudo adduser --ingroup GROUP user

Sample Output:

adduser command to add a user in different group

6. adduser command to add a user with different home directory

By default, adduser creates the user’s home directory with username under the /home directory. We can create a user’s home directory in a different location using —home option. It also allows us to specify a different name for the user’s home directory.

It is recommended to make sure the new home path doesn’t exist as it will be automatically created by adduser command with proper permission.

$ sudo adduser --home /directory/home_dir_name user 

Sample Output:

Here, we are adding a user ‘sam‘ with its directory ‘record‘ under the /snap directory.

adduser command to add a user with different home directory

7. adduser command to create a user without a home directory

Normally, adduser command automatically creates the user’s home directory when adding a user. We can use —no-create-home option to add a user without its home directory.

$ sudo adduser --no-create-home user

Sample Output:

adduser command to create a user without home directory

As we can see, the default home directory is /home/elliot but it is not created.

check home directory

8. Add a user with specific user ID with adduser command

Every user has a unique user ID in the Linux system. By default, adduser command assigns the next available user ID to a new user. We can use -u or —uid option to add a new user with a custom user ID.

$ sudo adduser -u userID user
$ sudo adduser --uid userID user

Sample Output:

The user chris is assigned with a custom user ID 4567.

adduser command to add a user with custom user ID

9. adduser command to create a user with specific group ID

We have already seen that the adduser command creates a new group with a user name and adds a user to that group. To add a user to a different group, we can use —gid option. The group should be already present in the system.

$ sudo adduser --gid groupID user

Sample Output:

Читайте также:  Running windows program in linux

adduser command to add a user with different group ID

10. adduser command to add a user with a custom login shell

adduser command selects the default login shell specified by DSHELL variable in /etc/adduser.conf. The default login shell is /bin/bash. To use a different login shell, we can use —shell option as shown below.

$ sudo adduser --shell SHELL user

Sample Output:

adduser command to create a new user with different shell

11. Add an existing user to an existing group with adduser command

Instead of adding a new user, you can also add an existing user to an existing group using adduser command.

Sample Output:

add existing user to an existing group with adduser command

12. adduser command to create a user with a disabled login

—disabled-login option does not ask to set a password for a new user. The user won’t be able to use the account until the password is set.

$ sudo adduser --disabled-login user

Sample Output:

adduser command to add a user with disabled login

We can set or change the user password in the Linux system by using the following passwd command.

ubuntu@golinux:~$ sudo passwd maxim New password: Retype new password: passwd: password updated successfully

13. Create a user without password using adduser command

—disabled-password also does not ask to set a password when adding a user. It is similar to the above command —disabled-login .

$ sudo adduser --disabled-password user

Sample Output:

adduser command to add a user without password

14. adduser command to add a user with a GECOS or comment

A GECOS or comment field is generally used for keeping the information related to the user. We can use —gecos option to add a GECOS or comment when creating a new user.

$ sudo adduser --gecos GECOS user

Sample Output:

adduser command to add a gecos or comment

15. adduser command to print the debug information

We can use —debug option to print the information in a verbose mode. It is useful when debugging the adduser.

Sample Output:

adduser command to print the debug information

16. Hide informational messages with adduser command

—quiet option hides the informational messages in the output. It does not hide warnings and errors.

Sample Output:

adduser command to suppress informational messages

Conclusion

adduser is a useful command to add a user or group in the Linux system. We hope you have learned to use adduser command from this article. If you still have any confusion, please feel free to ask us in the comment section.

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.

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.

Читайте также:  Удалить старую версию linux

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