Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux chown Command

Linux chown Command

Linux Command Reference Linux Command Reference

Linux chown (short forchange owner) command is used to set the owner and the associated group of a file.

Linux/Unix is a multi-user, multitasking operating system, and all files have an owner. Use chown to change the owner of a specified file to a specified user or group. A user can be a username or a user ID; a group can be a group name or a group ID; the file is a list of files whose permissions are to be changed, separated by spaces, and wildcards are supported.

chown requires superuser (root) privileges to execute this command; a non-superuser who needs to set the associated group may need to use chgrp command.

Permissions : root

Syntax

chown [选项] 所有者[:组] 文件...

chown [选项] --reference=参考文件 文件...

Option :

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

Example

Set the owner of /var/run/httpd.pid to root:

chown root /var/run/httpd.pid

Set the owner of the file file1.txt to runoob and the group to runoobgroup:

chown runoob:runoobgroup file1.txt

Set the owner of all files and subdirectories in the current directory to runoob and the group to runoobgroup:

chown -R runoob:runoobgroup *

Set the associated group of /home/runoob to 512 (group ID) without changing the owner:

chown :512 /home/runoob

Practical Application Examples

1. Web server file management

# 将网站文件所有权给web服务器

chown -R www-data:www-data /var/www/html/



# Set upload directory permissions

chown -R apache:apache /var/www/uploads/

chmod -R 755 /var/www/uploads/

2. User file management

# 将用户目录所有权返回给用户

chown -R john:john /home/john/



# Set home directory after creating user

chown -R newuser:newuser /home/newuser/

3. System service files

 设置服务日志文件所有者

chown syslog:adm /var/log/application.log



# Set database file owner

chown -R mysql:mysql /var/lib/mysql/

4. Development project permissions

# 设置项目文件所有者为开发团队

chown -R developer:developers /home/projects/webapp/



# Set deployment directory permissions

chown -R deploy:deploy /opt/application/

Advanced Tips

1. Batch operations

# 使用find命令批量更改

find /home/user -name "*.sh" -exec chown user:staff {} \;



# Change owner of all files of a specific type

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



# Set based on reference file

chown --reference=/etc/passwd /etc/shadow

3. Symbolic link handling

# 更改符号链接本身

chown -h john:staff /home/user/link



# Change the file pointed to by the symbolic link

chown john:staff /home/user/link

Linux Command Reference Linux Command Reference