The chown command changes file and directory ownership in Linux. It’s essential for system administration, security management, and proper file permissions. Only root or the current owner can change ownership.
Key Concepts
- Owner: User who owns the file/directory
- Group: Group that owns the file/directory
- UID/GID: Numeric user and group identifiers
- Recursive: Apply changes to directories and contents
- Symbolic Links: How ownership changes affect links
Command Syntax
chown [options] owner[:group] file(s)
- Changes user ownership, optionally group too
- Can use usernames or numeric IDs
- Supports multiple files and wildcards
Common Options
-R - Recursive (include subdirectories)
-v - Verbose (show changes made)
-c - Show only actual changes
--reference=file - Copy ownership from reference file
-h - Change symbolic links, not targets
Practical Examples
Example 1: Change user ownership
|
|
Changes file owner to user ‘alice’
Example 2: Change user and group
|
|
Sets owner to ‘bob’ and group to ‘developers’
Example 3: Recursive ownership change
|
|
Changes ownership of web directory and all contents
Example 4: Using numeric IDs
|
|
Uses numeric user ID 1001 and group ID 1001
Example 5: Copy ownership from reference
|
|
Copies ownership from existing file
Use Cases
- Setting up web server file permissions
- Fixing ownership after file transfers
- Preparing files for specific applications
- System administration and user management
- Securing sensitive files and directories
Related Commands
chgrp - Change group ownership only
chmod - Change file permissions
ls -l - View current ownership and permissions
id - Show user and group IDs
stat - Display detailed file information
Tips & Troubleshooting
Permission Issues:
- Only root can change ownership to different users
- Regular users can only change group to groups they belong to
- Use
sudowhen permission denied
Common Mistakes:
|
|
Security Notes:
- Verify ownership changes with
ls -l - Be cautious with recursive operations
- Don’t change system file ownership unless necessary
- Use principle of least privilege
Backup Important Files:
|
|
The -p preserves original ownership and permissions