Administrators on Linux have to be very careful when it comes to user management for security reasons. They have to assign different levels of Sudo privileges to other users. Sometimes, they may even have to create users without a home directory. Those users can have a home directory later in CentOS 8.

This post contains a detailed explanation of how to create a user with or without a home directory. Later, we will also learn how to create a home directory for a user which already exists. So let’s begin.

To run the commands mentioned in this article, you will have to have Sudo privileges or log in as a root user.

Login as a root user

You can log in as a root user by running the following command:

You will be asked to authenticate yourself with the root password.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-10.png" height="79" src="data:image/svg xml,” width=”252″>

There can be two ways to add/create a user in CentOS or any Linux based Operating system:

  • By using adduser
  • By using useradd

Add User using the “adduser” command

The first method you can use to create a user uses the “adduser” command followed by the user name. This method automatically creates a default directory usually located at /home directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image5-10.png" data-lazy- height="49" src="data:image/svg xml,” width=”380″>

Remember to replace the “username” with the intended user name.

Add User using the “useradd” command

A new user can be created with the “useradd” command.

By default, the “useradd” command creates a home directory for the user.

However, by running the command mentioned above along with -M or –no-create-home, you will make a user who doesn’t have a home directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-10.png" data-lazy- height="41" src="data:image/svg xml,” width=”444″>

# sudo useradd –no-create-home <username>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image7-9.png" data-lazy- height="49" src="data:image/svg xml,” width=”581″>

If you try to log in using the “su” command, you will be notified that the directory cannot change to /home/username as no such file or directory exists.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image6-9.png" data-lazy- height="90" src="data:image/svg xml,” width=”622″>

Create a Home directory for a user on CentOS 8

Now that we have learned how to create a user without a home directory, the next step is to create a home directory for an already existing user. You can do that by using the mkhomedir_helper command.

# mkhomedir_helper <username>

After using the command given above, verify whether the user has a home directory or not.

To verify, log in as a root user, using the “su” command as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-10.png" data-lazy- height="66" src="data:image/svg xml,” width=”499″>

As you can see in the screenshot above, the command line has not given an error message like before. This signifies, the user now has a home directory.

You also have the option to create a new user with a custom directory. Use the user add command along with -m and -d flags to create a custom directory.

# useradd -m -d /testdir username

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-10.png" data-lazy- height="69" src="data:image/svg xml,” width=”546″>

In the example given above, the command has created a user with a directory /testdir.

Conclusion

In this post, we have walked through a profound guide on how to create a user using two commands(“adduser” and “useradd”). We have also learned to create a new user without a home directory, and afterward, we have learned how to create a home directory for that user.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/7011409B3A384F43A8417D1DAC68D179-150×150.jpg60eb769c2a0f3.jpg" height="112" src="data:image/svg xml,” width=”112″>

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.