How to change the owner and group of a file in Linux?
To change the owner and group of a file, you can use the chown command. The syntax of the chown command is as follows:
chown [选项] 用户名[:组名] 文件名
The username will be changed to the new owner’s username, the group name will be changed to the new group’s name, and the file name will be changing the owner and group of the file.
Here are some common options and examples of the chown command:
- Change the owner and group of the file to a new user and new group:
chown username:groupname filename - Change the owner of the file to a new user while keeping the group the same:
chown username filename - Change the file group to a new group while keeping the owner unchanged:
chown :group_name file_name - Recursively change the owner and group of all files and subdirectories in a directory:
chown -R username:group directoryname - View the owner and group of a file:
ls -l filename
Changing the file owner and group typically requires having write access to the file.