How to Create Home Directory for a User on CentOS 8

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:

$ su

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

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:

# adduser username

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.

$ sudo useradd username

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.

# useradd -M <username>

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

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.

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:


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


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.



from Linux Hint https://ift.tt/2VvUBY2

Post a Comment

0 Comments