How to Use the Chown Command in Linux to Change File Ownership

Chown Command Linux Featured

On Linux systems, each file is associated with an owner and group owner. When you don’t have the appropriate permission, you won’t be able to access or edit the files or directory. On a Linux system, there is a “change owner” (chown) tool that allows you to change the owner of a file/directory as well as the group owner. Let’s see how you can use the chown command in Linux to better manage your files and folders.

Also read: How to Use Access Control Lists to Control File Permissions on Linux

How to Use the chown Command in Linux

The command can be used according to the following syntax:

sudo chown [new_owner]:[new_group_owner] filename

Check the Current Permissions of a File

Before changing the owner (or the group owner) of a file, you should first list the current permissions of a file by using the ls -l. With this command, you will be able to see the owner and the group owner of the files you intend to operate on.

Chown Group Change 1

Changing Only the Owner of a File/Directory

It’s possible to change only the current owner of a file on the Linux system. As in the syntax of the command, you have something like two columns separated by the :. The first column stands for the new owner, while the second column stands for the new group owner. Also, to change the owner of the file, you need superuser permission, which means prepending sudo to all chown commands.

sudo chown maketech: docker-machine

Check the result of the command with ls -l:

$ ls -l
total 1964
-rw-r--r--  1 userkubetrain user_kubetrain    2148 Mar 18  2019  certnew.cer
-rw-r--r--  1 maketech      user_kubetrain      48 Jan 29 10:13  docker-machine

You can see that “maketech” is now the new owner of our file. By default, the command only works for a single file or directory. If you want to apply the changes to all the files and subdirectories within a folder, you have to use the -R parameter:

sudo chown -R maketech virtual-machine

You can also change the owner of a file by using the UID of the user:

sudo chown -R 1002 virtual-machine

Change Only the Group Owner of a File or Directory

If you want to give other users permission to access the file, you can place them in a group, then change the group ownership of the file.

sudo chown :maketechpublic -R docker-machine

Check the result.

Chown Group Change 2

As you can see, we put the colon : before the name of new group owner just to indicate that this is the group name and not the name of the new user.

Change the User and Group of a File

If you want to change the owner and the group owner of a file or directory, you will need to indicate the two values:

sudo chown maketech:maketechpublic -R docker-machine

By doing this, you are assigning the new owner of the directory and also the group (meaning the users of the group) that can also operate.

The chown command in Linux is responsible for changing the file’s user and/or group ownership. It is often used together with the chmod command to better manage the permission of files in Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Alain Francois

A Linux system administrator passionate about Open-Source environments and working on installations, deployments for different IT solutions and cloud environments. I like to share my knowledge regarding the technologies that I can discover and use