{"id":31232,"date":"2022-09-12T20:01:18","date_gmt":"2022-09-12T20:01:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31232"},"modified":"2022-12-23T05:47:25","modified_gmt":"2022-12-23T05:47:25","slug":"linux-tutorials-chown-command-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/linux-tutorials-chown-command-example\/","title":{"rendered":"Linux Tutorials: chown command &#038; example"},"content":{"rendered":"\n<p>The command chown, an abbreviation of change owner, is used on Unix and Unix-like operating systems to change the owner of file system files, directories. Unprivileged users who wish to change the group membership of a file that they own may use chgrp.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chmod-and-chown-command-Linux.jpg\" alt=\"\" class=\"wp-image-31233\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chmod-and-chown-command-Linux.jpg 800w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chmod-and-chown-command-Linux-300x188.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chmod-and-chown-command-Linux-768x480.jpg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"165\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chown-change-owner-file.png\" alt=\"\" class=\"wp-image-31234\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chown-change-owner-file.png 729w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/09\/chown-change-owner-file-300x68.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Change the owner of a file<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -lart tmpfile\n-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile\n\n# chown root tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>So we see that the owner of the file was changed from \u2018himanshu\u2019 to \u2018root\u2019.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Change the group of a file<\/h3>\n\n\n\n<p>Through the chown command, the group (that a file belongs to) can also be changed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile\n-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile\n\n# chown :friends tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>If you observe closely, the group of the file changed from \u2018family\u2019 to \u2018friends\u2019. So we see that by just adding a \u2018:\u2019 followed by the new group name, the group of the file can be changed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Change both owner and the group<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile\n-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile\n\n# chown himanshu:friends tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>So we see that using the syntax \u2018&lt;newOwner&gt;:&lt;newGroup&gt;\u2019, the owner as well as group can be changed in one go.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Using chown command on symbolic link file<\/h3>\n\n\n\n<p>Here is a symbolic link :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile_symlnk\nlrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -&gt; tmpfile<\/pre>\n\n\n\n<p>So we see that the symbolic link \u2018tmpfile_symlink\u2019 links to the file \u2018tmpfile\u2019.<\/p>\n\n\n\n<p>Lets see what happens if chown command is issued on a symbolic link:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># chown root:friends tmpfile_symlnk\n\n# ls -l tmpfile_symlnk\nlrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -&gt; tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>When the chown command was issued on symbolic link to change the owner as well as the group then its the referent of the symbolic link ie \u2018tmpfile\u2019 whose owner and group got changed. This is the default behavior of the chown command. Also, there exists a flag \u2018\u2013dereference\u2019 for the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Using chown command to forcefully change the owner\/group of symbolic file.<\/h3>\n\n\n\n<p>Using flag \u2018-h\u2019, you can forcefully change the owner or group of a symbolic link as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile_symlnk\nlrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -&gt; tmpfile\n\n# chown -h root:friends tmpfile_symlnk\n\n# ls -l tmpfile_symlnk\nlrwxrwxrwx 1 root friends 7 2012-05-22 20:03 tmpfile_symlnk -&gt; tmpfile<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Change owner only if a file is owned by a particular user<\/h3>\n\n\n\n<p>Using chown \u201c\u2013from\u201d flag, you can change the owner of a file, only if that file is already owned by a particular owner.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile\n-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile\n\n# chown --from=guest himanshu tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile\n\n# chown --from=root himanshu tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>In the example above, we verified that the original owner\/group of the file \u2018tmpfile\u2019 was root\/friends.<\/li><li>Next we used the \u2018\u2013from\u2019 flag to change the owner to \u2018himanshu\u2019 but only if the existing owner is \u2018guest\u2019.<\/li><li>Now, as the existing owner was not \u2018guest\u2019. So, the command failed to change the owner of the file.<\/li><li>Next we tried to change the owner if the existing owner is \u2018root\u2019 (which was true) and this time command was successful and the owner was changed to \u2018himanshu\u2019.<\/li><\/ul>\n\n\n\n<p>On a related note, if you want to change the permission of a file, you should use&nbsp;<a href=\"https:\/\/www.thegeekstuff.com\/2010\/06\/chmod-command-examples\/\" target=\"_blank\" rel=\"noopener\">chmod command<\/a>.<\/p>\n\n\n\n<p>If you are a beginner, you should start by reading the&nbsp;<a href=\"https:\/\/www.thegeekstuff.com\/2010\/04\/unix-file-and-directory-permissions\/\" target=\"_blank\" rel=\"noopener\">basics of file permissions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Change group only if a file already belongs to a certain group<\/h3>\n\n\n\n<p>Here also the flag \u2018\u2013from\u2019 is used but in the following way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l tmpfile\n-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile\n\n# chown --from=:friends :family tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>Since the file \u2018tmpfile\u2019 actually belonged to group \u2018friends\u2019 so the condition was correct and the command was successful.<\/p>\n\n\n\n<p>So we see that by using the flag \u2018\u2013from=:&lt;conditional-group-name&gt;\u2019 we can change the group under a particular condition.<\/p>\n\n\n\n<p>NOTE: By following the template \u2018\u2013from=&lt;conditional-owner-name&gt;:&lt;conditional-group-name&gt;\u2019, condition on both the owner and group can be applied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. Copy the owner\/group settings from one file to another<\/h3>\n\n\n\n<p>This is possible by using the \u2018\u2013reference\u2019 flag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l file\n-rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file\n\n# ls -l tmpfile\n-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile\n\n# chown --reference=file tmpfile\n\n# ls -l tmpfile\n-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile<\/pre>\n\n\n\n<p>In the above example, we first checked the owner\/group of the reference-file \u2018file\u2019 and then checked the owner\/group of the target-file \u2018tmpfile\u2019. Both were different.&nbsp; Then we used the chown command with the \u2018\u2013reference\u2019 option to apply the owner\/group settings from the reference file to the target file. The command was successful and the owner\/group settings of \u2018tmpfile\u2019 were made similar to the \u2018file\u2019.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. Change the owner\/group of the files by traveling the directories recursively<\/h3>\n\n\n\n<p>This is made possible by the \u2018-R\u2019 option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ls -l linux\/linuxKernel\n-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux\/linuxKernel\n\n# ls -l linux\/ubuntu\/ub10\n-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux\/ubuntu\/ub10\n\n# ls -l linux\/redhat\/rh7\n-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux\/redhat\/rh7\n\n# chown -R himanshu:family linux\/\n\n# ls -l linux\/redhat\/rh7\n-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux\/redhat\/rh7\n\n# ls -l linux\/ubuntu\/ub10\n-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux\/ubuntu\/ub10\n\n# ls -l linux\/linuxKernel\n-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux\/linuxKernel<\/pre>\n\n\n\n<p>So we see that after checking the owner\/group of all the files in the directory \u2018linux\u2019 and its two sub-directories \u2018ubuntu\u2019 and \u2018redhat\u2019.&nbsp; We issued the chown command with the \u2018-R\u2019 option to change both the owner and group. The command was successful and owner\/group of all the files was changed successfully.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. Using chown command on a symbolic link directory<\/h3>\n\n\n\n<p>Lets see what happens if we issue the \u2018chown\u2019 command to recursively change the owner\/group of files in a directory that is a symbolic link to some other directory.<\/p>\n\n\n\n<p>Here is a symbolic link directory \u2018linux_symlnk\u2019 that links to the directory \u2018linux\u2019 (already used in example \u20189\u2019 above) :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls -l linux_symlnk\nlrwxrwxrwx 1 himanshu family 6 2012-05-22 22:02 linux_symlnk -&gt; linux\/<\/pre>\n\n\n\n<p>Now, lets change the owner (from himanshu to root) of this symbolic link directory recursively :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># chown -R root:friends linux_symlnk\n\n# ls -l linux_symlnk\/\n-rw-r--r-- 1 himanshu friends&nbsp;&nbsp;&nbsp; 0 2012-05-22 21:52 linuxKernel\ndrwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 redhat\ndrwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 ubuntu<\/pre>\n\n\n\n<p>In the ouput above we see that the owner of the files and directories was not changed. This is because by default the \u2018chown\u2019 command cannot traverse a symbolic link. This is the default behavior but there is also a flag \u2018-P\u2019 for this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. Using chown to forcefully change the owner\/group of a symbolic link directory recursively<\/h3>\n\n\n\n<p>This can be achieved by using the flag -H<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># chown -R -H guest:family linux_symlnk\n\n# ls -l linux_symlnk\/\ntotal 8\n-rw-r--r-- 1 guest family&nbsp;&nbsp;&nbsp; 0 2012-05-22 21:52 linuxKernel\ndrwxr-xr-x 2 guest family 4096 2012-05-22 21:52 redhat\ndrwxr-xr-x 2 guest family 4096 2012-05-22 21:52 ubuntu<\/pre>\n\n\n\n<p>So we see that by using the -H flag, the owner\/group of all the files\/folder were changed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. List all the changes made by the chown command<\/h3>\n\n\n\n<p>Use the verbose option -v, which will display whether the&nbsp;ownership&nbsp;of the file was changed or retained as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># chown -v -R guest:friends linux\nchanged ownership of `linux\/redhat\/rh7' to guest:friends\nchanged ownership of `linux\/redhat' retained to guest:friends\nownership of `linux\/redhat_sym' retained as guest:friends\nownership of `linux\/ubuntu_sym' retained as guest:friends\nchanged ownership of `linux\/linuxKernel' to guest:friends\nchanged ownership of `linux\/ubuntu\/ub10' to guest:friends\nownership of `linux\/ubuntu' retained as guest:friends\nownership of `linux' retained as guest:friends<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The command chown, an abbreviation of change owner, is used on Unix and Unix-like operating systems to change the owner of file system files, directories. Unprivileged users who wish to&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-31232","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31232"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31232\/revisions"}],"predecessor-version":[{"id":31235,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31232\/revisions\/31235"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}