10 C
Texas

How to create users in Linux with the useradd command

For many users, obtaining privacy, personalization and above all, making the most of Linux resources. Because it is a multi-user system, this means that more than one person uses the same system at the same time. Also, among so many users, it is necessary that there is one who directs the rest, in this case, would be the system administrator. Similarly, it has the ability to manage the users and groups of the platform, creating, eliminating, or assigning users to different groups in the system. To do all this, we will use the command useradd.

However, this process may present some difficulties for those users who do not have much experience with the operating system. In this article, we will explain how to create new user accounts in Linux. Using the useradd command.

Useradd command

The command userdd, basically in Linux, allows users to create user accounts. Also, user accounts by parameter may or may not have:

  • Account name
  • User ID
  • Group ID
  • Name of person or service
  • Personal or home directory
  • Shell to use

The files to be modified will be passwd, shadow, group, and gshadow, all in the /etc directory.

- Advertisement -

On the other hand, the general syntax for the useradd command is:

useradd [OPTIONS] USERNAME

Similarly, only root users with sudo privileges can use the useradd command to create new user accounts.

Useradd command: how to create a new Linux user?

To create a new user account, simply enter the command useradd followed by user name.

For example, to create a user called marquez-osradar you execute the following command:

sudo useradd marquez-osradar

Subsequently, it runs without any other options. The useradd command creates a new user account using the default settings specified in the /etc/default/useradd file

This command adds an entry to the files /etc/passwd , /etc/shadow/ and /etc/gshadow/

Likewise, to enter the user that has just been created, it is only necessary to set the password of that user. To do this, you need to run the passwd command followed by the username, like this:

sudo passwd marquez-osradar

Afterward, the system will invite you to confirm the password. Be sure to enter a strong password.

How to add a new user and create a home directory

In most of the various Linux distributions, when a new user account is created with the useradd command. The user’s home directory is not created at the same time.

To solve this problem, they use the option -m (--create-home) to create the user home directory as /home/marquez-osradar

sudo useradd –m marquez-osradar

This command creates the new user’s home directory and makes a copy of files from the /etc/skel directory to the user’s home directory.

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

-m Means create the main directory if it does not exist.

Create a user with a specific home directory with the command useradd

By default, the useradd command creates the user’s home directory in /home . If the user wants to create the user’s home directory in another location, just use the d (--home) option

For example, create a new user named marquez-osradar with a home directory of /opt/ marquez-osradar

sudo useradd -m -d /opt/ marquez-osradar marquez-osradar

-d It means that they will use the main directory instead of the default directory.

Create a user a specific user ID

On Linux and all Unix-like operating systems, users identify it by a UID and a user name.

In addition, the user identifier (UID) is a unique integer assigned by the Linux system for each user. Similarly, the UID and other access control policies will be used to set the types of actions. That a user can exercise on system resources.

However, by default when a new user is created, the system assigns it the following UID. Which is available from the variety of specific user IDs in the login.defs file

To use this option, just enter useradd with the -u (--uid) option to create a user with a specific UID. For example, to create a user called marquez-osradar with a UID of 2000, enter the following:

sudo useradd -u 2000 marquez-osradar

-u Means User ID, which must be unique and greater than 499.

If you want to check the user’s UID, just enter the command id:

id -u marquez-osradar

departure

2000

How to create a user with an expiration date in Linux

To set a date for the user’s account to become invalid, you can use the -e (-expiredate) option. This option is extremely useful for temporary accounts.

Similarly, if you want to do it easily and without problems, just specify the date in YYYY-MM-DD format.

For example, to create an account called marquez-osradar with an expiration date of December 31, 2020. Then execute the command as follows:

sudo useradd -e 2020-12-31 marquez-osradar

They can also use the chage command to check the expiry date of the account:

sudo chage -l marquez-osradar

With this, you will have already configured the account with an expiration date.

We leave you with the various articles on the commands. Clicking here.

Create a user and assign it to several groups

In the Linux operating system, there is the primary group and the secondary or supplementary group. Also, each group can belong specifically to a primary group, and zero or more secondary groups.

Similarly, the group creator must specify a list of subgroups that the user will be a member of with the -g (--groups)

Next, you will see the command to create a new user with the name marquez-osradar whose primary group is students. Besides that, the secondary group are computer scientists and programmers.

sudo useradd -g estudiantes -G informaticos, programadores marquez-osradar

To check the user groups, enter the following command:

id marquez-osradar

departure

uid=1005(marquez-osradar) gid=300(students) groups=300(students),20(computer scientists),883(programmers)

-g Means the name or group number for the user’s default group.

-G means list of names of additional groups, separated by commas, of which the user is a member.

Useradd Command: Set a Password

Once you have created a user account if the creator wants to have more privacy or protection. It is time to create a password for that user. It is advisable to use a password with a good level of security.

  • Combine upper and lower case letters and numbers.
  • Do not use sequences of numbers.
  • Add special characters.

To create the password, the set command is passwd followed by the user’s name. For example, to set the password for the user marquez-osradar is used:

passwd marquez-osradar

Once the command has been executed, your shell will ask you to enter your password and then confirm it. For security reasons, Linux doesn’t display the password or hidden characters while you type, so you should do so very carefully. Below is an example of the output on your terminal using the above command.

Changing password for user marquez-osardad
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Add an Shell to the new user

When you add a user without a shell it will take the default, but you have to define it. Similarly, the default shell for the user-created with the useradd command is sh. However, currently, sh is not used when there is bash and zsh.

On the other hand, the user can change his or her default shell. Although they can create a user with a different shell than the default one, using the -s

Then, if the created one so wishes the new user to use bash as the default shell. Then you must execute the following command:

sudo useradd -s /bin/bash marquez-osradar

Conclusion

The creation of user accounts in Linux is used to obtain more privacy. In case more than one person is using the same system on the computer. Besides that Linux is a multi-user system, more than one can be using it at the same time.

Similarly, with the useradd command, it allows us to create user accounts. This means that they can monitor the control that this account has within the system. Likewise, the account created, can have an expiration date, change the directory, as to assign it to a group.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article