68

In some examples, I saw that some used chown instead of chmod. I do not know where to use chmod and chown. Please explain to me the difference between them, when and why I should use either.

3
  • 23
    Nobody's made it explicit yet: one changes the mode and the other changes the owner. Also, read the manual.
    – OrangeDog
    May 24, 2017 at 17:33
  • 2
    And while chown can also change the group, there is chgrp dedicated specifically to this task.
    – OrangeDog
    May 25, 2017 at 14:17
  • 6
    What's the main difference between a banana and a chair? Don't sit on the banana. May 27, 2017 at 23:48

6 Answers 6

117

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: enter image description here

6
  • 2
    The owner cannot do whatever they want to the file - deleting or moving requires write permissions on the containing directory.
    – muru
    May 24, 2017 at 14:05
  • 3
    @muru that's true ;) I meant read/write/exec, there's also chattr which can effects the file I guess.
    – Ravexina
    May 24, 2017 at 14:13
  • 3
    @KamilMaciorowski I haven't asked you to pretend anything. What you're missing is that most people don't think of deleting or renaming a file as being operations on the directory, but as operations on the file. That's why we have questions like these: superuser.com/q/373115/334516, unix.stackexchange.com/q/48579/70524, askubuntu.com/q/240424/158442 Obviously you know these are operations on the directory (congratulations!), but there's a common misconception about that, so I noted that renaming or deleting involve permissions on the directory, not the file. ...
    – muru
    May 26, 2017 at 4:43
  • 2
    I too upvoted just for the comic :-) However, you may want to expand on chmod. The reason it's not called chperm (other than early UNIX bods not liking long command names) is that it's for other things as well (setuid, setgid, stick bits, etc). Hence the name "change mode".
    – user9184
    May 26, 2017 at 5:03
  • @paxdiablo Thanks :-) The reason I didn't explain the details in my answer is that I was trying to show just enough difference to get the comic point. I did an update ;)
    – Ravexina
    May 26, 2017 at 7:49
74

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.

5
  • 1
    +1 for image.. No octal def though? Or chmod a+w example? Which is time consuming to do but gets more votes I think :D May 24, 2017 at 12:32
  • 2
    @WinEunuuchs2Unix haha thanks... I deliberately chose a particularly useless permissions setting. If I really start explaining when and why to use every possible permutation of chmod and chown (which is too broad a question by far) my answer will go on forever. Trying to keep it simple
    – Zanna
    May 24, 2017 at 12:35
  • confused what's a difference between ownership and permissions? is the difference that only owner can update permission? Dec 30, 2019 at 15:31
  • @MuhammadUmer ownership is who owns the file, permissions are what owners and non owners can do with it
    – Zanna
    Dec 31, 2019 at 17:30
  • It seems like the link to the article is broken, here is the fixed one: this short guide Sep 18, 2020 at 7:49
22

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:

  1. the owner of the file
  2. users who are members of the group owning the file
  3. everybody else

chown is used to change the first two. chmod is used to change the rights granted to these types.

0
10

A few points/tips to add to the previous excellent answers:

  • You must have execute permission on a directory (and all those above it) to access it
  • Use the -R option to apply the changes recursively to everything including and below the target, e.g. chown -R www-data files/
  • You can change user and group at the same time by using the syntax chown user:group myfile
  • Often it's easier/better to use the '+' and '-' options rather than setting absolute permissions, especially when dealing with a combination of files and directories. For example if you want to give group write permissions to a directory and everything below it, chmod -R 775 files/ would make 'files/' and everything in and below it executable and readable to all, which might not be the desired outcome for every file. Using chmod -R g+w files/ will just add the group write permission without touching anything else.
9

Very good answers already, but I would like to make a contribution where the permissions is very easy to understand

chmod u=r+w,o=r-w,g=-r-w test.php

u = user
o = other
g = group

This way you can easily append permissions to a file. In the example above

user = read + write
other = read but not write
group = not read not write

And don't forget the -R if you want to change permissions recursively.

5

chown Will change who owns the file and what group it belongs, while chmod changes how the owners and groups can access the file (or if they can access it at all).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .