21

Many new users make mistakes (or have misunderstanding) while applying chmod on files or directories, because of a lack of informative knowledge of:

  • Symbolic notation for ugo and rwx
  • Use of octal numbers

So, in this answer I have provided some useful information that can help to understand correct symbolic notation and using octal numbers.

2
  • Maybe you meant that chmod +x [file name] works like chmod 111 [file name]
    – user579722
    Aug 9, 2016 at 7:52
  • chmod 111 affects read and write permissions while chmod +x doesn't. So, chmod +x turns 000 into 111. It turns 444 into 555. It turns 666 into 777. It turns 640 into 751. Oct 25, 2022 at 7:49

1 Answer 1

31

Every file and directory on Linux has an owner. Each file's owner can set different types of access (read, write, and execute) allowed for the owner themself, group owner of the file, and others (ie. any other user).

These levels of access for each file and directory are shown in the output of ls -l for any file or directory.

To read them, here's an example:

$ ls -l power.txt
-rw-r--r--  1 user user       4096 Aug  1 23:24  power.txt

The first 10 characters are called the file attributes. Of these 10:

  • The first character shows the type of file - or directory d or symbolic link l or character special c file or block special b file.
  • The rest of the 9 characters are called the file mode (or file permissions). These represent the read, write and execute permissions for the file's owner, the file's group owner and everybody else:
Owner Group World
rwx rwx rwx

Look at some of examples of file attributes to understand them better:

File Attributes Meaning of the Attributes
-rwx------ A regular file that is readable, writable, and executable by the file's owner. No one else has any access.
-rw------- A regular file that is readable and writable by the file's owner. No one else has any access.
-rw-r--r-- A regular file that is readable and writable by the file's owner. Members of the file's owner group may read the file. The file is world-readable.
-rwxr-xr-x A regular file that is readable, writable, and executable by the file's owner. The file may be read and executed by everybody else.
-rw-rw---- A regular file that is readable and writable by the file's owner and members of the file's group owner only.
lrwxrwxrwx A symbolic link. NOTE: All symbolic links have “dummy” permissions. The real permissions are kept with the actual file pointed to by the symbolic link.
drwxrwx--- A directory. The owner and the members of the owner group may enter the directory and, create, rename and remove files within the directory.
drwxr-x--- A directory. The owner may enter the directory and create, rename and delete files within the directory. Members of the owner group may enter the directory but cannot create, delete or rename files.

chmod

chmod sets the mode (permission) bits of a file or directory.

Syntax:

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...

chmod changes the file mode bits of each given file according to mode, which can be either

  1. a symbolic representation of changes to make,
  2. or an octal number representing the bit pattern for the new mode bits.

Symbolic Notation

The format of a symbolic mode is

[ugoa...][[-+=][perms...]...]

where,

  • ugoa - combination of letters controls which users' access to the file will be changed:
    1. u - the user who controls it
    2. g - other users in the file's group
    3. o - other users not in the file's group (means everyone else or the world).
    4. a - Or all users
    5. If none of these are given, the effect is as if a were given.
  • perms - is either zero or more letters from the set rwx, or a single letter from the set ugo
  • -+= -
    1. The + operator causes the selected file mode bits to be added to the existing file mode bits of each file
    2. - causes them to be removed
    3. = causes them to be added AND causes unmentioned bits to be removed EXCEPT that a directory's unmentioned set user and group ID bits are not affected

Multiple specifications may be separated by commas.

See some examples for clarity:

Notation Meaning
u+x Add execute permission for the owner.
u-x Remove execute permission from the owner.
+x Add execute permission for the owner, group, and world. Equivalent to a+x.
o-rw Remove the read and write permission from anyone besides the owner and group owner.
go=rw Set permissions for the group and others to have read and write permission. If either the group or others previously had execute permissions, they are removed.
u+x,go=rx Add execute permission for the owner and set the permissions for the group and others to read and execute.
o=u For a particular file, Set the mode bits of others to equal that of the user (file or directory owner) of the file
  • Using Octal number:

    Octal Binary File Mode
    0 000 ---
    1 001 --x
    2 010 -w-
    3 011 -wx
    4 100 r--
    5 101 r-x
    6 110 rw-
    7 111 rwx

Hence the following work the same:

chmod a=rwx [file_name]
chmod 777 [file_name]

And

chmod 775 [file_name]
chmod ug=rwx,o=rx [file_name]

Informative knowledge is provided with the help of The Linux Command-line as reference/source.

You can download pdf version of The Linux Command-line book from Sourceforge.

1
  • Would it be accurate to say that example chmod ug+rwx,o=rx [file_name] could be chmod a=rwx,o-w [file_name]? Would that be a bad practice? Jun 22, 2019 at 16:53

You must log in to answer this question.

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