File ownership and group ownership are fundamental concepts in the Linux operating system. Properly managing these attributes is essential for controlling access to files and directories. Here we will explore how to change file ownership and group ownership in Linux. We have already discussed File permissions, another crucial aspect of Linux file management.
File Ownership
In Linux, each file and directory is associated with an owner. The owner has full control over the file, including the ability to modify, delete, and set permissions. To change the file ownership, use the chown command.
chown [new_owner] [file_or_directory]
[root@ervintest home]# chown ervin file1
To view the owner of a file, you can use the ls command with the -l option.
[root@ervintest home]# ls -li file1
Group Ownership
In addition to the file owner, every file and directory also has a group associated with it. The group is a collection of users who share similar permissions for the file. To change the group ownership, use the chgrp command.
chgrp [new_group] [file_or_directory]
[root@ervintest home]# chgrp systemadmin file1
[root@ervintest home]# ls -l file1
How to change both file owner and group owner of a file
If you want to change both the owner and group that a file belongs to, specify both the user and group options separated by a full colon as shown in the syntax below. Be sure that there are no spaces between the options and the colon.
chown [new_owner] [new_group] [file_or_directory]
[root@ervintest home]# chown ervin:systemadmin file1
[root@ervintest home]# ls -li file1
How to recursively change file ownership
When applying permissions to directories, you might want to apply changes recursively i.e make the ownership changes to descend and apply to files and sub-directories. To achieve this, use the recursive option -R or –recursive directive.
chown -R [user]:[group] [directoryname]
For example, the command below assigns all files and folders in the /home/adminfiles directory ownership to the systemadmin group.
[root@ervintest home]# chown -R ervin:systemadmin /home/adminfiles
[root@ervintest home]# ls -l /home/
chmod, chown and chgrp commands are powerful tool that is used for managing file and directory ownership and permissions.