Never Ending Security

It starts all here

How To use Chown Command Examples on Linux / Mac OS X

HowTo: Chown Command Examples

The concept of owner and groups for files is fundamental to Linux. Every file is associated with an owner and a group. You can use chown and chgrp commands to change the owner or the group of a particular file or directory.

In this article, we will discuss the ‘chown’ command as it covers most part of the ‘chgrp’ command also.

Even if you already know this command, probably one of the examples mentioned below might be new to you.

Change the owner of a file

# ls -lart file
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 20:03 file

# chown root file

# ls -l file
-rw-r--r-- 1 root punks 0 2014-10-22 20:03 file

So we see that the owner of the file was changed from ‘cyberpunk’ to ‘root’.

Change the group of a file

Through the chown command, the group (that a file belongs to) can also be changed.

# ls -l file
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 20:03 file

# chown :others file

# ls -l file
-rw-r--r-- 1 cyberpunk others 0 2014-10-22 20:03 file

If you observe closely, the group of the file changed from ‘punks’ to ‘others’. So we see that by just adding a ‘:’ followed by the new group name, the group of the file can be changed.

Change both owner and the group

# ls -l file
-rw-r--r-- 1 root punks 0 2014-10-22 20:03 file

# chown cyberpunk:others file

# ls -l file
-rw-r--r-- 1 cyberpunk others 0 2014-10-22 20:03 file

So we see that using the syntax ‘<newOwner>:<newGroup>’, the owner as well as group can be changed in one go.

Using chown command on symbolic link file

Here is a symbolic link :

# ls -l file_symlnk
lrwxrwxrwx 1 cyberpunk punks 7 2014-10-22 20:03 file_symlnk -> file

So we see that the symbolic link ‘file_symlink’ links to the file ‘file’.

Lets see what happens if chown command is issued on a symbolic link:

# chown root:others file_symlnk

# ls -l file_symlnk
lrwxrwxrwx 1 cyberpunk punks 7 2014-10-22 20:03 file_symlnk -> file

# ls -l file
-rw-r--r-- 1 root others 0 2014-10-22 20:03 file

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 ‘file’ whose owner and group got changed. This is the default behavior of the chown command. Also, there exists a flag ‘–dereference’ for the same.

Using chown command to forcefully change the owner/group of symbolic file.

Using flag ‘-h’, you can forcefully change the owner or group of a symbolic link as shown below.

# ls -l file_symlnk
lrwxrwxrwx 1 cyberpunk punks 7 2014-10-22 20:03 file_symlnk -> file

# chown -h root:others file_symlnk

# ls -l file_symlnk
lrwxrwxrwx 1 root others 7 2014-10-22 20:03 file_symlnk -> file

Change owner only if a file is owned by a particular user

Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.

# ls -l file
-rw-r--r-- 1 root others 0 2014-10-22 20:03 file

# chown --from=guest cyberpunk file

# ls -l file
-rw-r--r-- 1 root others 0 2014-10-22 20:03 file

# chown --from=root cyberpunk file

# ls -l file
-rw-r--r-- 1 cyberpunk others 0 2014-10-22 20:03 file
  • In the example above, we verified that the original owner/group of the file ‘file’ was root/others.
  • Next we used the ‘–from’ flag to change the owner to ‘cyberpunk’ but only if the existing owner is ‘guest’.
  • Now, as the existing owner was not ‘guest’. So, the command failed to change the owner of the file.
  • Next we tried to change the owner if the existing owner is ‘root’ (which was true) and this time command was successful and the owner was changed to ‘cyberpunk’.

On a related note, if you want to change the permission of a file, you should use chmod command.

If you are a beginner, you should start by reading the basics of file permissions.

Change group only if a file already belongs to a certain group

Here also the flag ‘–from’ is used but in the following way:

# ls -l file
-rw-r--r-- 1 cyberpunk others 0 2014-10-22 20:03 file

# chown --from=:others :punks file

# ls -l file
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 20:03 file

Since the file ‘file’ actually belonged to group ‘others’ so the condition was correct and the command was successful.

So we see that by using the flag ‘–from=:<conditional-group-name>’ we can change the group under a particular condition.

NOTE: By following the template ‘–from=<conditional-owner-name>:<conditional-group-name>’, condition on both the owner and group can be applied.

Copy the owner/group settings from one file to another

This is possible by using the ‘–reference’ flag.

# ls -l file
-rwxr-xr-x 1 cyberpunk punks 8968 2014-10-09 07:10 file

# ls -l file
-rw-r--r-- 1 root others 0 2014-10-22 20:03 file

# chown --reference=file file

# ls -l file
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 20:03 file

In the above example, we first checked the owner/group of the reference-file ‘file’ and then checked the owner/group of the target-file ‘file’. Both were different.  Then we used the chown command with the ‘–reference’ 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 ‘file’ were made similar to the ‘file’.

Change the owner/group of the files by traveling the directories recursively

This is made possible by the ‘-R’ option.

# ls -l Temp/file1
-rw-r--r-- 1 root others 0 2014-10-22 21:52 Temp/file1

# ls -l Temp/folder1/sub-file1
-rw-r--r-- 1 root others 0 2014-10-22 21:52 Temp/folder1/sub-file1

# ls -l Temp/folder2/sub-file2
-rw-r--r-- 1 root others 0 2014-10-22 21:52 Temp/folder2/sub-file2

# chown -R cyberpunk:punks 2014/

# ls -l Temp/folder2/sub-file2
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 21:52 Temp/folder2/sub-file2

# ls -l Temp/folder1/sub-file1
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 21:52 Temp/folder1/sub-file1

# ls -l Temp/file1
-rw-r--r-- 1 cyberpunk punks 0 2014-10-22 21:52 Temp/file1

So we see that after checking the owner/group of all the files in the directory ‘Temp’ and its two sub-directories ‘folder1’ and ‘folder2’.  We issued the chown command with the ‘-R’ option to change both the owner and group. The command was successful and owner/group of all the files was changed successfully.

Using chown command on a symbolic link directory

Lets see what happens if we issue the ‘chown’ command to recursively change the owner/group of files in a directory that is a symbolic link to some other directory.

Here is a symbolic link directory ‘linux_symlnk’ that links to the directory ‘Temp’ (already used in example ’9′ above) :

$ ls -l linux_symlnk
lrwxrwxrwx 1 cyberpunk punks 6 2014-10-22 22:02 linux_symlnk -> Temp/

Now, lets change the owner (from cyberpunk to root) of this symbolic link directory recursively :

# chown -R root:others linux_symlnk

# ls -l linux_symlnk/
-rw-r--r-- 1 cyberpunk punks    0 2014-10-22 21:52 file1
drwxr-xr-x 2 cyberpunk punks 4096 2014-10-22 21:52 folder2
drwxr-xr-x 2 cyberpunk punks 4096 2014-10-22 21:52 folder1

In the ouput above we see that the owner of the files and directories was not changed. This is because by default the ‘chown’ command cannot traverse a symbolic link. This is the default behavior but there is also a flag ‘-P’ for this.

Using chown to forcefully change the owner/group of a symbolic link directory recursively

This can be achieved by using the flag -H

# chown -R -H guest:punks linux_symlnk

# ls -l linux_symlnk/
total 8
-rw-r--r-- 1 guest punks    0 2014-10-22 21:52 file1
drwxr-xr-x 2 guest punks 4096 2014-10-22 21:52 folder2
drwxr-xr-x 2 guest punks 4096 2014-10-22 21:52 folder1

So we see that by using the -H flag, the owner/group of all the files/folder were changed.

List all the changes made by the chown command

Use the verbose option -v, which will display whether the ownership of the file was changed or retained as shown below.

# chown -v -R guest:others Temp
changed ownership of `Temp/folder2/sub-file2' to guest:others
changed ownership of `Temp/folder2' retained to guest:others
ownership of `Temp/folder2_sym' retained as guest:others
ownership of `Temp/folder1_sym' retained as guest:others
changed ownership of `Temp/file1' to guest:others
changed ownership of `Temp/folder1/sub-file1' to guest:others
ownership of `Temp/folder1' retained as guest:others
ownership of `Temp' retained as guest:others
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s