Knowledge base
Search…
File ownership & groups in linux

File concept

Every file in Linux is managed by a specific user and a specific group.

1. Display ownership and group information:

1
$ ls -l file.txt
2
-rw-rw-r-- 1 root www-data 0 Feb 25 15:51 file.txt
Copied!
This file is owned by the root user and belongs to the www-data group.

2. Change the ownership of a file by using chown

Important: ONLY root user or members of the sudo group may transfer ownership of a file
1
$ sudo chown robert file.txt
2
$ ls -l file.txt
3
-rw-rw-r-- 1 robert www-data 0 Feb 25 15:51 file.txt
Copied!
3. Changing the Group Ownership of a file by 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: groups username
Change the group ownership of a specific file using the chgrp command
1
$ chgrp webdev file.txt
2
$ ls -l file.txt
3
-rw-rw-r-- 1 robert webdev 0 Feb 25 15:51 file.txt
Copied!
The file file.txt now belongs to the webdev group.

Most important:

Change both the owner and group of a file using just the chown command
1
$ sudo chown tito:editors file.txt
2
$ ls -l file.txt
3
-rw-rw-r-- 1 tito editors 0 Feb 25 15:51 file.txt
Copied!
Last modified 2yr ago