useradd command to create user

The use of useradd command in Linux to create users.

Hello, all penguins in this tutorial we understand how to use Linux useradd command to create or manipulate users in Linux.

However, with useradd command, you can add more types of security when you create a new user.

You can do all this work graphically. Similarly, as you do on windows but if you work on Linux servers there is no graphical interface. A good or I say the best Linux administrator is do all his work on command-line interface. So that’s why the Linuxcan team suggests you do your all work in the terminal.

useradd command to create users
useradd command to create users

Let’s start useradd command to create users

To create new users, all you have to do run useradd command followed by given a username.

wolverine@xmen:~$ useradd linuxcan

There are some important facts you should know to add new user in Linux. Firstly to adduser into your Linux you want superuser privileges. secondly, you cant add users with the same names, Each user has a different username. On the above example, I add a linuxcan user in my machine.

In the Microsoft Windows environment if you create a user you can log in with that user without a password. But in the Linux type Operating system once you create a new user. The user is disabled by Linux nature. So you have to set a password to enable that user.

How to set password for Linux user

To set a password for users use passwd command. To use passwd command type passwd followed by user name.

wolverine@xmen:~$ Sudo passwd linuxcan
password for wolverine:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
wolverine@xmen:~$

So above output, I enable linuxcan user to give a password for that user. To give the password you also want superuser privilege. I run the passwd command with Sudo followed by username.

Users home directory in Different Location.

By default, Linux creates every user home directory in /home directory. And in /home directory you can find users’ home directory with user name.

Likewise, before we create user linuxcan, So its default home directory location is /home/linuxcan.

But you can change this default location by using d option with the command, Followed by the location of the users’ home directory.

wolverine@xmen:~$ useradd -d /new_users/account linuxpro

SO above command give linuxpro user /new_user/account as his home directory

you can check this information in /etc/passwd file.

wolverine@xmen:/home$ cat /etc/passwd | tail -1
linuxpro:x:1002:1002::/new_users/account/:/bin/sh

However, now the question is what is the passwd file. whenever we create new users in Linux it adds users’ entry to some files. So the passwd file is one of them.

However, there are some more file which Linux add entry on them. Those files are /etc/passwd file contain full user information. /etc/shadow file contains users’ password in encrypted form. /etc/group file has a group name and its group id.

linuxpro:x:1002:1002::/new_users/account/:/bin/sh

Linux passwd file.

So in the above output, you can see six fields separated with colon each filed has some meaning. Let’s explore them

The first linuxpro is a username into the system. The maximum character you can choose for the username is 32 characters. On Debian, the only constraints are that usernames must neither start with a dash (‘-‘) nor plus (‘+’) nor tilde (‘~’) nor contain a colon (‘:’), a comma (‘,’), or a whitespace (space: ‘ ‘, end of line: ‘\n’,tabulation: ‘\t’, etc.). Note that using a slash (‘/’) may break the default algorithm for the definition of the user’s home directory.

Second x is for password and this file does not show your password because the password is stored in /etc/shadow file in encrypted form.

Third 1002 stands for user-id (UID) Linux gives every user a unique User ID Number. For users, UID Number starts with 1000. Root user always has number Zero(0) for UID. And number 1-999 is used by Linux to its own predefined account, Which YOu can See on the Passwd file.

Fourth 1002 stand for user Group Id(GID) Number. you can see all GID is in /etc/group file.

Fifth Field is a user default home directory. which we can change with the d option.

Sixth files id for uses default shell and shell location.

Add user without home directory

To create users without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘linuxpro‘ without a home directory.

Add user account with Expiry Date

When we create a new user account with useradd command so by default the user account will never expire.

However, you can change this default behavior with e option. This option is very useful for a Linux administrator, to assume you have multiple users in server and from time to time you have to disable some account when the user is done that work. So you have to do this work manually. But you can handle this work to Linux.

The command is to create a user with expiry date is and date format is YYYY-MM-DD.

wolverine@xmen:~$ Sudo useradd -e 2020-05-12 Shawn

For example, I create a Shawn account that will expire on the 12th of May 2020 after this date user cant log in with this user. We can check that information with the chage command.

Last password change : Jan 17, 2020
Password expires : never
Password inactive : never
Account expires: May 12, 2020
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

On the above output, we can see the account expires on 12th May 2020.

Add user account with Password Expire date

Similarly, account expiry time you can add password expire date with useradd command. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. before this expiry date if the user did not change their password so so their account will be disabled. This useradd command option is useful to forcefully change the password fo user account in a certain time of period.

For instance, we create user paul and set its password to expire policy 15 days

wolverine@xmen:~$ useradd -e 2020-05-12 -f 15 king

Add a User with Custom Comments

The comment option gives you to add users more information like its last name its phone number or anything you want to add about that user.

for instance, we create user adam with the custom comment on its profile in the company which is a support engineer.

wolverine@xmen:~$ useradd -c “support engineer” adam

Now we can check that custom comment in /etc/passwd file.

wolverine@xmen:~$cat /etc/passwd | tail -1
adam:x:1008:1008:support engineer:/home/adam:/bin/sh

Add user with different shell

In Linux, there are different types of shell are available. For example, sh shell, bash shell, c shell, and many more. You can give every user to its preferable shell. You can add s option to give different shells for different users. For instance, we create user ricky and give a rick bash shell. In my previous output, the useradd command gives sh shell by default. We can change this default behavior with s option.

wolverine@xmen:~$ sudo useradd -s /bin/bash ricky

Now we create the user and give that user bash shell now we check in /etc/passwd file.

wolverine@xmen:~$cat /etc/passwd |tail -1
ricky:x:1009:1009::/home/ricky:/bin/bash

Add a User to Multiple Groups

we can add a user with multiple groups with G option. Each group name is separated by a comma, with no intervening spaces. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.

Create a User with Specific User ID

The numerical value of the user’s ID. This value must be unique unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.

Some key things to remember

You may not add a user to a NIS or LDAP group. This must be performed on the corresponding server.

Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation request.

It is usually recommended to only use usernames that begin with a lower case letter or an underscore, followed by lower case letters, digits, underscores, or dashes. They can end with a dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]?

/etc/default/useradd file has all default configuration when you create new user in this file you can see default setting.

Conclusion

In this article, we learn the useradd command to create users and their different options. Like how to change its home directory, how to set account expiry date and how to give different shells to users.

I hope you enjoy my article. please subscribe to our newsletter to read my articles and please share with all your penguins’ friends to grow. To Improve this article if you find anything incorrect so please comment below and I will update. to check out my home page click HERE.

reference: Wikipedia 

Leave a Comment

Your email address will not be published. Required fields are marked *