Linux chown Command
Linux chown (English full spelling:change owner) The command is used to set the file owner and the file's associated group.
Linux/Unix is a multi-user and multi-tasking operating system, and all files have an owner. Use chown to change the owner of specified files to a specified user or group. The user can be a username or user ID, and the group can be a group name or group ID. The files are a space-separated list of files whose permissions are to be changed, and wildcards are supported.
chown requires superuserrootprivileges to execute this command. Non-superusers who need to set the associated group may need to usechgrpcommand.
Usage Permissions : root
Syntax
chown [选项] 所有者[:组] 文件... chown [选项] --reference=参考文件 文件...
Options :
- -R, --recursive: Recursively change the owner of all files in directories and their subdirectories
- -v, --verbose: Display detailed operation information
- -c, --changes: Only show files that actually changed
- -f, --silent, --quiet: Suppress error messages
- --reference=file: Use the owner and group settings of the reference file
- -h, --no-dereference: Affects symbolic links themselves, not the files they point to
- --from=current owner: Change only if the current owner matches
Basic Usage Format
1. Change only the owner
chown username filename chown john file.txt
2. Change both the owner and the group
chown username:groupname filename chown john:developers script.sh
3. Change only the group (leave the owner blank)
chown :groupname filename chown :staff document.txt
4. Use numeric IDs
chown 1000:1000 filename # 用户ID 1000,组ID 1000 chown 1001 filename # 只更改用户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 example, and the group to examplegroup:
chown example:examplegroup file1.txt
Set the owner of all files and subdirectories in the current directory to example, and the group to examplegroup:
chown -R example:examplegroup *
Set the associated group of /home/example to 512 (group ID), without changing the owner:
chown :512 /home/example
Practical Application Examples
1. Web server file management
# 将网站文件所有权给web服务器 chown -R www-data:www-data /var/www/html/ # 设置上传目录权限 chown -R apache:apache /var/www/uploads/ chmod -R 755 /var/www/uploads/
2. User file management
# 将用户目录所有权返回给用户 chown -R john:john /home/john/ # 新建用户后设置home目录 chown -R newuser:newuser /home/newuser/
3. System service files
#设置服务日志文件所有者 chown syslog:adm /var/log/application.log # 设置数据库文件所有者 chown -R mysql:mysql /var/lib/mysql/
4. Development project permissions
# 设置项目文件所有者为开发团队 chown -R developer:developers /home/projects/webapp/ # 设置部署目录权限 chown -R deploy:deploy /opt/application/
Advanced Usage Tips
1. Batch operations
# 使用find命令批量更改
find /home/user -name "*.sh" -exec chown user:staff {} \;
# 只更改特定类型文件的所有者
find /var/www -type f -name "*.php" -exec chown www-data:www-data {} \;
2. Conditional changes
# 只有当前所有者是root时才更改 chown --from=root john /home/john/file.txt # 基于参考文件设置 chown --reference=/etc/passwd /etc/shadow
3. Symbolic link handling
# 更改符号链接本身 chown -h john:staff /home/user/link # 更改符号链接指向的文件 chown john:staff /home/user/linkOther Extensions
Linux Command Collection