On Unix-like operating systems, the chown command changes ownership of files and directories in a filesystem. Show
This page describes the GNU/Linux version of chown.
What is file "ownership"?Linux is designed to support a large number of 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. There are three major types of file permissions:
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. Syntaxchown [-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 ownerNew 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: new-owner formDescriptionuserThe name of the user to own the file. In this form, the colon (":") and the group is omitted. The owning group is not altered.user:groupThe user and group to own the file, separated by a colon, with no spaces between.:groupThe group to own the file. In this form, user is omitted, and the group must be preceded by a colon.user:If group is omitted, but a colon follows user, the owner is changed to user, and the owning group is changed to the login group of user.:Specifying a colon with no user or group is accepted, but ownership isn't changed. This form does not cause an error, but changes nothing.Notes on usage
OptionsOptionDescription-c,--changesSimilar to --verbose mode, but only displays information about files that are actually changed. For example: changed ownership of 'dir/dir1/file1' from hope:neil to hope:hope-v, --verboseDisplay verbose information for every file processed. For example: changed ownership of 'dir/dir1/file1' from hope:neil to hope:hopeownership of 'dir/dir1' retained as hope:hope-f, --silent, --quietQuiet mode. Do not display output.--dereferenceDereference all symbolic links. If file is a symlink, change the owner of the referenced file, not the symlink itself. This is the default behavior.-h, --no-dereferenceNever dereference symbolic links. If file is a symlink, change the owner of the symlink rather than the referenced file.--from=currentowner:currentgroupChange the owner or group of each file only if its current owner or group match currentowner and/or currentgroup. Either may be omitted, in which case a match is not required for the other attribute.--no-preserve-rootDo not treat / (the root directory) in any special way. This is the default behavior. If the --preserve-root option is previously specified in the command, this option will cancel it.--reference=ref-fileUse the owner and group of file ref-file, rather than specifying ownership with new-owner.-R, --recursiveOperate on files and directories recursively. Enter each matching directory, and operate on all its contents. OptionsThe following options modify how a hierarchy is traversed when the -R or --recursive option is specified. OptionDescription--preserve-rootNever operate recursively on the root directory /.If --recursive is not specified, this option has no effect.-HIf a file specified on the command line is a symbolic link to a directory, traverse it and operate on those files and directories as well.-LTraverse all symbolic links to a directories.-PDo not traverse any symbolic links; operate on the symlinks themselves. This is the default behavior. If more than one of -H, -L, or -P is specified, only the final option takes effect. OptionsThese options display information about the program, and cannot be used with other options or arguments. OptionDescription--helpDisplay a brief help message and exit.--versionDisplay version information and exit.Exit statuschown 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 scenariosHere are examples of when you might use chown:
sudo chown myuser myfile.txt
sudo chown notme:notmygroup myfile.txt
sudo chown -R myuser:mygroup otherfiles The above command changes the ownership of every file, subdirectory, and subdirectory contents in otherfiles. Groups in LinuxIn 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: chown --help0 chown --help1 This is your numeric GID (the number of your current group). To see the name, specify the -n option: chown --help2 chown --help3 To view all of your group memberships, use a capital G: chown --help4 chown --help5 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 groupsOther 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. ExamplesViewing ownershipBefore 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: chown --help6 chown --help7 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: DataField positionDescription-Field 1, character 1File type: d for a directory, l (lowercase L) for a symbolic link, or - (a dash) for a regular file.rwxField 1, characters 2-4User permissions. The owner can read ("r"), write to ("w"), and execute ("x") this file.rw-Field 1, characters 5-7Group permissions. The owning group can read and write to this file, but cannot execute it as a command.r--Field 1, characters 8-10Other permissions, also known as world permissions. Any other user on the system is allowed to read the file only.1Field 2Number of symbolic links to this file. If there are no symbolic links to the file, this number is 1, because the original file name is included in this count. If there were one symbolic link to the file, this number would be 2, or 3 for two symbolic links, etc.hopeField 3Name of owner. This is the name of the user who owns the file. When this user tries to access the file, access is restricted according to the user permissions.hopeusersField 4Name of owning group. This is the user group who owns the file. When a user who is a member of this group tries to access the file, access is restricted according to the group permissions.12Field 5Size. This file contains 12 bytes of data.NovField 6Mtime (month). Abbreviated name of the month when the file's contents were last modified. This file was last modified in the month of November.5Field 7Mtime (day of month). This file was last modified on the fifth day of November.13:14Field 8Mtime (time, or year). This file was last modified at 13:14 (1:34 P.M.) on November 5 of this year. If it was modified over a year ago, this field lists the year instead, for instance 2015.myscript.shField 9File name. The name of the file.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 ownershipchown --help8 Change the owner of file.txt to user hope. chown --help9 Change the owner of file1, file2, and file3 to user hope. chown --version0 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. chown --version1 Change the owner of file or directory myfiles to user hope. chown --version2 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. chown --version3 Change the owners of file1 and file2 to user hope, and the owning groups to admins. chown --version4 Change the owner of file1 to user hope, and the owning group to hope's login group. chown --version5 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. chown --version6 Change the ownership of file1 to the user with numeric UID 1000, and the group with numeric GID 1001. chown --version7 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. chown --version8 Recursively change the ownership of directory Documents, and all files and subdirectories therein, to user hope, group hope. chown --version9 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. Who may change the ownership of a file?Normal users can change the group of the file only if they own the file and only to a group of which they are a member. Administrative users can change the group ownership of all files.
Who can change file ownership in Linux?On Linux, only root can use chown for changing ownership of a file, but any user can change the group to another group he belongs to.
Who can change the ownership of a file using the command chown?By default, the owner cannot use the chown command to change the owner of a file or directory. However, you can enable the owner to use the chown command by adding the following line to the system's /etc/system file and rebooting the system.
Who can change the owner of a file in Unix?No one else (but the administrator) is able to make these changes, and only the administrator can change the owner of a file.. The command to change the owner is chown: ... . The user named "exampleuser" now owns file. ... . More information regarding the chown command can be found here.. |