Chown Command In Linux (File Ownership)
-
The chown command in Linux changes user ownership of a file, directory, or link(soft link or hard link) in Linux.
-
Every file is connected to an owner in Linux. An owner can be a user or a group.
-
The user who creates the file in Linux is the owner of the file.
-
But you can change the owner of the file in Linux by using the chown command in Linux.
A user having root or sudo rights is required to alter the ownership of any file or directory. Or, you must be the owner of the file/directory to change its ownership.
Remember to run the chown
commands with sudo
as a prefix to execute them with the root user.
Linux chown Command Syntax
The basic chown
command syntax consists of a few things.
chown [OPTIONS] USER[:GROUP] FILE(s)
-
[OPTIONS] - the command can be used with or without additional options.
-
[USER] - the username or the numeric user ID of the new owner.
-
[:] - use the colon when changing a group of a file.
-
[GROUP] - altering the group ownership of a file. This is optional.
-
FILE - the target files or directories.
Superuser rights are essential to perform the chown command. Although the owner of the file can also change the ownership of the file/directory.
NOTE: We tested the command examples using the chown version 8.32 in Ubuntu 22.04 LTS, Ubuntu 20.04 LTS, and macOS.
To verify the chown installation or version on your system, run the following command:
chown --version
chown (GNU coreutils) 8.32
In macOS the above --version command will not work.
chown Command Options/Flags
Following are the options or flags that you can use with the chown command in Linux.
Flag/Options |
Description |
-f |
If you use this flag, you will not see any error messages after running the chown command. |
-h |
If you want to change the ownership of a symbolic link, but not for the file or directory pointed by the symbolic link. |
-H |
When you use the -H flag with -R, then if a symbolic link is found for some directory, the ownership of the symbolic link is changed but the chown command will not enter the directory and leave the files and subdirectories as it is. The chown command will not change the user for them. |
-L |
When you use the -L flag with -R, then if a symbolic link is found for some directory, the ownership of the symbolic link and all the files inside the directory with the symbolic link also gets updated. |
-P |
When you use the -P flag with -R, then while changing ownership of all the files recursively, the ownership of the symbolic link will also be changed. |
-R |
You can use this flag to recursively change the ownership of files inside directories. For example, if you have a directory and you want to change ownership of that directory along with all the files/sub-directories inside that directory then you will have to use the -R flag. |
You can use the flags alone or in pairs while running the chown command depending upon your requirements.
For example,
chown -HR user2 sample
Let's see some more examples of the chown command. You should try all the commands with different flags for practice and see how the chown command in linux works.
Using the chown Command
Now let's see the different use cases of the chown command along with examples that you can use directly to run the chown command and get the desired results.
1. How to Check Ownership of a File in Linux?
First, you need to identify the original file owner or group before making ownership changes using the chown command.
To check the current group or user ownership of Linux files and directories in the current directory(location), execute the following ls command:
ls -l
An example output of this command looks like this:
total 0
-rw------- 1 user1 studytonight 0 Jul 3 08:42 error.log
-rw-rw-r-- 1 user1 studytonight 0 Jul 3 08:43 file1
-rw-r--r-- 1 root root 0 Jul 3 08:13 file2
2. How to Change the Owner of a File?
Changing the owner of a file using the chown command requires you to provide the new owner and the file name.
The format of the command is:
chown NewUser FILE
The following command transfers the ownership of a file sample.txt from user1
(or any existing owner) to the user user2
:
chown user2 sample.txt
If you see a blank output, that means the command is executed successfully and ownership has been transferred.
You can use the same syntax to change the ownership for both files and directories.
3. Change the Owner of a File With UID
Instead of a username, you may give a user ID to modify the ownership of a file.
For example:
chown 1002 sample.txt
Make sure there is no user with the same name as the numeric UID. If there is, the chown command gives precedence to the username, not the UID.
4. How to Change the Group of a File
With the chown command, you can change the group for a file or directory without affecting the owning user. The effect is the same as using the chgrp
command.
Run the chown command, specify a new group with a colon (:
) and the group name:
chown :NewGroup FILE
The following example changes the group of the file sample.txt from grouptest to group3.
chown :group3 sample.txt
You can also verify the ownership change with ls
command:
ls -l
5. Change the Group of a File Using GID
Similar to UID, use a group ID (GID) instead of a group name to alter the group of a file.
For example:
chown :1003 sample.txt
6. Change the Owner and the Group
To assign a new owner of a file and change its group at the same time, execute the chown
command in this format:
chown NewUser:NewGroup FILE
Therefore, to establish linuxuser
as the new owner and group3
as the new group of the file sample.txt:
chown linuxuser:group3 sample.txt
Remember that there are no white spaces before or after the colon.
7. Change Ownership of Multiple Linux Files
-
List the target file names after the new username, to change the ownership for multiple files in one go.
-
Use single spaces between the file names.
In the following example, root
will be the new owner of the files sample1 and sample2.
chown root sample1 sample2
You can also combine file names and directory names to change their ownership with one single command. For example:
chown root sample3 Dir1
Do not forget that the file and directory names are case-sensitive.
Advanced chown command option usage
The chown tool has plenty of options to change the behavior of the command, which allows us to perform tasks for specific use cases.
Let's dive into a few of them.
1. Change Group to a User's Login Group
The chown command sets the owner's login group to the file (by default) when no group is supplied.
To do so, specify a new user followed by a colon, space, and the destination file:
chown NewUser: FILE
For example, to change the owner group to the user linuxuser
login group, use the following format.
chown linuxuser: sample.txt
where sample.txt is the file name.
2. Copy Ownership and Group Settings from One File to Another
Rather than setting the ownership to a particular user directly, you can use the chown command to provide a reference to some other file. But what does that mean? Well, when we provide a reference to some other file, we are informing the chown command that we want our file to have the same user and group, just like the reference file.
Add the --reference
argument to the chown command to replicate the settings (user and group) from one file to another:
chown --reference=ReferenceFILE TargetFILE
For example, if you want the new.txt file to have the same user and group as the old.txt file, then you can run the following command:
chown --reference=old.txt new.txt
3. Check Owner and Group before making changes
The --from
option of the chown command helps you validate the existing owner and group and then make modifications.
The chown syntax for verifying both the user and group look like this:
chown --from=CurrentUser:CurrentGroup NewUser:NewGroup FILE
The above command will first check that the current user and group are CurrentUser and CurrentGroup and then only it will change the ownership of the file.
In the command below, we first checked the ownership and the group of the file sample.txt, and then updated it:
chown --from=olduser:oldgroup newuser:newgroup sample.txt
The chown command will first check if the owner and group of file sample.txt are olduser and oldgroup, if Yes, then it will update the user and the group.
FAQs
You may have some questions related to the chown command. Here are some relevant ones:
Q. What is ownership in Linux?
It is a mechanism that determines who can access and modify files and directories on a Linux system.
Q. How can I change the ownership of all files and subdirectories in a directory?
You can use the -R
option, which makes the command run recursively and it changes ownership of all files and subdirectories.
chown -R NewUser:NewGroup DirectoryName
Q. How can I check the ownership of a specific file or directory in Linux?
You can use ls command as given below:
ls -l FileorDirectoryName
Q. What is the difference between the chown and chmod command in Linux?
The chown command is used to modify who can access and modify the file or directory, while the chmod command is used to change what actions a user group can perform on its file or directory.
Now you know how to use the chown command in Linux to alter a file's user and/or group ownership. Take extreme care while altering the group or ownership of a file or folder, especially when using the chown command with sudo.