Linux Change File / Directory Ownership - Chown Command

September 5, 2013 | By
| Reply More

The files, directories and processes (which are again files) in Linux are owned by users. They have a group owner. The owner and group ownership is important as the security of files through permissions (the DAC) is set on owner, group owner and others. The chown command can change the ownership and group ownership of a file.

Linux chown command

Change Ownership

To change the ownership of a file, chown is provided with two arguments, the new owner and the file whose owner is to be changed.

# ls -l corpora/stopwords/english
-rw-r--r-- 1 root root 623 Dec 10 2012 corpora/stopwords/english

# chown raghu corpora/stopwords/english

# ls -l corpora/stopwords/english
-rw-r--r-- 1 raghu root 623 Dec 10 2012 corpora/stopwords/english

The root user is the owner of the file, chown commands makes raghu user the new owner.

Changing owner and group

If the owner is followed by a colon and a group name (without spaces), the group name is changed as well.

# ls -l corpora/stopwords/danish
-rw-r--r-- 1 root root 424 Dec 10 2012 corpora/stopwords/danish

# chown raghu:altair corpora/stopwords/danish

# ls -l corpora/stopwords/danish
-rw-r--r-- 1 raghu altair 424 Dec 10 2012 corpora/stopwords/danish

Now the new owner of the file is raghu and the new group owner is altair group.

Now, in this syntax involving colon, if a colon but no group name follows the user name, the given user is made the owner of the file and that user's login group is made as the group owner of the file.

# ls -l corpora/stopwords/dutch
-rw-r--r-- 1 root root 453 Dec 10 2012 corpora/stopwords/dutch

# chown raghu: corpora/stopwords/dutch

# ls -l corpora/stopwords/dutch
-rw-r--r-- 1 raghu raghu 453 Dec 10 2012 corpora/stopwords/dutch

If the colon and group are mentioned, only the group of the file is changed. In this case, the command works like chgrp command.

# ls -l corpora/stopwords/finnish
-rw-r--r-- 1 root root 1579 Dec 10 2012 corpora/stopwords/finnish

# chown :altair corpora/stopwords/finnish

# ls -l corpora/stopwords/finnish
-rw-r--r-- 1 root altair 1579 Dec 10 2012 corpora/stopwords/finnish

Changing permissions for directories recursively

The ownership of the directories and files contained in them can be changed recursively with -R option.

# ls -l /home/jones/
total 12
-rw-rw-r-- 1 jones javaproject 0 Aug 19 2012 file1
drwxrwxr-x 2 jones jones 4096 Aug 19 2012 hello

# chown -R raghu /home/jones/

# ls -l /home/jones/
total 12
-rw-rw-r-- 1 raghu javaproject 0 Aug 19 2012 file1
drwxrwxr-x 2 raghu jones 4096 Aug 19 2012 hello

Verbose output

The --verbose option shows all the ownership changing. It outputs the diagnostics for each file processed

# chown -R --verbose jones /home/jones/
changed ownership of `/home/jones/hello' to jones
changed ownership of `/home/jones/.emacs' to jones
changed ownership of `/home/jones/.bash_history' to jones
changed ownership of `/home/jones/.bash_logout' to jones
changed ownership of `/home/jones/.bashrc' to jones
changed ownership of `/home/jones/file1' to jones
changed ownership of `/home/jones/.mozilla/plugins' to jones
changed ownership of `/home/jones/.mozilla/extensions' to jones
changed ownership of `/home/jones/.mozilla' to jones
changed ownership of `/home/jones/.bash_profile' to jones
changed ownership of `/home/jones/' to jones

The verbose option outputs processing of each file even when the changes are not made. But with -c or --changes option, the output is reported only when changes are made. For example,

# chown -R --verbose jones /home/jones/
ownership of `/home/jones/hello' retained as jones
ownership of `/home/jones/.emacs' retained as jones
ownership of `/home/jones/.bash_history' retained as jones
ownership of `/home/jones/.bash_logout' retained as jones
ownership of `/home/jones/.bashrc' retained as jones
ownership of `/home/jones/file1' retained as jones
ownership of `/home/jones/.mozilla/plugins' retained as jones
ownership of `/home/jones/.mozilla/extensions' retained as jones
ownership of `/home/jones/.mozilla' retained as jones
ownership of `/home/jones/.bash_profile' retained as jones
ownership of `/home/jones/' retained as jones

# chown -R -c jones /home/jones/
#

Silent operation

The normal users cannot change the ownership of files owned by others. So an error is displayed when a normal user tries to change the ownership.

[raghu@redhat-server ~]$ chown raghu /etc/
chown: changing ownership of `/etc/': Operation not permitted

But if we use -f or --silent or --quiet option, the error is not displayed.

[raghu@redhat-server ~]$ chown -f raghu /etc/
[raghu@redhat-server ~]$

Preserve root

To prevent changing the ownership of / directory recursively, --preserve-root is used.

[root@redhat-server ~]# chown -R --preserve-root raghu /
chown: changing ownership of `/proc/1/task/1/fd/10': Permission denied
chown: changing ownership of `/proc/1/task/1/fd': Permission denied
chown: changing ownership of `/proc/1/task/1/fdinfo/10': Permission denied
chown: changing ownership of `/proc/1/task/1/fdinfo': Permission denied
chown: changing ownership of `/proc/1/task/1/environ': Permission denied
chown: changing ownership of `/proc/1/task/1/auxv': Permission denied
chown: changing ownership of `/proc/1/task/1/status': Permission denied
<--output-truncated-->

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.