chown Command

Purpose

Changes the owner or group associated with a file.

Syntax

chown-f ] [ -h ] [  -R ] Owner :Group ] { File ... Directory ... }

chown -R -f ] [ -H | -L | -P ] Owner :Group ] { File ... Directory ... }

Description

The chown command changes the owner of the file or directory specified by the File or Directory parameter to the user specified by the Owner parameter. The value of the Owner parameter can be a user name from the user database or a numeric user ID. Optionally, a group can also be specified. The value of the Group parameter can be a group name from the group database or a numeric group ID.

Only the root user can change the owner of a file. You can change the group of a file only if you are a root user or if you own the file. If you own the file but are not a root user, you can change the group only to a group of which you are a member.

Although the -H, -L and -P flags are mutually exclusive, specifying more than one is not considered an error. The last flag specified determines the behavior that the command will exhibit.

When a symbolic link is encountered and you have not specified the -h flag, the chown command changes the ownership of the file or directory pointed to by the link and not the ownership of the link itself.

If you specify the -h flag, the chown command has the opposite effect and changes the ownership of the link itself and not that of the file or directory pointed to by the link.

If you specify the -R flag, the chown command recursively descends the specified directories.

If you specify both the -h flag and the -R flag, the chown command descends the specified directories recursively, and when a symbolic link is encountered, the ownership of the link itself is changed and not that of the file or directory pointed to by the link.

Flags

Item Description
-f Suppresses all error messages except usage messages.
-h Changes the ownership of an encountered symbolic link and not that of the file or directory pointed to by the symbolic link.
-H If the -R option is specified and a symbolic link referencing a file of type directory is specified on the command line, the chown command shall change the user ID (and group ID, if specified) of the directory referenced by the symbolic link and all files in the file hierarchy below it.
-L If the -R option is specified and a symbolic link referencing a file of type directory is specified on the command line or encountered during the traversal of a file hierarchy, the chown command shall change the user ID (and group ID, if specified) of the directory referenced by the symbolic link and all files in the file hierarchy below it.
-P If the -R option is specified and a symbolic link is specified on the command line or encountered during the traversal of a file hierarchy, the chown command shall change the owner ID (and group ID, if specified) of the symbolic link if the system supports this operation. The chown command shall not follow the symbolic link to any other part of the file hierarchy.
-R Descends directories recursively, changing the ownership for each file. When a symbolic link is encountered and the link points to a directory, the ownership of that directory is changed but the directory is not further transversed. If the -h, -H, -L or -P flags are not also specified, when a symbolic link is encountered and the link points to a directory, the group ownership of that directory is changed but the directory is not traversed further.

Exit Status

This command returns the following exit values:

Item Description
0 The command executed successfully and all requested changes were made.
>0 An error occurred.

Security

Access Control

This program should be installed as a normal user program in the Trusted Computing Base.

Attention RBAC users and Trusted AIX® users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations associated with this command, see the lssecattr command or the getcmdattr subcommand.

Examples

  1. To change the owner of the file program.c:
    chown jim program.c
    The user access permissions for program.c now apply to jim. As the owner, jim can use the chmod command to permit or deny other users access to program.c.
  2. To change the owner and group of all files in the directory /tmp/src to owner john and group build:
    chown -R john:build /tmp/src

Files

Item Description
/usr/bin/chown The chown command
/etc/group File that contains group IDs
/etc/passwd File that contains user IDs