chown Command Reference

Change File Owner and Group

About chown

chown (change owner) changes the user and/or group ownership of files and directories. It's essential for managing file permissions and access control in Linux systems.

Basic Syntax: chown [OPTIONS] [OWNER][:GROUP] FILE...

Common Forms:

  • chown user file - Change owner only
  • chown user:group file - Change owner and group
  • chown :group file - Change group only (same as chgrp)
  • chown user: file - Change owner and set group to user's login group

Related Commands: chgrp (change group only), chmod (change permissions), ls -l (view ownership)

Example 1: Change File Owner

The most basic use of chown - changing the owner of a file or directory. Requires root or sudo privileges.
# Check current ownership
ls -l document.txt
-rw-r--r-- 1 olduser users 1234 Nov 12 10:30 document.txt

# Change owner to 'john'
sudo chown john document.txt

# Verify the change
ls -l document.txt
-rw-r--r-- 1 john users 1234 Nov 12 10:30 document.txt

# Change owner of multiple files
sudo chown mary file1.txt file2.txt file3.txt
After change:
-rw-r--r-- 1 john users 1234 Nov 12 10:30 document.txt
Note:
Only root can change file ownership to another user. Regular users can change group ownership if they're members of both the current and target groups.

Example 2: Change Owner and Group Together

Use the colon syntax to change both owner and group in a single command.
# Change both owner and group
sudo chown john:developers project.txt

# Verify
ls -l project.txt
-rw-r--r-- 1 john developers 5678 Nov 12 10:35 project.txt

# Alternative syntax using period (older form)
sudo chown john.developers project.txt

# Change owner and set group to owner's primary group
sudo chown john: project.txt

# Multiple files with same owner:group
sudo chown mary:staff report1.doc report2.doc report3.doc
Result:
-rw-r--r-- 1 john developers 5678 Nov 12 10:35 project.txt
Note:
Using "user:" without specifying a group sets the group to the user's login group. The period syntax works but colon is preferred for clarity.

Example 3: Change Group Only

Use chown to change group ownership without affecting the file owner. This is equivalent to using chgrp.
# Change only the group
sudo chown :developers file.txt

# Before:
-rw-r--r-- 1 john staff 1234 Nov 12 10:40 file.txt

# After:
-rw-r--r-- 1 john developers 1234 Nov 12 10:40 file.txt

# Multiple files to new group
sudo chown :webadmin *.html

# This is equivalent to chgrp
sudo chgrp developers file.txt
Group changed:
-rw-r--r-- 1 john developers 1234 Nov 12 10:40 file.txt
Note:
The leading colon tells chown to change only the group. This is useful when you want to preserve ownership but adjust group access.

Example 4: Recursive Ownership Change

Use the -R (recursive) option to change ownership of directories and all their contents.
# Change ownership of directory and all contents
sudo chown -R john:developers /var/www/project

# Verify with recursive listing
ls -lR /var/www/project | head -20

# Change only files (not directories) in a tree
sudo find /var/www/project -type f -exec chown john:developers {} \;

# Change only directories in a tree
sudo find /var/www/project -type d -exec chown john:developers {} \;

# Common use: fix web directory ownership
sudo chown -R www-data:www-data /var/www/html
Directory structure:
/var/www/project:
drwxr-xr-x 5 john developers 4096 Nov 12 10:45 .
-rw-r--r-- 1 john developers 256 Nov 12 10:45 index.html
-rw-r--r-- 1 john developers 1024 Nov 12 10:45 style.css
Warning:
Be careful with recursive chown, especially on system directories. Always double-check the path before executing. Never run chown -R on / or system directories unless you know exactly what you're doing.

Example 5: Using Numeric User and Group IDs

Specify ownership using numeric UID and GID instead of names. Useful for scripting and when names might not resolve.
# Find user and group IDs
id john
uid=1001(john) gid=1001(john) groups=1001(john),1005(developers)

id -u john # Just the UID
1001

id -g developers # Just the GID
getent group developers
developers:x:1005:john,mary

# Change ownership using numeric IDs
sudo chown 1001:1005 file.txt

# Useful in scripts
USER_ID=$(id -u john)
GROUP_ID=$(getent group developers | cut -d: -f3)
sudo chown $USER_ID:$GROUP_ID file.txt

# Essential when user/group names contain special characters
sudo chown 1001:1005 "file with spaces.txt"
Result (ls -n shows numeric IDs):
$ ls -ln file.txt
-rw-r--r-- 1 1001 1005 1234 Nov 12 10:50 file.txt
Note:
Numeric IDs are portable and work even if /etc/passwd or /etc/group are unavailable (e.g., in chroot or network issues). Use ls -n to view numeric ownership.

Example 6: Reference File for Ownership

Copy ownership from one file to another using the --reference option. Useful for maintaining consistent ownership.
# Check ownership of reference file
ls -l template.txt
-rw-r--r-- 1 john developers 100 Nov 12 10:55 template.txt

# Apply same ownership to another file
sudo chown --reference=template.txt newfile.txt

# Verify
ls -l newfile.txt
-rw-r--r-- 1 john developers 200 Nov 12 10:56 newfile.txt

# Apply to multiple files
sudo chown --reference=template.txt file1.txt file2.txt file3.txt

# Recursive with reference
sudo chown -R --reference=/var/www/html /var/www/newsite

# Useful in backup restoration
sudo chown --reference=original.bak restored.file
Both files now have same ownership:
-rw-r--r-- 1 john developers 100 Nov 12 10:55 template.txt
-rw-r--r-- 1 john developers 200 Nov 12 10:56 newfile.txt
Note:
The --reference option is perfect when you need to match ownership but don't know or don't want to look up the specific user and group names.

Example 7: Verbose and Changes Output

Use -v (verbose) to see what chown is doing, and -c (changes) to see only actual changes made.
# Verbose output shows all operations
sudo chown -v john:developers file1.txt file2.txt
changed ownership of 'file1.txt' from mary:staff to john:developers
changed ownership of 'file2.txt' from bob:users to john:developers

# Show only files that actually changed
sudo chown -c john:developers file1.txt file2.txt file3.txt
changed ownership of 'file1.txt' from mary:staff to john:developers
ownership of 'file3.txt' retained as john:developers

# Recursive with verbose output
sudo chown -Rv www-data:www-data /var/www/site/

# Useful for logging changes in scripts
sudo chown -cv john:developers *.txt > ownership_changes.log 2>&1
Verbose Output:
changed ownership of 'file1.txt' from mary:staff to john:developers
changed ownership of 'file2.txt' from bob:users to john:developers
changed ownership of 'file3.txt' from root:root to john:developers
Note:
Use -v when you want confirmation of all operations. Use -c when you only want to see what actually changed. Both are valuable for auditing and troubleshooting.

Example 8: Preserve Root and Symbolic Link Handling

Use --preserve-root to prevent accidental recursive changes to root, and control how symbolic links are handled.
# Prevent accidental recursive operation on /
sudo chown --preserve-root -R john:developers /
chown: it is dangerous to operate recursively on '/'
chown: use --no-preserve-root to override this failsafe

# Change ownership of symbolic link itself (not target)
sudo chown -h john:developers symlink

# Change ownership of link target (default behavior)
sudo chown john:developers symlink

# Don't follow symbolic links during recursive operations
sudo chown -R -h john:developers /path/with/symlinks/

# Example with symlink
ln -s /var/log/app.log current.log
ls -l current.log
lrwxrwxrwx 1 root root 16 Nov 12 11:00 current.log -> /var/log/app.log

sudo chown -h john:developers current.log
lrwxrwxrwx 1 john developers 16 Nov 12 11:00 current.log -> /var/log/app.log
Symlink ownership changed:
lrwxrwxrwx 1 john developers 16 Nov 12 11:00 current.log -> /var/log/app.log
Warning:
--preserve-root is a safety feature that prevents catastrophic mistakes. Without -h, chown follows symlinks and changes the target file's ownership, which may not be what you want.

Example 9: Common Real-World Scenarios

Practical examples of chown usage in system administration tasks.
# Fix web server file ownership
sudo chown -R www-data:www-data /var/www/html
sudo chown -R apache:apache /var/www/html # On CentOS/RHEL

# Set ownership for user's home directory
sudo chown -R john:john /home/john

# Configure log file ownership
sudo chown syslog:adm /var/log/application.log

# Database file ownership
sudo chown -R mysql:mysql /var/lib/mysql
sudo chown -R postgres:postgres /var/lib/postgresql

# SSH directory ownership (security requirement)
sudo chown -R $USER:$USER ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

# Shared directory for group collaboration
sudo chown -R :developers /opt/projects
sudo chmod -R g+w /opt/projects
sudo chmod g+s /opt/projects # Set SGID bit

# Docker socket access
sudo chown root:docker /var/run/docker.sock
Note:
These are common patterns in system administration. Always verify ownership requirements for services - incorrect ownership can cause service failures or security issues.

Example 10: Ownership in Scripts and Automation

Use chown in scripts for automated deployment, backup restoration, and system configuration.
#!/bin/bash
# deployment_script.sh
# Deploy application with correct ownership

APP_USER="appuser"
APP_GROUP="appgroup"
APP_DIR="/opt/myapp"

# Create directory if it doesn't exist
sudo mkdir -p "$APP_DIR"

# Extract application files
sudo tar -xzf myapp.tar.gz -C "$APP_DIR"

# Set ownership
sudo chown -R "$APP_USER:$APP_GROUP" "$APP_DIR"

# Set specific ownership for different directories
sudo chown -R "$APP_USER:$APP_GROUP" "$APP_DIR/app"
sudo chown -R www-data:www-data "$APP_DIR/public"
sudo chown -R "$APP_USER:$APP_GROUP" "$APP_DIR/config"

# Fix ownership after backup restoration
restore_backup() {
local BACKUP_FILE=$1
local RESTORE_DIR=$2

sudo tar -xzf "$BACKUP_FILE" -C "$RESTORE_DIR"
sudo chown -R --reference="$RESTORE_DIR" "$RESTORE_DIR"/*
}

# Conditional ownership change
if [ -d "$APP_DIR/logs" ]; then
sudo chown -R syslog:adm "$APP_DIR/logs"
fi

# Report ownership changes
echo "Ownership set for $APP_DIR"
ls -la "$APP_DIR"
Note:
In scripts, always use quotes around variables to handle filenames with spaces. Verify ownership after deployment. Use conditional checks to avoid errors when directories don't exist.

Additional Tips & Best Practices

  • Permission requirements: Only root can change file owner to another user. Regular users can change group if they're members of both groups
  • Security implications: Incorrect ownership can create security vulnerabilities. Be especially careful with SUID/SGID files
  • Web servers: Most web servers run as www-data (Debian/Ubuntu) or apache (CentOS/RHEL). Check with ps aux | grep apache
  • Service accounts: Many services use dedicated users (mysql, postgres, nginx). Always use the correct service account
  • Combining with chmod: Ownership and permissions work together. Set ownership first, then permissions
  • ACLs: For complex permission scenarios, consider using setfacl/getfacl (Access Control Lists) instead of or in addition to chown
  • Backup ownership: Use tar with -p option to preserve ownership during backups: tar -czpf backup.tar.gz /path
  • Docker volumes: Inside containers, UID/GID may differ from host. Use numeric IDs for consistency
  • NFS shares: Ownership may behave differently on NFS. Ensure UID/GID match across systems or use NFSv4 with ID mapping
  • Automation safety: Use --preserve-root in scripts. Test with --dry-run equivalent (echo) first
  • Audit trail: Log ownership changes with -v option redirected to log files for security auditing
  • Group inheritance: Use SGID bit on directories (chmod g+s) to make new files inherit the directory's group