This is a complete, beginner-friendly, detailed guide to the chown command in Linux. This tutorial will teach you what the chown command does, and how to use it, along with a few useful examples.
What is the chown command used in Linux for?
The chown command is used in Linux to change a file’s (or folder’s) ownership. It stands for change owner. It’s commonly used to change the owner of a certain file or directory from one user to another.
How to use the chown command in Linux?
The most basic syntax of the chown command is:
chown username_of_new_owner file_name
And the most basic example of that syntax is:
chown linuxstans notes.txt
This command will make the user “linuxstans” the owner of the file “notes.txt”.
To check and verify the ownership, we can use the “ls -l” command for that file:
ls -l notes.txt
Which should give you an output similar to:
-rw-r--r-- 1 linuxstans root 0 Sep 6 12:35 notes.txt
The owner of the file is in the third column, the group is in the fourth column.
The full syntax of the chown command is:
chown OPTIONs OWNER:GROUP FILEs
Some of the more useful available options for the chown command are:
- -f forces the command, it will suppress most error messages
- -v outputs a result with what has been done for every processed file
- -c outputs a result only if the ownership has been changed
- -R makes chown work on files and directories recursively
- –reference uses a reference file to source the ownership settings from
- –from checks and verifies if the specified user and group are the current ones, and if they are, updates them
- -h updates the symbolic link instead of the file
- –help displays the help page of the chown command with detailed usage instructions and examples
Run the following command:
To get more details and information about chown.
You can combine multiple options at once.
You can use a user’s ID instead of their username, as well as a group’s GID instead of its name.
Examples of the chown command in Linux
Here are some useful examples of the chown command that you can run on Linux:
Change the ownership of multiple files and directories at once
To change the ownership of multiple files and directories with one chown command, just separate the files with a space:
chown linuxstans file1.txt file2.mp3 Directory1
This command will make the user “linuxstans” the owner of the files file1.txt, file2.mp3 and the owner of the directory “Directory1”.
Change the ownership of all files and (sub)directories recursively
To change ownership recursively of all specified files and (sub)directories, you need to use the -R option:
chown -R linuxstans Music
This command will make the user “linuxstans” the owner of all files and directories within the directory Music.
Change the ownership by specifying their UID instead of username
Instead of using a username, you can replace it with a UID on all examples in this article.
So, to change the owner of the notes.txt file to the user with UID 1337, you can run the following command:
chown 1337 notes.txt
Warning: if a user with the username “1337” exists, it takes precedence over the user with the UID 1337.
To check/get the UID of a certain user, run the following command:
id -u username
Change the group of a file or directory
You can change the group without changing the owner. To do that, run the following command:
chown :GROUP FILEs
If you run the following command:
chown :Admins notes.txt
It will change the group of the file notes.txt to Admins.
So if you run “ls -l notes.txt” now, you’ll get:
-rw-r--r-- 1 linuxstans Admins 0 Sep 6 12:35 notes.txt
Change the owner AND the group of a file
To change the owner and the group of a file at the same time, you can use this syntax:
chown OWNER:GROUP file
So if you run:
chown linuxstans:Admins notes.txt
You’ll change the owner of the file “notes.txt” to “linuxstans” and the group to “Admins”.
Change the group of a file to the owner’s login group
If you don’t specify a group when using : with the chown command, it will use the user’s login group.
So, for example, this command:
chown linuxstans: notes.txt
Will change the group of the file notes.txt to the login group of the “linuxstans” user.
Copy ownership settings from one file to another
If you need to copy the owner and group settings from one file to another, you can use the –reference option.
Running this command:
chown --reference=notes.txt newNotes.txt
Will set the owner and group of the file “newNotes.txt” to the same owner and group from the file “notes.txt”
Change the owner of a file only if the current owner is the specified one
You can use the “–from” option to first check if the specified user is the owner of the file, and if it is, change the ownership to the new specified user. Example:
chown --from=linuxstans root notes.txt
This command will update the owner of the file “notes.txt” to “root” only if the current owner of that file is “linuxstans”.
Get an output of what the chown command did
Use the option “-v” to get an output of what the chown command did when running it.
This command will output all the files and ownerships that were changed in the “Notes” directory:
chown -R -v linuxstans Notes
An example output is:
changed ownership of 'Notes/tuesday/todo.txt' from root to linuxstans changed ownership of 'Notes/school/math.txt' from root to linuxstans changed ownership of 'Notes/work/report.txt' from root to linuxstans changed ownership of 'Notes' from root to linuxstans
Using the option “-c” instead of “-v” will give you an output only if something is changed. So, if you run this command again, after running it with the “-v” option, you wouldn’t get an output because no files or ownerships were changed:
chown -R -c linuxstans Notes
Hide errors when running chown
You may sometimes get errors that a file or a directory doesn’t exist, you can hide those errors by using the “-f” option.
chown -f linuxstans Notess
That’s about it. You can now combine these examples and test the chown command yourself.
FAQs about the chown command in Linux
Here are the answers to some questions you might have for chown in Linux:
Can you use chown on all Linux distros?
Yes, the chown command can be used on all Linux distros, even on other Unix-like systems.
Can I install the chown command if it’s not already installed?
In most cases, the chown command is pre-installed on all systems as part of the “coreutils” package (GNU core utilities). You can install the package by running:
sudo apt install coreutils
For Ubuntu, Linux Mint, Debian, and other similar distros.
Or you can run:
dnf install coreutils
For CentOS, Fedora, RedHat, and other similar distros.
Can I update chown?
You can check the current version of chown along with other information by running:
Updating chown is done by updating the coreutils package.
Can I use multiple options at once with chown?
Yes, you can combine multiple options at once with the chown command. Just separate them with a space.
Why am I getting the “Operation not permitted” error?
You can get this error with several different uses and scenarios – all of them meaning that you don’t have permission to update the ownership. The most common causes are:
- You need to be the root user or run the command with sudo.
- The file you’re trying to update has an immutable flag.
- The filesystem is not compatible (NTFS).
- If SELinux is enabled, it might block some users from running chown on certain files.
- The filesystem (storage device) is read-only.
What’s the difference between chown and chmod?
In short, chown changes who owns a file or a directory, and chmod changes who can do what on a file or a directory.
If you have any other questions about the chmod command on Linux, leave a comment below.
If you want to learn more about other commands on Linux, check our commands tag.