On Unix-like operating systems, the chown command changes ownership of files and directories in a filesystem.
This page describes the GNU/Linux version of chown.
What is file “ownership”?
Linux is designed to support many users. Because of this, it needs to keep careful track of who is allowed to access a file, and how they can access it. These access rules are called permissions.
What is file “ownership”?
Syntax
Specifying the new owner
Notes on usage
Options
Options
Options
Exit status
Why change a file’s ownership?
Hypothetical scenarios
Groups in Linux
Other operating system groups
Examples
Viewing ownership
Changing ownership
Related commands
Linux commands help
Specifying the new owner
Notes on usage
Options
Options
Exit status
Hypothetical scenarios
Groups in Linux
Other operating system groups
Viewing ownership
Changing ownership
There are three major types of file permissions:
- User permissions. These permissions apply to a single user who has special access to the file. This user is called the owner.
- Group permissions. These apply to a single group of users who can access the file. This group is the owning group.
- Other permissions. These apply to every other user on the system. These users are known as others, or the world.
When a file is created, its owner is the user who created it, and the owning group is the user’s current group.
chown can change these values to something else.
Syntax
chown [-c|–changes] [-v|–verbose] [-f|–silent|–quiet] [–dereference] [-h|–no-dereference] [–preserve-root] [–from=currentowner:currentgroup] [–no-preserve-root] [-R|–recursive] [–preserve-root] [-H] [-L] [-P] {new-owner|–reference=ref-file} file …
chown –help
chown –version
Specifying the new owner
New ownership of file is specified by the argument new-owner, which takes this general form:
[user[:group]]
Specifically, there are five ways to format new-owner:
Notes on usage
- user and group can be specified by name or by number.
- Only root can change the owner of a file. The owner cannot transfer ownership, unless the owner is root, or uses sudo to run the command.
- The owning group of a file can be changed by the file’s owner, if the owner belongs to that group. The owning group of a file can be changed, by root, to any group. Members of the owning group other than the owner cannot change the file’s owning group.
- The owning group can also be changed using the chgrp command. chgrp and chown use the same system call, and are functionally identical.
- Certain miscellaneous file operations can be performed only by the owner or root. For instance, only owner or root can manually change a file’s “atime” or “mtime” (access time or modification time) using the touch command.
- Because of these restrictions, run chown as root, or with sudo.
Options
Options
The following options modify how a hierarchy is traversed when the -R or –recursive option is specified.
changed ownership of ‘dir/dir1/file1’ from hope:neil to hope:hope
changed ownership of ‘dir/dir1/file1’ from hope:neil to hope:hopeownership of ‘dir/dir1’ retained as hope:hope
If more than one of -H, -L, or -P is specified, only the final option takes effect.
These options display information about the program, and cannot be used with other options or arguments.
Exit status
chown exits with a status of 0 for success. Any other number indicates failed operation.
Why change a file’s ownership?
You should use chown when you want a file’s user or group permissions to apply to a different user or group.
Hypothetical scenarios
Here are examples of when you might use chown:
- You create a file, myfile.txt, using sudo or while logged in as root, so the file is owned by root. However, you intend the file to be used by your regular user account, myuser.Use chown to change the owner:
sudo chown myuser myfile.txt
- You own myfile.txt, but you want to give it to another user on the system named notme. You also want to change the owning group to that user’s group, notmygroup.Use chown to change the owner and group:
sudo chown notme:notmygroup myfile.txt
- You just transferred an entire directory of files, otherfiles, from another computer. All the files and directories are owned by your username on the other system, and you want your current user and group to own them all.Change the ownership of the directory and all its contents recursively, with the -R option:
sudo chown -R myuser:mygroup otherfiles
The above command changes the ownership of every file, subdirectory, and subdirectory contents in otherfiles.
Groups in Linux
In Linux, a user is a member of multiple groups, but it has only one “current group”. The user’s current group is the user’s group identity, or GID.
When the user creates a new file, the file’s ownership is set to the user’s UID (user identity) and GID (group identity). So when user carla starts writing a new document, the file is owned by carla, and also by her current group. She can change the file’s group ownership with chown, but only root can use chown to change the owner to someone else.
Also, each user has a configurable login group, which can be any of the user groups. So when carla logs in, her login group is her current group. The login group can be changed with the usermod command, using the -g option.
sudo usermod -g newlogingroup carla
A user can change current group with the newgrp command. The change takes place in a subshell, and persists until the subshell is closed. Even if carla changes her current group with newgrp, it will be reset to her login group the next time she logs in.
You can check your current group using the id command with the -g option:
id -g
1001
This is your numeric GID (the number of your current group). To see the name, specify the -n option:
id -ng
hope
To view all of your group memberships, use a capital G:
id -nG
hope sudo neil libvirtd vboxusers usergroup
By default, every Linux user has a private group, with that user as the only member. So, when the user account jeff is created with the adduser command, a group named jeff is also created. Group jeff is jeff’s default login group, and has only one member (jeff).
Other operating system groups
Other operating systems use chown, but their groups may function differently.
In macOS X and BSD, for example, users don’t have private groups. Instead, all regular users belong to a general group called users.
In these operating systems, the options and functionality of chown may be similar, but different. If you’re using chown on a non-Linux operating system, make sure to run man chown to learn what the differences are.
Examples
Viewing ownership
Before you use chown, you may want to check the current ownership of a file. You can view a file’s ownership, permissions, and other important information with the ls command, using the -l option:
ls -l myscript.sh
-rwxrw-r– 1 hope hopeusers 12 Nov 5 13:14 myscript.sh
In the output, you see several fields of information listed, including the permissions and ownership of the file. It might not make sense at first, so let’s describe it in detail.
Here’s what the information means:
So the important fields here are 1, 3 and 4. They tell us that user hope can read, write, or execute the file’s contents, and members of the group hopeusers can read or write to it.
Changing ownership
sudo chown hope file.txt
Change the owner of file.txt to user hope.
sudo chown hope file1 file2 file3
Change the owner of file1, file2, and file3 to user hope.
sudo chown hope file*
Here, the asterisk ("*") is a wildcard which the shell expands to a list of every file whose name begins with “file”. If the current directory contains four files named file1, file2, file3, and file4, all these files’ names are passed to the chown command, and their owners changed to user hope.
sudo chown hope myfiles
Change the owner of file or directory myfiles to user hope.
sudo chown -R hope myfiles
Change the owner of myfiles to user hope. If myfiles is a directory, chown will recursively (-R) search that directory, and change the owner of all files, subdirectories, and subdirectory contents.
sudo chown hope:admins file1 file2
Change the owners of file1 and file2 to user hope, and the owning groups to admins.
sudo chown hope: file1
Change the owner of file1 to user hope, and the owning group to hope’s login group.
chown :othergroup file2
Change the owning group of file2 to group othergroup. Notice that this is the only command in these examples which may run without sudo.
If user hope runs the previous command but does not belong to group othergroup, the command fails, unless it is run with sudo.
sudo chown 1000:1001 file1
Change the ownership of file1 to the user with numeric UID 1000, and the group with numeric GID 1001.
sudo chown +1000:+1001 file1
Same as the previous command. If user hope has UID 1000, and another user is named “1000” but has UID 1002, this command form (with the “+” signs) unambiguously changes the owner to hope.
sudo chown -R hope:hope Documents
Recursively change the ownership of directory Documents, and all files and subdirectories therein, to user hope, group hope.
sudo chown -Rc –reference /home/hope/inbox ~/Documents/work
Recursively change the ownership of the directory ~/Documents/work, and all files and subdirectories therein, to match the ownership of the file or directory /home/hope/inbox.
In the above command, ~ (a tilde) is an alias in bash which represents your home directory. Your home directory can also be represented by the environment variable $HOME, as in $HOME/Documents/work.
Also, if any files change ownership (-c option), information will be printed to standard output:
changed ownership of ‘dir/file2’ from neil:neil to hope:hope changed ownership of ‘dir/dir1/file1’ from susie:susie to hope:hope changed ownership of ‘dir/dir1’ from judy:judy to hope:hope changed ownership of ‘dir/dir2/file2’ from jeff:jeff to hope:hope changed ownership of ‘dir/dir2’ from carla:carla to hope:hope changed ownership of ‘dir/file1’ from steve:steve to hope:hope changed ownership of ‘dir’ from grace:grace to hope:hope
Related commands
chgrp — Change the group ownership of files or directories.chmod — Change the permissions of files or directories.ls — List the contents of a directory or directories.id — Display group IDs.usermod — Modify a user’s account settings.