{"id":2269,"date":"2014-06-09T11:16:08","date_gmt":"2014-06-09T11:16:08","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=2269"},"modified":"2014-06-09T11:19:23","modified_gmt":"2014-06-09T11:19:23","slug":"linux-change-file-ownership-user","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/linux-change-file-ownership-user\/","title":{"rendered":"Linux &#8211; Change file ownership from one user to another"},"content":{"rendered":"<p>It had always been a common chore for me to hop in a update file permissions on a directory when a project changed hands or for any number of other reasons.<\/p>\n<p>But this week, I was tasked with removing an entire user from the server. I know that Linux will handle the file ownership situation somewhat gracefully if I simply remove the user. Though, that just doesn&#8217;t seem very clean in my opinion.<\/p>\n<p>I much preferred to reassign the permissions for that user to another user, then remove their account.<\/p>\n<h3>Reassigning the User<\/h3>\n<p>I started by having a look at the man pages for <strong>chown<\/strong>. Pretty straightforward if you make use of the <em>&#8211;from<\/em> flag.<\/p>\n<pre>chown --from=oldguy newguy * -R<\/pre>\n<p>Easy as pie right. So, let&#8217;s do the group. On most Linux servers that I&#8217;ve helped administer, if one user manages a directory solo, we also assign the directory to the primary group that is created for a user.<\/p>\n<p>Here we go! Crap, there&#8217;s no <em>&#8211;from<\/em> available on the <strong>chgrp<\/strong> command. Now what?<\/p>\n<p>Though I can&#8217;t imagine it, I&#8217;m sure there&#8217;s a pretty good reason for not providing a<em> &#8211;from<\/em> flag. It just seems to common an operation to overlook.<\/p>\n<h3>Reassigning the Group<\/h3>\n<p>Luckily, we can leverage Linux&#8217;s built-in find command to loop over files conditionally based on their group id.<\/p>\n<p>Group id, what&#8217;s that? If you&#8217;re familiar with relational databases, you&#8217;re no doubt aware of how most records in a database have a numeric primary key to reference them. So, a users table might, for example contain three fields:<\/p>\n<p><em>id, username, firstname, lastname<\/em><\/p>\n<p>In the case of Linux, the same thing kinda happens under the hood. Except, only the id and username are stored.<\/p>\n<p>To find the user id of a user or group id of a group, simple view the contents of the \/etc\/passwd or \/etc\/group files. You&#8217;ll likely need to have root privileges to do this.<\/p>\n<pre>cat \/etc\/passwd\r\ncat \/etc\/group<\/pre>\n<p>We&#8217;re only concerned with the group id here, since the <strong>chown<\/strong> command made our lives easy. Once you know the group id, we&#8217;ll use the find command to loop over all files that are identified as being owned the specific group id you provide.<\/p>\n<pre>find . -gid 512 -exec chgrp newgroup {} +<\/pre>\n<p>That&#8217;s it!<\/p>\n<p>As a final note, if you are removing a user completely, don&#8217;t forget to remove them from any groups in the \/etc\/group file, and also to remove their user account from the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Changing file and group ownership from one user or group to another is a common requirement in the server admin world, here are two quick tips on how to go about it.<\/p>\n","protected":false},"author":1,"featured_media":2271,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[41],"tags":[261,141],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2269"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=2269"}],"version-history":[{"count":4,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":2274,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2269\/revisions\/2274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/2271"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}