Linux treats everything as a file. The chown command helps with changing the ownership of a file, directory, or link. The ownership involves the user and the group.
Whether you use a Linux or UNIX-like system, this post shares detailed insights about the chown command to ensure you understand how to use it. Moreover, we will go through practical examples of using it. Read on!
Understanding the chown Command Syntax
The chown command adheres to the below syntax.
$ chown [options] user[:group] file(s)
- The options are the different flags you can use with the command for specific results.
- The user represents the new owner of the file. You can express the new owner by adding their username or user ID.
- The group represents the new group ownership of the target file. You can choose to omit it and only change the user.
- The file(s) are the target files for which you want to change their ownership.
Note: The chown command requires superuser permissions to execute. Therefore, you must add sudo to execute it unless you are a superuser.
Before we check examples of the command, let’s first understand how to check the existing file ownership. Run the below command.
For our case, we’ve used the ls command to show the file ownership of all text files.
$ ls -l *.txt
For instance, the ‘file1.txt’ has its user as ‘linuxgeekery’ and its group as ‘linuxgeekery.’
With this understanding, we can dive deeper into using the command.
Examples of Using the chown Command
The first step to changing file ownership is understanding its current owners. We’ve already seen how to use the ls command for that. This section shows different ways and options that you can use to change a file’s ownership.
1. chown Command Change a File’s Owner
To change the owner of a file without changing the group ownership, add the new user and the target file as in the below example.
$ sudo chown root file1.txt
When we run the ls command, we verify that our target file has root as its new user.
2. Changing the Owner Using the User ID
When changing the user of the file, you can use the user ID instead of the username. First, check the user ID. We want to set ‘root’ as the new user for our case.
$ id -u root
After getting the user ID, you can run the chown command and specify the user ID as the new owner of the file.
Here’s an example.
$ sudo chown 0 demo.txt
3. Changing Ownership of Multiple Files
It’s possible to set new ownership of multiple files or directories simultaneously. For that, add their file names when running the command.
For this example, we’ve set the new user using the user ID.
$ sudo chown 1000 hosts.txt names.txt
When we run the ls command, we verify that we managed to set the new ownership for the files.
4. Changing a File’s Group
Unlike when changing the user, you must add a colon before the group before adding the target file. For instance, in the below output, the ‘names.txt’ has root as its group. To change the group and set it to group ‘users,’ run the below command.
$ sudo chown :users names.txt
5. chown Command Change Owner and Group
Suppose you have a case where you want to change the user and group of a file or directory. Add the new user and the group when running the command. As in the example below, ensure you add a colon before the group.
$ sudo chown root:users test
From the output of the ls command, we’ve verified that our file has a new user and group ownership.
6. Working with Links
Even when you have a symbolic link, it’s possible to change its ownership. However, the chown command will only change the ownership of the reference file and not the symbolic link.
If your focus is to change ownership of the symbolic link, add the -h option to override the default behavior.
Let’s first use the Linux touch command to create a file for demonstration.
$ touch linked.txt
Next, create the symbolic link using the Linux ln command and verify the file’s user and group using the ls command.
$ ln -s linked.txt ref1.txt
The above output shows that the reference file and its symbolic link share the same ownership. To change the user and group, run the below command.
$ sudo chown -h root:users ref1.txt
When you rerun the ls command, you will notice that the ownership of the symbolic link has been successfully changed.
7. Matching the Ownership of a File to Match Another File
Sometimes, you can have a case where you want two files to share the same ownership. In that case, you can change ownership of a file to match that of a reference file.
Taking the two files below, we can see they have different ownership.
Suppose we want the ownership of the ‘linked.txt’ to match that of the ‘names.txt.’ We would run the command below.
$ sudo chown --reference=names.txt linked.txt
Your files will now have the same ownership.
8. Working with a Directory
When you have a directory, it’s possible to change the ownership of the directory and its contents recursively.
We’ve created a directory using the mkdir command and added a text file using the touch command.
The below output shows that the directory and its contents have the same user and group ownership.
If we run the chown command, it will only change the ownership of the input directory, which could be the parent directory. However, adding the -R option will change ownership of the directory and its contents.
We’ve also included the -v option to enable the verbose mode.
$ sudo chown -vR root:users Users/
That’s how you change a file’s ownership using the chown command.
Conclusion
You can easily change the ownership of a file using the Linux chown command. This post discusses the command in detail and gives practical examples, ensuring you get comfortable using the command. You can now use the command to change the user or group of a file.