{"id":5937,"date":"2024-06-25T01:49:11","date_gmt":"2024-06-25T05:49:11","guid":{"rendered":"https:\/\/www.both.org\/?p=5937"},"modified":"2024-06-16T21:09:39","modified_gmt":"2024-06-17T01:09:39","slug":"intro-to-the-linux-chown-command","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=5937","title":{"rendered":"Intro to the Linux chown command"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"5937\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\">Image by: Opensource.com CC-by-SA 4.0<\/p>\n\n\n\n<p>Every file and directory on a Linux system is owned by someone, and the owner has complete control to change or delete the files they own. In addition to having an owning <em>user<\/em>, a file has an owning <em>group<\/em>.<\/p>\n\n\n\n<p>You can view the ownership of a file using the <strong>ls -l<\/strong> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;elon@workstation Downloads]$ ls -l\ntotal 2454732\n-rw-r--r--. 1 elon elon 1934753792 Jul 25 18:49 Fedora-Workstation-Live-x86_64-40-1.14.iso<\/code><\/pre>\n\n\n\n<p>The third and fourth columns of the output are the owning user and group, which together are referred to as <em>ownership<\/em>. Both are <strong>elon<\/strong> for the ISO file above.<\/p>\n\n\n\n<p>The ownership settings, set by the <a href=\"https:\/\/www.both.org\/wp-admin\/post.php?post=5926\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>chmod<\/strong> command<\/a>, control\u00a0who is allowed to perform read, write, or execute actions. You can change ownership (one or both) with the <strong>chown<\/strong> command.<\/p>\n\n\n\n<p>It is often necessary to change ownership. Files and directories can live a long time on a system, but users can come and go. Ownership may also need to change when files and directories are moved around the system or from one system to another.<\/p>\n\n\n\n<p>The ownership of the files and directories in my home directory are my user and my primary group, represented in the form <strong>user:group<\/strong>. Suppose Susan is managing the Delta group, which needs to edit a file called <strong>team_podman_notes<\/strong>. You can use the <strong>chown<\/strong> command to change the user to <strong>susan<\/strong> and the group to <strong>delta<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown susan:delta team_podman_notes\nls -l\n-rw-rw-r--. 1 susan delta 0 Aug  1 12:04 mynotes<\/code><\/pre>\n\n\n\n<p>Once the Delta group is finished with the file, it can be assigned back to me:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown alan team_podman_notes\n$ ls -l mynotes\n-rw-rw-r--. 1 alan delta 0 Aug  1 12:04 mynotes<\/code><\/pre>\n\n\n\n<p>Both the user and group can be assigned back to me by appending a colon (<strong>:<\/strong>) to the user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown alan: team_podman_notes\n$ ls -l mynotes\n-rw-rw-r--. 1 alan alan 0 Aug  1 12:04 mynotes<\/code><\/pre>\n\n\n\n<p>By prepending the group with a colon, you can change just the group. Now members of the <strong>gamma<\/strong> group can edit the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown :gamma team_podman_notes\n$ ls -l\n-rw-rw-r--. 1 alan gamma 0 Aug  1 12:04 mynotes<\/code><\/pre>\n\n\n\n<p>A few additional arguments to chown can be useful at both the command line and in a script. Just like many other Linux commands, chown has a recursive argument(<strong>-R<\/strong>) which tells the command to descend into the directory to operate on all files inside. Without the<strong> -R <\/strong>flag, you change permissions of the folder only, leaving the files inside it unchanged. In this example, assume that the intent is to change permissions of a directory and all its contents. Here I have added the <strong>-v<\/strong> (verbose) argument so that chown reports what it is doing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -l . conf\n.:\ndrwxrwxr-x 2 alan alan 4096 Aug  5 15:33 conf\n\nconf:\n-rw-rw-r-- 1 alan alan 0 Aug  5 15:33 conf.xml\n\n$ chown -vR susan:delta conf \nchanged ownership of 'conf\/conf.xml' from alan:alan to  susan:delta\nchanged ownership of 'conf' from alan:alan to  susan:delta<\/code><\/pre>\n\n\n\n<p>Depending on your role, you may need to use <strong>sudo<\/strong> to change ownership of a file.<\/p>\n\n\n\n<p>You can use a reference file (<strong>&#8211;reference=RFILE<\/strong>) when changing the ownership of files to match a certain configuration or when you don&#8217;t know the ownership (as might be the case when running a script). You can duplicate the user and group of another file (<strong>RFILE<\/strong>, known as a reference file), for example, to undo the changes made above. Recall that a dot (<strong>.<\/strong>) refers to the present working directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown -vR --reference=. conf<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Report Changes<\/h2>\n\n\n\n<p>Most commands have arguments for controlling their output. The most common is <strong>-v<\/strong> (-<strong>-verbose<\/strong>) to enable verbose, but chown also has a <strong>-c<\/strong> (<strong>&#8211;changes<\/strong>) argument to instruct chown to only report when a change is made. Chown still reports other things, such as when an operation is not permitted.<\/p>\n\n\n\n<p>The argument <strong>-f<\/strong> (<strong>&#8211;silent<\/strong>, <strong>&#8211;quiet<\/strong>) is used to suppress most error messages. I will use <strong>-f<\/strong> and the <strong>-c<\/strong> in the next section so that only actual changes are shown.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preserve root<\/h2>\n\n\n\n<p>The root (<strong>\/<\/strong>) of the Linux filesystem should be treated with great respect. If a mistake is made at this level, the consequences could leave a system completely useless. Particularly when you are running a recursive command that makes any kind of change or worse: deletions. The chown command has an argument that can be used to protect and preserve the root. The argument is <strong>&#8211;preserve-root<\/strong>. If this argument is used with a recursive chown command on the root, nothing is done and a message appears instead.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown -cfR --preserve-root alan \/\nchown: it is dangerous to operate recursively on '\/'\nchown: use --no-preserve-root to override this failsafe<\/code><\/pre>\n\n\n\n<p>The option has no effect when not used in conjunction with <strong>&#8211;recursive<\/strong>. However, if the command is run by the root user, the permissions of the <strong>\/<\/strong> itself will be changed, but not of other files or directories within.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chown -c --preserve-root alan \/\nchown: changing ownership of '\/': Operation not permitted\n&#091;root@localhost \/]# chown -c --preserve-root alan \/\nchanged ownership of '\/' from root to alan<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Ownership is security<\/h2>\n\n\n\n<p>File and directory ownership is part of good information security, so it&#8217;s important to occasionally check and maintain file ownership to prevent unwanted access. The chown command is one of the most common and important in the set of Linux security commands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Image by: Opensource.com CC-by-SA 4.0 Every file and directory on a Linux system is owned by someone, and<\/p>\n","protected":false},"author":429,"featured_media":3182,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,407,90,5,75],"tags":[453,454,261],"class_list":["post-5937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-file-management","category-in-depth","category-linux","category-security","tag-chown","tag-file-ownership","tag-security"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5937"}],"version-history":[{"count":6,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5937\/revisions"}],"predecessor-version":[{"id":5977,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5937\/revisions\/5977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3182"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}