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 2013 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/rajesh
-rw-r--r-- 1 root root 424 Dec 10 2013 corpora/stopwords/rajesh
# chown
raghu:altair corpora/stopwords/rajesh
# ls -l
corpora/stopwords/rajesh
-rw-r--r-- 1 raghu altair 424 Dec 10 2012 corpora/stopwords/rajesh
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 2013 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 2013 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/rajesh/
total 12
-rw-rw-r-- 1 rajesh javaproject 0 Aug 19 2013 file1
drwxrwxr-x 2 rajesh rajesh 4096 Aug 19 2013 hello
# chown -R raghu
/home/rajesh/
# ls -l /home/rajesh/
total 12
-rw-rw-r-- 1 raghu javaproject 0 Aug 19 2013 file1
drwxrwxr-x 2 raghu rajesh 4096 Aug 19 2013 hello
Verbose output
The --verbose option
shows all the ownership changing. It outputs the diagnostics for each file
processed
# chown -R
--verbose rajesh /home/rajesh/
changed ownership of `/home/rajesh/hello' to rajesh
changed ownership of `/home/rajesh/.emacs' to rajesh
changed ownership of `/home/rajesh/.bash_history' to rajesh
changed ownership of `/home/rajesh/.bash_logout' to rajesh
changed ownership of `/home/rajesh/.bashrc' to rajesh
changed ownership of `/home/rajesh/file1' to rajesh
changed ownership of `/home/rajesh/.mozilla/plugins' to rajesh
changed ownership of `/home/rajesh/.mozilla/extensions' to rajesh
changed ownership of `/home/rajesh/.mozilla' to rajesh
changed ownership of `/home/rajesh/.bash_profile' to rajesh
changed ownership of `/home/rajesh/' to rajesh
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 rajesh /home/rajesh/
ownership of `/home/rajesh/hello' retained as rajesh
ownership of `/home/rajesh/.emacs' retained as rajesh
ownership of `/home/rajesh/.bash_history' retained as rajesh
ownership of `/home/rajesh/.bash_logout' retained as rajesh
ownership of `/home/rajesh/.bashrc' retained as rajesh
ownership of `/home/rajesh/file1' retained as rajesh
ownership of `/home/rajesh/.mozilla/plugins' retained as rajesh
ownership of `/home/rajesh/.mozilla/extensions' retained as rajesh
ownership of `/home/rajesh/.mozilla' retained as rajesh
ownership of `/home/rajesh/.bash_profile' retained as rajesh
ownership of `/home/rajesh/' retained as rajesh