LinuxCommandLibrary

chown

Change file owner and group

TLDR

Change the owner user of a file/directory

$ chown [user] [path/to/file_or_directory]
copy

Change the owner user and group of a file/directory
$ chown [user]:[group] [path/to/file_or_directory]
copy

Change the owner user and group to both have the name user
$ chown [user]: [path/to/file_or_directory]
copy

Recursively change the owner of a directory and its contents
$ chown [[-R|--recursive]] [user] [path/to/directory]
copy

Change the owner of a symbolic link
$ chown [[-h|--no-dereference]] [user] [path/to/symlink]
copy

Change the owner of a file/directory to match a reference file
$ chown --reference [path/to/reference_file] [path/to/file_or_directory]
copy

SYNOPSIS

chown [OPTION]... OWNER[:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

PARAMETERS

OWNER
    The new owner for the specified files. Can be a username or numeric UID.

GROUP
    The new group for the specified files. Can be a group name or numeric GID. Preceded by a colon (:) after the owner.

FILE...
    One or more files or directories whose ownership will be changed.

-R, --recursive
    Recursively change ownership of directories and their contents.

-v, --verbose
    Output a diagnostic for every file processed.

-c, --changes
    Like --verbose, but report only when a change is made.

-f, --silent, --quiet
    Suppress most error messages.

--from=CURRENT_OWNER[:CURRENT_GROUP]
    Change the owner/group only if the current owner/group matches this specification.

--no-preserve-root
    Do not treat '/' specially when operating recursively. By default, chown refuses to operate on the root directory recursively.

--reference=RFILE
    Use the owner and group of the specified RFILE instead of explicit OWNER and GROUP arguments.

DESCRIPTION

The chown command is used to change the user and/or group ownership of specified files or directories. It is a fundamental command for managing file system permissions and security in Linux and Unix-like operating systems.

By assigning specific users and groups as owners, administrators can control who has access to read, write, or execute files and directories. The command accepts either a user name (e.g., john) or a numeric user ID (UID), and optionally a group name (e.g., staff) or a numeric group ID (GID). If only a user is specified, their primary group may implicitly become the new group owner, or the existing group owner may remain. If a group is specified after a colon (e.g., john:staff), both user and group ownership are changed simultaneously.

chown is crucial for setting up proper access controls, especially when managing shared resources, web servers, or application data, ensuring that processes run with appropriate privileges and that sensitive information is protected.

CAVEATS

To change the ownership of a file or directory, you typically need to be the superuser (root) or have appropriate capabilities. Ordinary users can only change the group ownership of a file if they are the owner of the file and a member of the target group.

When changing the owner of an executable file, the set-user-ID (SUID) bit will be cleared for security reasons. Similarly, the set-group-ID (SGID) bit will be cleared if the group is changed and the file is executable.

chown, by default, follows symbolic links and changes the ownership of the file the link points to, not the symbolic link itself. To change the ownership of the symbolic link itself (if supported by the filesystem), the lchown system call would be used, but the chown command does not typically expose an option for this behavior directly in GNU coreutils.

USAGE WITH NUMERIC IDS

Instead of user and group names, you can also specify numeric UIDs and GIDs. For example, chown 1000:1001 file.txt would change the owner to user ID 1000 and group ID 1001. This is particularly useful in environments where user/group names might not be consistently mapped or when dealing with NFS mounts where UIDs/GIDs are paramount.

HISTORY

The chown command has been a staple of Unix-like operating systems since their early days. It is part of the POSIX standard, ensuring its consistent behavior across various Unix and Linux distributions. Its implementation is usually provided by GNU coreutils on Linux systems, reflecting its fundamental role in system administration and security.

SEE ALSO

chgrp(1), chmod(1), ls(1), id(1), stat(1)

Copied to clipboard