LinuxCommandLibrary

chown

Change file owner and group

TLDR

Change the owner user of a file/directory

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

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

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

Change the group of a file to a group that the current user belongs to
$ chown :[group] [path/to/file_or_directory]
copy

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

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

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

SYNOPSIS

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

PARAMETERS

-c, --changes
    Like verbose, but report only when changes occur

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

-h, --no-dereference
    Affect symlinks themselves (requires -P or default)

-H
    Traverse symlinks to dirs on command line

-L, --dereference
    Follow symlinks (default with -H/-P off)

-P
    Do not follow symlinks on command line (default)

-R, --recursive
    Operate recursively on directories

--reference=RFILE
    Copy owner/group from reference file RFILE

-v, --verbose
    Output message for each processed file

--help
    Display usage summary

--version
    Show version info

DESCRIPTION

The chown command modifies the owner and/or group of files and directories in Linux/Unix systems. Ownership is fundamental to the permission model, determining access rights via user ID (UID) and group ID (GID). Typically, only root or the current owner (with CAP_CHOWN capability) can alter ownership, making chown a powerful tool for administrators.

Specify ownership as OWNER[:GROUP], where OWNER is a username or UID, and GROUP a groupname or GID. Numeric IDs allow scripting without name resolution. For instance, chown alice:users file.txt sets owner to alice and group to users.

Common in server setup, like assigning web files to apache user, or securing directories. Combined with chmod, it enforces least privilege. Options handle recursion for trees, symlinks, and verbosity. Misuse risks security breaches by granting unintended access.

Always verify with ls -l post-change. Operates atomically per file, but recursion on large filesystems demands caution.

CAVEATS

Requires root privileges usually; recursive (-R) on large trees can be slow/dangerous.
Group changes may need CAP_CHOWN; verify with ls -l.

EXAMPLES

chown root:root /etc/passwd
chown -R www-data:www-data /var/www
chown --reference=template.txt newfile.txt

NUMERIC IDS

Use UIDs/GIDs like chown 1000:1000 file for portability.

HISTORY

Originated in Version 7 Unix (1979). Standardized in POSIX.1-2001. GNU coreutils version enhances symlink handling since 1990s.

SEE ALSO

chgrp(1), chmod(1), ls(1)

Copied to clipboard