The chown command is used to change the owner, the group, or both for a file or directory. At times, you may wish for someone else to manage an object’s permissions other than the user who created that object.
The following example describes how to use this command.
1. Change the owner but not the group.
# chown {user name} {file/directory name}
2. Change the owner and the group.
# chown {user name}:{group name} {file/directory name}
3. Changes the owner and the group. The group will be changed to the specified user’s login group.
# chown {user name}:{file/directory name}
4. Changes the group but not the owner. This is the same as using the chgrp command.
# chown :{group name} {file/directory name}
You can combine the chown command with the -R option to recursively change ownership through a directory structure.
If you encounter the below error while running the chown command:
chown: command not found
you may try installing the coreutils package as shown below as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
chown Command Examples
1. To change the owner of the file:
# chown mike file.txt
2. To see if the changes have taken place or not:
# chown -c mike file.txt
3. To suppress if any error messages:
# chown -f mike file.txt
4. To apply the changes recursively:
# chown -R mike /mydir
5. To change the file owner and group at once:
# chown mike:SUPPORT file.txt
6. Change the owner of a symbolic link:
# chown -h user path/to/symlink
7. Change the owner of a file/directory to match a reference file:
# chown --reference=path/to/reference_file path/to/file_or_directory