Using chown to change ownership of files and directories

Controlling access to files and directories really just boils down to ensuring that the proper users can access their own files and directories and that each file and directory has permissions set in such a way that only authorized users can access them. The chown utility covers the first part of this equation.

One unique thing about chown is that you must have sudo privileges to use it, even if you're working with your own files in your own directory. You can use it to change the user of a file or directory, the group that's associated with a file or directory, or both at the same time. 

First, let's say that you own the perm_demo.txt file and that you want to change both the user and group association to that of another user. In this case, I'll change the file ownership from me to maggie:

[donnie@localhost ~]$ ls -l perm_demo.txt
-rw-rw-r--. 1 donnie donnie 0 Nov 5 20:02 perm_demo.txt

[donnie@localhost ~]$ sudo chown maggie:maggie perm_demo.txt

[donnie@localhost ~]$ ls -l perm_demo.txt
-rw-rw-r--. 1 maggie maggie 0 Nov 5 20:02 perm_demo.txt
[donnie@localhost ~]$

The first maggie in maggie:maggie is the user that you want to grant ownership to. The second maggie, after the colon, represents the group that you want the file to be associated with. Since I was changing both the user and the group to maggie, I could have left off the second maggie, with the first maggie followed by a colon, and I would have achieved the same result:

sudo chown maggie: perm_demo.txt

To just change the group association without changing the user, just list the group name, preceded by a colon:

[donnie@localhost ~]$ sudo chown :accounting perm_demo.txt

[donnie@localhost ~]$ ls -l perm_demo.txt
-rw-rw-r--. 1 maggie accounting 0 Nov 5 20:02 perm_demo.txt
[donnie@localhost ~]$

Finally, to just change the user without changing the group, list the username without the trailing colon:

[donnie@localhost ~]$ sudo chown donnie perm_demo.txt

[donnie@localhost ~]$ ls -l perm_demo.txt
-rw-rw-r--. 1 donnie accounting 0 Nov 5 20:02 perm_demo.txt
[donnie@localhost ~]$

These commands work the same way on a directory as they do on a file. However, if you also want to change the ownership and/or the group association of the contents of a directory, while also making the change on the directory itself, use the -R option, which stands for recursive. In this case, I just want to change the group for the perm_demo_dir directory to accounting. Let's see what we have to begin with:

[donnie@localhost ~]$ ls -ld perm_demo_dir
drwxrwxr-x. 2 donnie donnie 74 Nov 5 20:17 perm_demo_dir

[donnie@localhost ~]$ ls -l perm_demo_dir
total 0
-rw-rw-r--. 1 donnie donnie 0 Nov 5 20:17 file1.txt
-rw-rw-r--. 1 donnie donnie 0 Nov 5 20:17 file2.txt
-rw-rw-r--. 1 donnie donnie 0 Nov 5 20:17 file3.txt
-rw-rw-r--. 1 donnie donnie 0 Nov 5 20:17 file4.txt

Now, let's run the command and look at the results:

[donnie@localhost ~]$ sudo chown -R :accounting perm_demo_dir

[donnie@localhost ~]$ ls -ld perm_demo_dir
drwxrwxr-x. 2 donnie accounting 74 Nov 5 20:17 perm_demo_dir

[donnie@localhost ~]$ ls -l perm_demo_dir
total 0
-rw-rw-r--. 1 donnie accounting 0 Nov 5 20:17 file1.txt
-rw-rw-r--. 1 donnie accounting 0 Nov 5 20:17 file2.txt
-rw-rw-r--. 1 donnie accounting 0 Nov 5 20:17 file3.txt
-rw-rw-r--. 1 donnie accounting 0 Nov 5 20:17 file4.txt
[donnie@localhost ~]$

That's all there is to chown.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.111.85