Linux Comm Chown
# Linux chown Command
[ Linux Command Manual](#)
The Linux **chown** (short for **change owner**) command is used to change the owner and associated group of files.
Linux/Unix is a multi-user, multitasking operating system where all files have an owner. Using `chown`, you can change the owner of specified files to a designated user or group. The user can be specified by username or user ID, and the group by group name or group ID. The file list is separated by spaces and supports wildcards.
Executing the `chown` command requires superuser (root) privileges. Non-superusers who need to change the associated group may need to use the (#) command.
**Required Permission**: root
### Syntax
chown owner[:group] file...
chown --reference=reference_file file...
**Options**:
* -R, --recursive: Recursively change the owner of all files and subdirectories within a directory.
* -v, --verbose: Display detailed operation information.
* -c, --changes: Only display files that are actually changed.
* -f, --silent, --quiet: Suppress error messages.
* --reference=file: Use the owner and group settings from the reference file.
* -h, --no-dereference: Affect the symbolic link itself, not the file it points to.
* --from=current_owner: Only change if the current owner matches.
### Basic Usage Format
**1. Change Owner Only**
chown username filename
chown john file.txt
**2. Change Owner and Group Simultaneously**
chown username:groupname filename
chown john:developers script.sh
**3. Change Group Only (Leave Owner Blank)**
chown :groupname filename
chown :staff document.txt
**4. Use Numeric IDs**
chown 1000:1000 filename # User ID 1000, Group ID 1000
chown 1001 filename # Change only the User ID
### Examples
Set the owner of `/var/run/httpd.pid` to root:
chown root /var/run/httpd.pid
Set the owner of `file1.txt` to `tutorial` and the group to `tutorialgroup`:
chown tutorial:tutorialgroup file1.txt
Set the owner of all files and subdirectories in the current directory to `tutorial` and the group to `tutorialgroup`:
chown -R tutorial:tutorialgroup *
Set the associated group of `/home/tutorial` to `512` (group ID) without changing the owner:
chown :512 /home/tutorial
### Practical Application Examples
**1. Web Server File Management**
# Give website file ownership to the web server
chown -R www-data:www-data /var/www/html/
# Set permissions for the upload directory
chown -R apache:apache /var/www/uploads/
chmod -R 755 /var/www/uploads/
**2. User File Management**
# Return user directory ownership to the user
chown -R john:john /home/john/
# Set home directory after creating a new user
chown -R newuser:newuser /home/newuser/
**3. System Service Files**
# Set the owner of service log files
chown syslog:adm /var/log/application.log
# Set the owner of database files
chown -R mysql:mysql /var/lib/mysql/
**4. Development Project Permissions**
# Set project file ownership for the development team
chown -R developer:developers /home/projects/webapp/
# Set permissions for the deployment directory
chown -R deploy:deploy /opt/application/
### Advanced Usage Tips
**1. Batch Operations**
# Use the find command for batch changes
find /home/user -name "*.sh" -exec chown user:staff {} ;
# Change ownership only for specific file types
find /var/www -type f -name "*.php" -exec chown www-data:www-data {} ;
**2. Conditional Changes**
# Only change if the current owner is root
chown --from=root john /home/john/file.txt
# Set based on a reference file
chown --reference=/etc/passwd /etc/shadow
**3. Handling Symbolic Links**
# Change the symbolic link itself
chown -h john:staff /home/user/link
# Change the file the symbolic link points to
chown john:staff /home/user/link
[ Linux Command Manual](#)
YouTip