DEV Community

Cover image for Master Linux File Ownership While Your Coffee Brews
Nedim Hadzimahmutovic
Nedim Hadzimahmutovic

Posted on

Master Linux File Ownership While Your Coffee Brews

This is the third article from a series compiled from my notes while taking the LPI exams.

In the Linux operating system, file ownership is a very important security aspect. The chown command lets you change who owns a file or folder. This is very helpful when administrators need to give or take away access to certain files. This chapter introduces how to use the chown command.

Identify File Ownership

To view the file ownership for /etc/passwd do as follows.

ls -l /etc/passwd
-rw-r--r-- 1 root root 3274 Dec 22 16:13 /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Identify File Ownership

The chown command

The chown command is used to modify file ownership. The syntax for the chown is:

chown user_name:group_name file_name
Enter fullscreen mode Exit fullscreen mode

To view the current ownership of a file use the ls -l command to list files with detailed information.

ls -l target_file.txt
-rw-r--r-- 1 root root 0 Nov  6 12:23 target_file.txt
Enter fullscreen mode Exit fullscreen mode

Change the Owner

Following is an example of how to change the owner of a file.

First, we create the myfile file and list the current ownership details.

touch myfile
ls -l myfile
-rw-r--r-- 1 root root 0 Jan  1 22:36 myfile
Enter fullscreen mode Exit fullscreen mode

The next step is to change the owner from root to user kulin.

chown -v kulin myfile
changed ownership of 'myfile' from root to kulin
Enter fullscreen mode Exit fullscreen mode

The last step is to list the new ownership information.

ls -l myfile
-rw-r--r-- 1 kulin root 0 Jan  1 22:36 myfile
Enter fullscreen mode Exit fullscreen mode

To view only the username of the owner you can use the stat command as follows.

stat -c "The username %U is the owner for the file %n" myfile
The username kulin is the owner for the file myfile
Enter fullscreen mode Exit fullscreen mode

Change the Owner Group

Following is an example of how to change the group ownership of a file.

First we list the current ownership information.

ls -l myfile
-rw-r--r-- 1 kulin root 0 Jan  1 22:36 myfile
Enter fullscreen mode Exit fullscreen mode

Next, we change the group of the owner.

chown -v :kulin myfile
changed ownership of 'myfile' from kulin:root to :kulin
Enter fullscreen mode Exit fullscreen mode

The last step is to list the new ownership information.

ls -l myfile
-rw-r--r-- 1 kulin kulin 0 Jan  1 22:36 myfile
Enter fullscreen mode Exit fullscreen mode

To view the only group of the owner you can use the stat command as follows.

stat -c "The group name of the owner is %G for the file %n" myfile
The group name of the owner is kulin for the file myfile
Enter fullscreen mode Exit fullscreen mode

To change both user and group at the same time follow the next example.

chown -v kulin:kulin myfile
Enter fullscreen mode Exit fullscreen mode

To change the ownership of a directory and all its contents recursively, use the -R option as demonstrated in the next example.

chown -R username:groupname directory
Enter fullscreen mode Exit fullscreen mode

Check out the man pages for more info.

man chown
Enter fullscreen mode Exit fullscreen mode

This article is part of my book:

Master Linux Permissions and File Types While Your Coffee Brews


Top comments (0)