This is the third article from a series compiled from my notes while taking the LPI exams.
File ownership is a very important security aspect of the Linux operating system. 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 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:


Top comments (0)