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 h…


This content originally appeared on DEV Community and was authored by Nedim Hadzimahmutovic

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

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

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

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

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

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

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

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

Next, we change the group of the owner.

chown -v :kulin myfile
changed ownership of 'myfile' from kulin:root to :kulin

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

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

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

chown -v kulin:kulin myfile

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

Check out the man pages for more info.

man chown

This article is part of my book:

Master Linux Permissions and File Types While Your Coffee Brews


This content originally appeared on DEV Community and was authored by Nedim Hadzimahmutovic


Print Share Comment Cite Upload Translate Updates
APA

Nedim Hadzimahmutovic | Sciencx (2025-01-19T13:52:11+00:00) Master Linux File Ownership While Your Coffee Brews. Retrieved from https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/

MLA
" » Master Linux File Ownership While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx - Sunday January 19, 2025, https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/
HARVARD
Nedim Hadzimahmutovic | Sciencx Sunday January 19, 2025 » Master Linux File Ownership While Your Coffee Brews., viewed ,<https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/>
VANCOUVER
Nedim Hadzimahmutovic | Sciencx - » Master Linux File Ownership While Your Coffee Brews. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/
CHICAGO
" » Master Linux File Ownership While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx - Accessed . https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/
IEEE
" » Master Linux File Ownership While Your Coffee Brews." Nedim Hadzimahmutovic | Sciencx [Online]. Available: https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/. [Accessed: ]
rf:citation
» Master Linux File Ownership While Your Coffee Brews | Nedim Hadzimahmutovic | Sciencx | https://www.scien.cx/2025/01/19/master-linux-file-ownership-while-your-coffee-brews/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.