How to Use the chown Command on Linux

A terminal window on a Linux systemFatmawati Achmad Zaenuri / Shutterstock.com

The files and directories of Linux systems all belong to someone. You can change their owner with the chown command. We show you how.

Each file belongs to a user and a group

Linux is a multi-user system. The operating system allows multiple user accounts to be defined and any valid user to log on to the computer. In addition, multiple users can use a single computer at the same time.

To maintain a record of the files belonging to which user and to enhance security, Linux uses the concept of ownership. Each file belongs to an owner – a user – and to a group.

When a file is created, its owner is the user who created it. The group to which the file belongs (the "owner" group) is the current group of the user. Users and groups have names, as well as numeric identities, called User ID (or Unique ID) (UID) and Group ID (GID).

When you create a file, it belongs to you and belongs to your current group. Usually, it is the group to which you have connected. By default, this is a group that shares the same name as your user name and that was created during your creation as a user on the system.

You can use the chown command to change the property values. You can define a new owner, a new group or a new owner and a new group at the same time. The owner of a file can change the property of the group, but only root can change the property of the user because it involves another user. Without root privileges, you may not have another system user "adopt" a file.

Why would you want to change ownership?

Here are some examples of situations where you may want to do this:

If you are transferring files between different Linux or Unix operating systems, you will need to replace the owners of users and groups with the new owners of users and groups of the account under which you want to use them. files on the new Linux computer.
A user can leave your organization and all his files will be under the responsibility of another staff member. You will need to replace the owner and owner of the group with the staff member who is now responsible for these files.
You can create a script that will be used by a specific user.
You can create a file or directory connected as root, but you want it to be accessible to a specific user.

Viewing your groups, UID and GID

AT list groups you are, you can use the groups command.

groups

groups in a terminal window

For a list of groups, their numeric identifiers, and your UID and GID, use the id command:

login

id in a terminal window

You can use some options with ID to narrow the output.

-u: List your UID.
-g: List your effective GID (current).
-nu: List your user name.
-ng: List the name of your current group.
id -u
id -g
id -nu
id -ng

Exiting the id command in a terminal window

Viewing the property of a user on a file

To see the owners of a file or directory, use the -l option (long list) with ls.

ls -l
ls -l in a terminal window

We can see that the name dave appears twice in the list. The leftmost aspect tells us that the owner of the file is a user called dave. The rightmost dave tells us that the file belongs to a group that also calls dave.

By default, when a Linux user is created, he is added to a private group named for his user name. They are the only member of this group.

This executable file belongs to the user mary and the group to which the file belongs is mary's private group.

ls -l

ls -l in a terminal window

This file belongs to the oscar user, but the group it belongs to is called researchlab. This means that other members of the researchlab group can access this file, based on the file permissions set for members of that group.

ls -l in a terminal window

Change the property of the user

Let's see some examples. This command will change the ownership of the file while.c by the user.

sudo chown mary while.c

sudo chown mary while.c in a terminal window

We can use ls to see the changes made to the properties of the file.

ls -l while.c

ls -l while.c in a terminal window

You can use chown to change the ownership of multiple files at once.

sudo chown mary getval.c global.c goto.c

sudo mary chown getval.c global.c goto.c in a terminal window

This changes the ownership of the user on the three files.

ls -l getval.c global.c goto.c

ls -l getval.c global.c goto.c in a terminal window

You can use wildcards to select groups of files. This command will change the user property on all files starting with the letter "c".

sudo chown mary c *. *

sudo chown mary c *. * in a terminal window

All files will now have Mary as the owner. Note that none of the properties of the group have been modified.

ls -l mary c *. *

ls -l mary c *. * in a terminal window

Let's change the property of a directory. We simply pass the directory name to chown instead of a file name.

sudo chown mary ./archive/

sudo chown mary ./archive/ in a terminal window

To check the property properties of the directory, we use ls, but we also use the -d (directory) option. This lists the directory properties, not the files that it contains.

ls -l -d ./archive/

ls -l -d ./archive/ in a terminal window

To change the ownership of all files in a directory, you can use the -R (recursive) option. This option will change the ownership of the user on all files in the archive folder.

sudo chown -R mary ./archive/

sudo chown -R mary ./archive/ in a terminal window

Now let's look at the files in the archive directory.

ls -l ./archive/

ls -l ./archive/ in a terminal window

As can be expected, all files now belong to Mary.

Change group owner

There are different ways to change the ownership of the group.

To change the group property at the same time as you change the user property, pass the new owner name and the new group name with a colon ":" separating them. The group must already exist.

sudo chown mary: researchlab charm.c

sudo chown mary: researchlab charm.c in a terminal window

The owner of the user and the group to which the file belongs have both been modified.

ls -l charm.c

ls -l charm.c in a terminal window

An abbreviated way to change the ownership of the group to the current group of the new owner, just provide the two points and omit the name of the group.

sudo chown mary: caps.c

sudo chown mary: caps.c in a terminal window

ls -l caps.c

ls -l caps.c in a terminal window

The ownership of the user and the property of the group have been changed to mary.

To change only the property of the group, precede it with two points and omit the user name. The owner of the user will not be modified.

sudo chown: researchlab at.c

sudo chown: researchlab at.c in a terminal window

ls -l at.c

ls -l at.c in a terminal window

The property of the group has been changed, but the ownership of the user remains the same.

Using Chown with UID and GID values

You can use UID and GID numeric values ​​with the chown command. This command will set the user and group property on mary.

sudo chown 1001: 1001 at.c

sudo chown 1001: 1001 at.c in a terminal window

ls -l at.c

ls -l at.c in a terminal window

Possession, that is nine tenths of the law

Or they say. But under Linux, ownership is an important part of file security, with file permissions providing the rest. Use the chown and chmod commands to secure access to files on your system.

Advertisements

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.