What is the main difference between chmod and chown?
In simple term
chown
is used to change the ownership of a file while chmod
is for changing the file mode bits.
chown
defines who owns the file.chmod
defines who can do what.
When you make someone the owner of a file, (s)he can do almost wherever (s)he want to that file, for example (s)he can use chmod
to changes its mods (say permissions) to define who can do what.
$ ls -l file
-rwxrwxr-x 2 ravexina admins 26 May 9 12:49 file
At the above line we can see that ravexina
is the owner of the file and admins
is the group. I can use: sudo chown dave:sudo file
to change the owner of the file to dave
and the group to sudo
; Now the file belongs to "dave" and everyone in "sudo" group.
However with chmod
we define who can do what? who has the right to read a file, write to a file or execute it. e.g:
chmod 777 file
gives the rights of read, write and execute to everyone including owner, group and everyones else.
From turnoff.us:
Let's create a file
touch rainbow
Let's have a look at the file's metadata
$ ls -l rainbow
-rw-rw-r-- 1 zanna zanna 0 May 24 10:09 rainbow
The first part of the information is the file type (-
at the beginning means it's a regular file) and the permission bits
After that we see the owner (zanna) and the group (zanna). We can use the chown
command to change those:
$ sudo chown pixie rainbow
$ ls -l rainbow
-rw-rw-r-- 1 pixie zanna 0 May 24 10:09 rainbow
And we use chmod
to change the permission bits
$ sudo chmod 333 rainbow
$ ls -l rainbow
--wx-wx-wx 1 pixie zanna 0 May 24 10:09 rainbow
Since the permission bits are set separately for owner, group and others, you can control file permissions for different users by combining chown
and chmod
. See this short guide for a crash course on permissions in Linux.
When considering the permissions of a file (or directory, or whatever), there are two factors:
- who owns the file - the user and group, and
- what they can do with it - read, write, execute or a combination thereof.
chown
deals with the who. chmod
deals with the what. You can't use one instead of the other.
Simple Unix permissions classify users trying to access a file into three types:
- the owner of the file
- users who are members of the group owning the file
- everybody else
chown
is used to change the first two. chmod
is used to change the rights granted to these types.