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
- a symbolic representation of changes to make,
- 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:
u
- the user who controls it
g
- other users in the file's group
o
- other users not in the file's group (means everyone else or the world).
a
- Or all users
- 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
-+=
-
- The
+
operator causes the selected file mode bits to be added to the existing file mode bits of each file
-
causes them to be removed
=
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.