How to create users with usseradd comand.

How To Create Users With Usseradd Comand.

In this article, we will talk about how to create new user accounts using the useradd command.

 

useradd Command

The general syntax for the useradd command is as follows:

useradd [OPTIONS] liviu.daniel .daniel 

You can add users only if you have sudo privileges.

 

How to Create a New User in Linux.

To create a new user account, invoke the useradd command followed by the name of the user.

For example to create a new user named liviu.daniel you would run:

sudo useradd liviu.daniel

When executed without any option, useradd creates a new user account using the default settings specified in the /etc/default/useradd file.

sudo passwd liviu.daniel

Now wee must set password

Changing password for user liviu.daniel .daniel .
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

 

How to Add a New User and Create Home Directory

sudo useradd -m liviu.daniel .daniel 

The command above creates the new user’s home directory and copies files from /etc/skel directory to the user’s home directory. If you list the files in the /home/liviu.daniel directory, you will see the initialization files:

ls -la /home/liviu.daniel/
drwx------  2 liviu.daniel liviu.daniel 4096 Jun 28 10:54 .
drwxr-xr-x. 3 root         root         4096 Jun 28 10:54 ..
-rw-r--r--  1 liviu.daniel liviu.daniel   18 Nov 24  2021 .bash_logout
-rw-r--r--  1 liviu.daniel liviu.daniel  193 Nov 24  2021 .bash_profile
-rw-r--r--  1 liviu.daniel liviu.daniel  231 Nov 24  2021 .bashrc

Within the home directory, the user can write, edit and delete files and directories.

 

Creating a User with Specific Home Directory

By default useradd creates the user’s home directory in /home. If you want to create the user’s home directory in other location, use the d (–home) option.

Here is an example showing how to create a new user named liviu.daniel .daniel with a home directory of /opt/liviu.daniel .daniel :

sudo useradd -m -d /opt/liviu.daniel .daniel  liviu.daniel .daniel 

 

Creating a User with Specific User ID

In Linux and Unix-like operating systems, users are identified by unique UID and liviu.daniel .daniel .

By default, when a new user is created, the system assigns the next available UID from the range of user IDs specified in the login.defs file.

Invoke useradd with the -u (–uid) option to create a user with a specific UID. For example to create a new user named liviu.daniel .daniel with UID of 1500 you would type:

sudo useradd -u 1500 liviu.daniel

You can verify the user’s UID, using the id command:

id -u liviu.daniel 
1000

 

Creating a User with Specific Group ID

When creating a new user, the default behavior of the useradd command is to create a group with the same name as the liviu.daniel .daniel , and same GID as UID.

The -g (–gid) option allows you to create a user with a specific initial login group. You can specify either the group name or the GID number. The group name or GID must already exist.

The following example shows how to create a new user named liviu.daniel .daniel and set the login group to users type:

sudo useradd -g users liviu.daniel

To verify the user’s GID, use the id command:

id -gn liviu.daniel 
liviu.daniel 

 

Creating a User and Assign Multiple Groups

You to specify a list of supplementary groups which the user will be a member of with the -G (–groups) option.

The following command creates a new user named liviu.daniel .daniel with primary group users and secondary groups wheel and docker.

sudo useradd -g users -G wheel,developers liviu.daniel

You can check the user groups by typing

id liviu.daniel
uid=1002(liviu.daniel) gid=100(users) groups=100(users),10(wheel),993(docker)

 

Creating a User with Specific Login Shell

By default, the new user’s login shell is set to the one specified in the /etc/default/useradd file. In some distributions the default shell is set to /bin/sh while in others it is set to /bin/bash.

The -s (–shell) option allows you to specify the new user’s login shell.

For example, to create a new user named liviu.daniel .daniel with /usr/bin/zsh as a login shell type:

sudo useradd -s /usr/bin/zsh liviu.daniel .daniel 

Check the user entry in the /etc/passwd file to verify the user’s login shell:

grep liviu.daniel .daniel  /etc/passwd
liviu.daniel .daniel :x :1001:1001::/home/liviu.daniel .daniel :/usr/bin/zsh

 

Creating a User with Custom Comment

The -c (–comment) option allows you to add a short description for the new user. Typically the user’s full name or the contact information are added as a comment.

In the following example, we are creating a new user named liviu.daniel .daniel with text string Test User Account as a comment:

sudo useradd -c "Test User Account" liviu.daniel .daniel 

The comment is saved in /etc/passwd file:

grep liviu.daniel .daniel  /etc/passwd
liviu.daniel .daniel :x :1001:1001:Test User Account:/home/liviu.daniel .daniel :/bin/sh

The comment field is also known as GECOS.

 

Creating a User with an Expiry Date

To define a time at which the new user accounts will expire, use the -e (–expiredate) option. This is useful for creating temporary accounts.

The date must be specified using the YYYY-MM-DD format.

For example to create a new user account named liviu.daniel .daniel with an expiry time set to January 22 2019 you would run:

sudo useradd -e 2019-01-22 liviu.daniel

Use the chage command to verify the user account expiry date:

sudo chage -l liviu.daniel

The output will look something like this:

Last password change                                    : Jun 28, 2023
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

 

Creating a System User

Use the -r (–system) option to create a system user account. For example, to create a new system user named liviu.daniel .daniel you would run:

sudo useradd -r liviu.daniel 

System users are created with no expiry date. Their UIDs are chosen from the range of system user IDs specified in the login.defs file, which is different than the range used for normal users.

 

Changing the Default useradd Values

The default useradd options can be viewed and changed using the -D, –defaults option, or by manually editing the values in the /etc/default/useradd file.

To view the current default options type:

useradd -D

The output will look something like this:

GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

Let’s say you want to change the default login shell from /bin/sh to /bin/bash. To do that, specify the new shell as shown below:

sudo useradd -D -s /bin/bash

You can verify that the default shell value is changed by running the following command:

sudo useradd -D | grep -i shell
SHELL=/bin/bash

 

Conclusion

We have shown you how to create new user accounts using the useradd command. The same instructions apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux.