Linux Commands – chown
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed chmod command in Linux which is used to modify a file or directory permissions.
https://cloudaffaire.com/linux-commands-chmod/
In this blog post, we will discuss chown command in Linux. chown command is used to change a file or directory owner or group or both. chown command changes the file/directory owner if only owner is provided or group if only group is provided. You can also change both owner and group at once using chmod new_owner:new_group file/directory command.
Note: A regular user cannot change a file or directory ownership by his own even if he is the owner of the file. You need to have root or sudo privileges to change a file or directory ownership. However, a regular user can change the group ownership of a file or directory provided he owns the file or directory and he is the member of the target group.
Linux Commands – chown:
You can use chown command to change a file or directory owner or group or both.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
############################ ## Linux Commands | chown ## ############################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------ ## chown ##------ ## chown [option]... [OWNER][:[GROUP]] FILE... ## chown [option]... --reference=RFILE FILE... ## chown command sudo groupadd mygroup1 ## create some groups sudo groupadd mygroup2 sudo useradd -g mygroup1 myuser1 ## create some users with group sudo useradd -g mygroup2 myuser2 sudo passwd myuser1 ## set password for users sudo passwd myuser2 mkdir mydir ## create a file and directory echo "hello" > myfile ls -l ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 1 09:15 mydir ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 1 09:15 myfile sudo chown myuser1:mygroup1 myfile ## chnage myfile owner and group at once using : sudo chown myuser2: mydir ## change mydir owner sudo chown :mygroup2 mydir ## change mydir group ls -l ## drwxrwxr-x 2 myuser2 mygroup2 6 Jun 1 09:15 mydir ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 1 09:15 myfile rm -rf my* |
You can use chown -c or –changes options to verbosely describe the action for each FILE whose ownership actually changes.
1 2 3 4 5 6 |
## chown -c or --changes options echo "hello" > myfile && ls -l myfile ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 1 09:17 myfile sudo chown -c myuser1:mygroup1 myfile ## changed ownership of ‘myfile’ from debjeet:cloudaffaire to myuser1:mygroup1 |
You can use chown -f or –silent or –quiet options to suppress printing of error messages about files whose ownership cannot be changed.
1 2 3 4 5 6 |
## chown -f or --silent or --quiet options chown myuser2:mygroup2 myfile ## a non privilaged user cannot transfer ownership to other ## chown: changing ownership of ‘myfile’: Operation not permitted chown -f myuser2:mygroup2 myfile ## returns nothing |
You can use chown –from=OLD-OWNER option to change a file or directory ownership, provided the OLD-OWNER is the current owner of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## chown --from=OLD-OWNER option sudo chown myuser1:mygroup1 myfile ls -l myfile ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 1 10:17 myfile sudo chown -v --from=myuser1 myuser2 myfile ## works as myfile current owner is myuser1 ls -l myfile ## -rw-rw-r-- 1 myuser2 mygroup1 6 Jun 1 10:17 myfile sudo chown -v --from=myuser1 myuser2 myfile ## does not works as myfile current owner is myuser2 ls -l myfile ## -rw-rw-r-- 1 myuser2 mygroup1 6 Jun 1 10:17 myfile rm -rf my* ## delete the files |
You can use chown –dereference option to not change the ownership of a symbolic link, instead follow the symbolic link and change the ownership of file or directory that the symbolic link refers to. This is the default behavior of chown and works the same without this option.
You can use chown -h or –no-dereference options to change the ownership of a symbolic link themselves instead of what they point to. This mode relies on the ‘lchown’ system call. On systems that do not provide the ‘lchown’ system call, ‘chown’ fails when a file specified on the command line is a symbolic link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
## chown --dereference option ## chown -h or --no-dereference options echo "hello" > myfile ## create a file and symbolic link to the file ln -s myfile mylink ls -l ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 1 11:03 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 1 11:03 mylink -> myfile sudo chown myuser1:mygroup1 mylink ## changes myfile ownership (default is to follow symbolic link) ls -l ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 1 11:03 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 1 11:03 mylink -> myfile sudo chown --dereference myuser1:mygroup1 mylink ## does same as above ls -l ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 1 11:03 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 1 11:03 mylink -> myfile sudo chown -h myuser2:mygroup2 mylink ## changes the ownership of the symbolic link itself ls -l ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 1 11:03 myfile ## lrwxrwxrwx 1 myuser2 mygroup2 6 Jun 1 11:03 mylink -> myfile rm -rf my* |
You can use chown –reference=REF_FILE option to change the user and group of each FILE to be the same as those of REF_FILE. If REF_FILE is a symbolic link, do not use the user and group of the symbolic link, but rather those of the file it refers to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## chown --reference=REF_FILE option touch myfile{1,2} ## create some files sudo chown myuser1:mygroup1 myfile1 ## change myfile1 ownership to myuser1 and mygroup1 ls -l ## -rw-rw-r-- 1 myuser1 mygroup1 0 Jun 1 12:05 myfile1 ## -rw-rw-r-- 1 debjeet cloudaffaire 0 Jun 1 12:05 myfile2 sudo chown --reference=myfile1 myfile2 ## set myfile2 ownership same as myfile1 ls -l ## -rw-rw-r-- 1 myuser1 mygroup1 0 Jun 1 12:05 myfile1 ## -rw-rw-r-- 1 myuser1 mygroup1 0 Jun 1 12:05 myfile2 rm -f my* |
You can use chown -R or –recursive options to recursively change ownership of directories and their contents. By default, chown changes only the ownership of directories, not their content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## chown -R or --recursive options mkdir -p mydir1/mydir2/mydir3 ## create some directories ls -lR ## drwxrwxr-x 3 debjeet cloudaffaire 20 Jun 1 13:01 mydir1 ## drwxrwxr-x 3 debjeet cloudaffaire 20 Jun 1 13:01 mydir1/mydir2 ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 1 13:01 mydir1/mydir2/mydir3 sudo chown myuser1:mygroup1 mydir1 ## only changes mydir1 ownership, not its content ownership ls -lR ## drwxrwxr-x 3 myuser1 mygroup1 20 Jun 1 13:01 mydir1 ## drwxrwxr-x 3 debjeet cloudaffaire 20 Jun 1 13:01 mydir1/mydir2 ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 1 13:01 mydir1/mydir2/mydir3 sudo chown -R myuser1:mygroup1 mydir1 ## changes ownership recursively ls -lR ## drwxrwxr-x 3 myuser1 mygroup1 20 Jun 1 13:01 mydir1 ## drwxrwxr-x 3 myuser1 mygroup1 20 Jun 1 13:01 mydir1/mydir2 ## drwxrwxr-x 2 myuser1 mygroup1 6 Jun 1 13:01 mydir1/mydir2/mydir3 |
You can use chown -v or –verbose options to output a diagnostic for every file processed. If a symbolic link is encountered during a recursive traversal on a system without the ‘lchown’ system call, and ‘–no-dereference’ is in effect, then issue a diagnostic saying neither the symbolic link nor its referent is being changed.
1 2 3 4 5 6 7 8 9 |
## chown -v or --verbose options sudo chown -Rv myuser2:mygroup2 mydir1 ## prints verbosely ## changed ownership of ‘mydir1/mydir2/mydir3’ from myuser1:mygroup1 to myuser2:mygroup2 ## changed ownership of ‘mydir1/mydir2’ from myuser1:mygroup1 to myuser2:mygroup2 ## changed ownership of ‘mydir1’ from myuser1:mygroup1 to myuser2:mygroup2 sudo rm -rf mydir1 |
You can use chown -H or -L or -P options with -R option to control the behavior of symbolic link in recursive mode if they refer to a directory.
- -H : follow the symbolic link provided as a command-line argument, but do not follow any other symbolic link during traversing
- -L : follow all symbolic links
- -P : Do not follow any symbolic link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
## chgrp -H or -L or -P options with -R option mkdir -p ~/mydir1/mydir11 ~/mydir2 ## create some files, directories and symbolic links ln -s ~/mydir1 mylink1 ln -s ~/mydir2 ~/mydir1/mylink2 echo "hello" > ~/mydir1/myfile1 echo "world" > ~/mydir2/myfile2 tree ## directory structure ## . ## ├── mydir1 ## │ ├── mydir11 ## │ ├── myfile1 ## │ └── mylink2 -> /home/debjeet/mydir2 ## ├── mydir2 ## │ └── myfile2 ## └── mylink1 -> /home/debjeet/mydir1 ls -Rl ## current permissions ## drwxrwxr-x 3 debjeet cloudaffaire 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 debjeet cloudaffaire 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:57 myfile2 sudo chown -RH myuser1:mygroup1 mylink1 ## follow the symbolic link provided in the argument ## but do not follow any other symbolic link there after ls -Rl ## drwxrwxr-x 3 myuser1 mygroup1 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 myuser1 mygroup1 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 myuser1 mygroup1 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 myuser1 mygroup1 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:57 myfile2 sudo chown -RL myuser2:mygroup2 mylink1 ## follow all symbolic links ls -Rl ## drwxrwxr-x 3 myuser2 mygroup2 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 myuser2 mygroup2 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 myuser2 mygroup2 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 myuser2 mygroup2 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 myuser2 mygroup2 6 Jun 2 02:57 myfile2 sudo chown -RP myuser1:mygroup1 mylink1 ## do not follow any symbolic link ls -Rl ## drwxrwxr-x 3 myuser2 mygroup2 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 myuser2 mygroup2 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 myuser1 mygroup1 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 myuser2 mygroup2 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 myuser2 mygroup2 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 myuser2 mygroup2 6 Jun 2 02:57 myfile2 rm -rf my* |
You can use chown –preserve-root option with -R option to prevent any attempt to recursively change the root directory (/). It’s a failsafe mechanism of OS to prevent recursively changing ownership in the root directory.
1 2 3 4 5 6 |
## chown --preserve-root option chown -cfR --preserve-root myuser1:mygroup1 / ## no changes, just for fun ## chown: it is dangerous to operate recursively on ‘/’ ## chown: use --no-preserve-root to override this failsafe |
You can use chown –no-preserve-root option with -R option to cancel the effect of any preceding ‘–preserve-root’ option. This is a dangerous command and should not be executed. The below example is given just for the demo, do not execute.
1 2 3 4 5 6 7 8 9 |
## chown --no-preserve-root option chown -R --no-preserve-root myuser1:mygroup1 / ## do not execute ## given just as an example sudo userdel -rf myuser1 ## cleanup sudo userdel -rf myuser2 sudo groupdel mygroup1 sudo groupdel mygroup2 |
Hope you have enjoyed this article. In the next blog post, we will discuss chgrp command in Linux.