When it comes to Linux permissions, things can get pretty tricky. Unless you are an admin user, like root, then you won’t have access to files or directories that were created by other users. In order to access those files, either their permissions should be updated to be available for other users or we can change ownership of the files.

In this tutorial, we will discuss what to do if you are required to change ownerships of files or directories. We would be discussing how to change ownership of files & directories using the ‘chown command’.

The syntax for using the chown command,

# chown user:group file/directory

Let’s discuss some examples of how to use the chown command in Linux for changing ownership of files.

Recommended Read: How to rename user in Linux (also rename group & home directory)

Also Read: Is there a way to change Jenkins Home Directory ?


Examples for Chown command in Linux

 

1- Change both user & group ownership for a file or directory

If you are required to change both user and group ownership for a file, execute the following command,

# chown shusain:admin file1

The same command will be used to change the ownership for a directory as well, just replace file address with directory address, like

# chown shusain:admin folder1/

Here, we are assigning ‘shusain’ as the new owner of the file and the group ‘admin’ as the owner group

2- Change ownership for all files and directory

If required to change all the permissions for files or directories insider another directory, then we need to recursively change the ownership using the flag ‘-R’,

# chown -Rv shusain:admin /home/test/

This will change the ownership of all files or directories at the path ‘/home/test/’. Also, the flag ‘v’ will show the output of the command as it progresses.

3- Change only user ownership for a file or directory

If required only to update the user ownership, then the following command can be used,

# chown shusain: file1

Remember the ‘:’ after the new owner’s name.

4- Change only group ownership for a file or directory

To only change the group ownership & not the user, execute the following command,

# chown :admin file1

5- Check a file as a reference for changing ownership

With the chown command, we can also take the user & group’s ownership of a file or directory as a reference, and then can assign that same ownership to another file in a single command,

# chown --reference=old_file new_file

With this command, it will check the user and group ownership of old_file and then will assign that same ownership to new_file.

6- Check if the file belongs to a particular user/group & change ownership if it does

Another great example that you will use more than you can imagine is changing ownership of files or directories only when they belong to a particular user/group.

The use case scenario for this can be that a user has left your organization & he might hundreds or thousands of files that are to be assigned to the new user. So how do you do it?

For this case scenario, we would use the following command,

# chown --from=shusain:admin new_user:new:group /home/shusain

Now, this command will check for all the files with ‘shusain:admin’ & change ownerships of all the files and directories under path ‘/home/shusain’ to ‘new_user:new_user’. If ownership is different than what has been mentioned, then it will not update anything.

Important Note:- If you are trying to change the ownership of files or directories with symbolic links, then there will be no change to the file with symbolic links, only the original file’s or directory’s permissions will be updated.

These were only some of the examples to use the chown command to update the ownership of files and directories. I am sure with these examples you can figure out a solution to your use case & if you face any issue, you are welcome to use the comment box below to contact us.

We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,

DigitalOcean - 100$ free credit & Linode - 100$ free credit

Check some Exclusive Deals, HERE.

Also, check out DevOps Book You should read section.