command: chmod

Discussion in 'Beginner Tutorials' started by gcawood, Nov 4, 2011.

  1. gcawood

    gcawood Administrator Staff Member

    Messages:
    49
    Likes Received:
    15
    Trophy Points:
    0
    chmod is a Linux command that will let you "set permissions" (aka, assign who can read/write/execute) on a file.

    Usage:
    Code:
     chmod permissions file 
    OR:
    Usage:
    Code:
     chmod permission1_permission2_permission3 file 
    When using chmod, you need to be aware that there are three types of Linux users that you are setting permissions for. Therefore, when setting permissions, you are assigning them for "yourself", "your group" and "everyone else" in the world. These users are technically know as:

    Therefore, when setting permissions on a file, you will want to assign all three levels of permissions, and not just one user.

    Think of the chmod command actually having the following syntax...
    Now that you understand that you are setting permissions for THREE user levels, you just have to wrap your head around what permissions you are able to set!

    There are three types of permissions that Linux allows for each file.

    Putting it all together:
    So, in laymen terms, if you wanted a file to be readable by everyone, and writable by only you, you would write the chmod command with the following structure.
    Wait! What are those numbers?!?
    Computers like numbers, not words. Sorry. You will have to deal with it. Take a look at the following output of `ls -l`
    Code:
    [root@demo]$ ls -l 
    -rw-r--r-- 1 gcawood iqnection 382 Dec 19 6:49 myDoc.txt 
    
    You will need to convert the word read or write or execute into the numeric equivalent (octal) based on the table below.

    Practical Examples

    Wait! I don't get it... there aren't enough permissions to do what I want!
    Good call. You need to add up the numbers to get other types of permissions...


    So, try wrapping your head around this!!
    7 = 4+2+1 (read/write/execute)
    6 = 4+2 (read/write)
    5 = 4+1 (read/execute)
    4 = 4 (read)
    3 = 2+1 (write/execute)
    2 = 2 (write)
    1 = 1 (execute)

    Good luck! Hope this helps.
    (ps, never set things to 777 unless you have a really good reason to do so.)
    jsina and diazsanc like this.
  2. tomfmason

    tomfmason New Member Staff Writer

    Messages:
    22
    Likes Received:
    6
    Trophy Points:
    0
    very nice write up. It is also work noting that you can use shortcuts like the following
    chmod ug+rw file
    chmod o-r file
    chmod ugo=rwx file
  3. enhu

    enhu New Member

    Messages:
    93
    Likes Received:
    4
    Trophy Points:
    0
    nice guide. thanks for reminding by the way.

    because every time i can't edit a file inside a directory, i just do chmod 777 * so i can edit files. :D the reason is that i don't exactly know which command to execute and its easier to me. but this is just something one would do if you know files aren't that important, the computer is yours and no one uses it except you. :D
  4. MustangV10

    MustangV10 New Member

    Messages:
    87
    Likes Received:
    3
    Trophy Points:
    0
    I've never CHMODD'ed via SSH..I always use the cPanel file manager (I have cPanel installed on my server). Guess it's important to know the 'get your hands dirty' method as well though.

    Thanks!
  5. mrnothersan

    mrnothersan New Member

    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    I don't usually CHMOD via SSH, unless there is a problem. I always use the cPanel file manager, or through an FTP client.
  6. sbircris.jr

    sbircris.jr New Member

    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ah this was one of the questions for my interview :D
    Thanks for this great post ... i`ll save it, because is very nicely explained
  7. lanlanlangzi

    lanlanlangzi New Member

    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    I have used it several times.It is very useful.:)
  8. Darwin

    Darwin New Member

    Messages:
    115
    Likes Received:
    3
    Trophy Points:
    0
    This would be for doing things from scratch, it is handy to know for noobs. I tend to use my CHMOD via SSH so having a general path to follow is handy even if this is a quick rundown of how things work.
  9. Stefano Messicano

    Stefano Messicano New Member

    Messages:
    88
    Likes Received:
    1
    Trophy Points:
    0
    Very nice tutorial! An example if you want to change the permissions of the file "participants" so that everybody has full access to it, you would enter:
    chmod 777 participants.
  10. sam300

    sam300 New Member

    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    chmod is a quite useful command, thanks for the description of chmod.
  11. forestplay

    forestplay New Member

    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Knowing chmod is very handy. It's second nature to me.

    The two most common values are

    755 - wide open for user, read and executable for everyone else
    644 - read and writable for user, readable by everyone else

    If you want to change one bit, use this variation:

    chmod u+x myfile

    This makes the file executable by the user who owns the file. There are variations of that too.

    chmod ug+x myfile (allow execute permission for user and group)
    chmod u+rwx myfile (allow read and write to user)
    chmod a-x myfile (disallow execute permission for all)

    u = user
    g = group
    o = other
    a = all

    r = read
    w = write
    x = execute

    '+' = allow permission
    '-' = disallow permission

    Sometimes using this syntax is easier than doing math. Have both techniques in your toolbox.

    -Bob
  12. dale

    dale Member

    Messages:
    115
    Likes Received:
    7
    Trophy Points:
    18
    On top of it, one could append "1" to the three digits for sticky, e.g.: chmod 1777 some_file
  13. vickyonl9

    vickyonl9 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    Can you actually explain what do you mean by " using CHMOD via SSH " ?

    If you are talking about logging into some other machine through SSH and then changing the permissions, can you please tell me , why do you find it unsafe to CHMOD using SSH? And also, can you tell me more about cPANEL?
  14. vickyonl9

    vickyonl9 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    what does sticky actually mean?
  15. dale

    dale Member

    Messages:
    115
    Likes Received:
    7
    Trophy Points:
    18
    Sticky means only owner can remove or rename the file or link.

    And after double checking to make sure, I realised that Linux does not recognise this use. So it is irrelevant to Linux. (I did not know this, so I also learnt something new. :))
  16. JimBobIII

    JimBobIII New Member

    Messages:
    27
    Likes Received:
    2
    Trophy Points:
    0
    I'm almost certain Linux machines can use the sticky bit; do you have a valid source that says otherwise?
  17. dale

    dale Member

    Messages:
    115
    Likes Received:
    7
    Trophy Points:
    18
    Wikipedia. And on reading it again, it seems like I have misread. Go me on Monday. :)

    So it seems like Linux ignores the traditional function of sticky bit, whilst retaining its effect on renaming and deletion?

    I think this explanation from Happy Hacker Digests sums it up nicely.

    Sorry for the confusion.
  18. JimBobIII

    JimBobIII New Member

    Messages:
    27
    Likes Received:
    2
    Trophy Points:
    0
    Ah, well that's significantly more correct-sounding. You learn something new every day, I suppose.
  19. ljepilo

    ljepilo New Member

    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Its not a problem ,now its much more clear :).
  20. linbgs

    linbgs New Member

    Messages:
    43
    Likes Received:
    1
    Trophy Points:
    0
    I learn something new everyday on here.
    Thanks for explaining, because I certainly was lost.

Share This Page