Linux

How to Change Owners of a File in Linux with the Chown Command

Introduction

The chown command stands for "change owner" and is a handy command in Linux for changing the user or group that owns the file. If you're like me, you find Linux man pages extremely difficult to follow. This guide serves as a substitute for the MAN page for the chown command. We will cover why we use the chown command, the syntax for the CHOWN command, and a practical example of how the chown command might be used. By the end of this tutorial, you will not only have a basic understanding of the chown command, but you will be confident using its features and functionalities at the command line.

Why Do We Use The Chown Command?

In order to control who has ownership, access, and restrictions for certain files, Linux employs users and groups for two main purposes: organization and security. Throughout the lifetime of a machine, many different users and groups of users will be creating, reading, updating, and deleting files. Additionally, large organizations can have many employees with many files per employee. Organizing who owns each file is made much simpler with users and groups. Additionally, an organization could have confidential information that is necessary for some employees to access but a risk for other employees to access, which warrants the need for managing the permissions and ownership of these sensitive files.

Practical Example

Let's say a school has a Linux system that has both student data and financial data. We want to ensure the accounting group owns certain files and that Mrs.Johnson, the 9th-grade algebra teacher, owns the file for that class. First, let's create a directory called school and navigate to it:

mkdir school && cd school
Now, let's create two files, receivables.txt, and algebra101.txt, and inspect their permissions:
touch receivables.txt algebra101.txt
ls -l
>>> -rw-r--r-- 1 root root 0 Jan  6 23:15 algebra101.txt
>>> -rw-r--r-- 1 root root 0 Jan  6 22:50 receivables.txt

Since we are currently using the root user on our Linux machine, the root user and group are assigned to the file. Let's improve security and organization by separating the owners of these two files. First, let's create a user named johnson and assign that user as the owner of algebra101.txt using the chown command:

useradd johnson
chown johnson: algebra101.txt
ls -l
>>> -rw-r--r-- 1 johnson johnson 0 Jan  6 23:15 algebra101.txt
>>> -rw-r--r-- 1 root root 0 Jan  6 22:50 receivables.txt

A few things to note about the snippet above. First, the syntax of the chown command is as follows: chown <user name>:<group name>. If the group is not specified, then Linux will change the group owner of the file to the default group associated with the newly specified user. When we create a user with our useradd command, it also creates a default group with the same name as the user and assigns it to the newly created user.

Finally, let's create our accounting group and assign it to our receivables.txt file:

groupadd accounting
chown :accounting receivables.txt
ls -l
>>> -rw-r--r-- 1 johnson johnson 0 Jan  6 23:15 algebra101.txt
>>> -rw-r--r-- 1 root accounting 0 Jan  6 22:50 receivables.txt

Unlike modifying the user, changing the group will have no effect on the file's owner.

Conclusion

With your newly equipped skills of the chown command, you will henceforth keep your Linux system more secure and more organized. Maintaining integrity over who owns all of your files is a necessary skill for mastering Linux. In this tutorial, you gained mastery over changing the user and group owner of the file through a practical example - managing a school system - giving you the confidence to tackle any issue with file ownership that comes your way.

Previous
chmod
Next
lsof