File ownership and groups for files are fundamental to the Linux operating system. Every file in Linux is managed by a specific user and a specific group.
Figure out who owns the File or Folder, then use either chown or chgrp
Display ownership and group information using the following command:
1 | $ ls -l ./ashokma.com/index.html |
This folder is owned by the root user and belongs to the docker group.
Changing the ownership of a file using chown
You can change the ownership of a specific file using the chown command. For security purposes only, the root user or members of the sudo group may transfer ownership of a file.
To change the ownership of a file:
1 | $ chown ashokma ./ashokma.com/index.html |
The file index.html is now owned by ashokma.
By default,
chownfollows symbolic links and changes the owner of the file pointed to by the symbolic link.
Tip: If you wish to change ownership of all files inside a directory, you can use the
-Roption.
1 | $ chown -R ashokma ./ashokma.com/ |
Changing the group ownership of a file using chgrp
All users on the system belong to at least one group. You can find out which groups you belong to using the following command:
1 | $ groups ashokma |
You can then change the group ownership of a specific file using the chgrp command:
1 | $ chgrp ashokma index.html |
The file index.html now belongs to the ashokma group.
Changing both the owner and the group using chown
You can change both the owner and group of a file using just the chown command.
1 | $ chown ashokma:docker index.html |
The file index.html is now owned by ashokma and belongs to the docker group.