Change Owner Of Home Directory In Linux

How To Change Owner Of Home Directory In Linux With Extra Tip

How To Change Owner Of Home Directory In Linux

How To Change Owner Of Home Directory In Linux With Extra Tip

In this article, you will learn how to change ownership of the directory in the correct way. Changin directory required in many cases but here we have covered where every Linux admin and user stuck in daily task. These cases are enough to understand the changing ownership of the directory.

Environment

  • RHEL 6
  • RHEL 7
  • RHEL 8
  • All Linux flavors

Issue

  • Change Owner of Home Directory

Command 

chown -R user:group directoryname

Setup an environment:

We will add two test users abc and xyz for our environment to understand how to change ownership of home directory.

 
Let’s add two users abc and xyz in our case to test this :

[root@explinux home]# useradd abc
[root@explinux home]# passwd abc
Changing password for user abc.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@explinux home]# ls
abc
[root@explinux home]# ll
total 0
drwx------ 2 abc abc 83 Apr 11 07:28 abc
[root@explinux home]# useradd xyz
[root@explinux home]# passwd xyz
Changing password for user xyz.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@explinux home]# ls
abc  xyz
[root@explinux home]# ll
total 0
drwx------ 2 abc abc 83 Apr 11 07:28 abc
drwx------ 2 xyz xyz 62 Apr 11 07:29 xyz
[root@explinux home]#

We have added two users and now both have the same name user and group ownership.
Group Owner linux
Now if you login with any user you will redirect to his home directory. You can see in the below image.
Login to home Directory
As you can see on the above image after login both is redirected to their home directory.

Case 1:

Change Complete Ownership of Directory

ISSUE:

Could not chdir to home directory /home/abc: Permission denied
-bash: /home/abc/.bash_profile: Permission denied
The user has been removed by mistake and created a new user with the same name. Linux understands the UID and GID of the user so when you try to log in with the new user it will show you an error and will not redirect to your home directory.
UID and GID of Users
In our case, we will remove the user by the below command and leave the home directory:
# userdel abc
userdel
Above the image, you can see no user in passwd, and in the home directory, you can see UID and GID or user which was owned by the user abc.
Now if anyone created the user with the same name.
# useradd abc

In this case, the user will create with new UID and GID, no Home directory will create for new user because the same name home directory already presented in the home directory. You will get warning also at the time of local user creation. In the case of AD and LDAP, you will not get any prior error messages.

UID and GID of New User
In the case of the local users, you can change GID and UID also but for AD and LDAP. You need to change ownership of home directory 
You will get errors in login and variables and the environment you set for the bash profile also not work in this case. Even you will not redirect to the user home directory.
Issue in login linux local user

Resolution

Change the ownership of the user home directory to fix the issue:
# chown -R abc:abc abc
How to change owner of home directory


Now your login issue has also been resolved. You will get no error login.

Case 2:

Change User Ownership of Directory

You have created a service user and you want this user to have any specific home directory user ownership. We will do this in case of the user who is nologin or any application we deploy with nologin user and want to give all access to service or generic account to have control over the application.
Change the only UID of the home directory
Note: This will again show the same error message for the user like case 1
# chown -R xyz abc
change home directory user ownership

Case 3:

Change Group Ownership of Directory

This case also similar to case 2 but here we will not get an error message. In this case, we will change the group ownership of the directory.
This will change the only group ownership of any directory. This is a correct method also because you have the control over users also to add them or remove them from a specific group. No effect on user account and application after configuration
#  chown -R :xyz abc
Now you have successfully resolved your ownership issue. This is not only for the home directory. This will work on any directory so be careful about it.
Note: Do not run the below command
# chown -R abc:abc /
This will corrupt your system and after reboot, it is very hard to recover the system, Because mostly directories required only specific ownership and this will affect your system

Extra Tip:

Change home directory of the user

If you required to change any user’s home directory if they login. In case of any different name where the same name is not possible or for any security reason. You can change the home directory also.
For this, you have to follow the same procedure to change directory ownership. But you will face issues like case1 because in the passwd section home directory defined is as /home/user.
So you need to change the user home directory also.
# chown -R xyz:xyz abc
# usermod -d /home/xyz abc
change user directory linux

Now at this point, you become perfect in changing ownership of the directory without any error. Share this article to show your care.

Leave a Comment

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