UNIX: advanced octal file permissions with chmod

Contributor Icon Contributed by qmchenry Date Icon September 19, 2004  
Tag Icon Tagged: UNIX

The chmod command in various UNIX flavors like Solaris, Linux, Mac OSX, and others, allows the access controls of a file or directory to be set. This recipe describes the more complex octal chmod syntax.


See the recipe Set UNIX file access permissions with chmod for the basics of file permissions and chmod. This recipe is for users familiar with these concepts.

The permissions for each user type can be represented by an octal value. Each type of permission carries with it a value:

4 r read
2 w write
1 x execute/cd

Putting these together in combination yields an octal number from 0 to 7. For example, read (4) and execute (1) permissions together are represented by 5 (4+1). Here is a table representing all of the possible values:

7 rwx read, write, execute
6 rw- read, write
5 r-x read, execute
4 r-- read
3 -wx write, execute
2 -w- write
1 --x execute
0 --- no permissions

This seems more complex than using the ug+rw notation covered in the recipe linked above. The character-based syntax is useful for simple changes in file permissions, but it provides only relative changes in state, such that the resulting state is dependent on the values before running chmod. In addition, common real-world requirements can make the character-based syntax very complex.

Using octal syntax for chmod allows setting the absolute permissions for owner, group, and other in one quick command. The syntax requires three octal digits, each representing the owner, group, and other permissions, respectively. For example, to set rwx (7) for owner, r-x (5) for group, and no permissions (0) for other, use the following chmod command:

chmod 750 file

The learning curve is a little steeper for the octal syntax, but the benefits are great, too.

Previous recipe | Next recipe |
 

Viewing 1 Comment

    • ^
    • v
    Unices and applications use the setgid and setuid bits so the octal may need to be 4 digits, especially if using UniCenter or AIX.
    AIX directories often have setgid set sometimes.

    Using chmod 0755 or 0750 if hardening is needed.
    remove setgid from dir with symbolic representaion
    drwxrws--- ie in octal 2750 change to safer nonsetgid
    drwxrwx--- ie in octal 0750
    with
    chmod 0750 dirname

    Usual ownership requirements apply.
    Note it is possible to get uppercase S appearing in ls -l output if setgid is set but no execute permissions are set. it means setgid is ineffective.
 
close Reblog this comment
blog comments powered by Disqus