Files and directories are owned by the user.
There are two notations of permissions, Symbolic Notation and Octal Notation.
Symbolic Notation:
- Read (r) : The read permission allows you to open and read the content of a file. But you can’t do any editing or modification in the file.
- Write (w) : The write permission allows you to edit, remove or rename a file. For instance, if a file is present in a directory, and write permission is set on the file but not on the directory, then you can edit the content of the file but can’t remove, or rename it.
- Execute (x): In Unix type system, you can’t run or execute a program unless execute permission is set.But in Windows, there is no such permission available.
Permission | On a File | On a directory |
---|---|---|
r (read) | read file content (cat) | read directory content (ls) |
w (write) | change file content (vi) | create file in directory (touch) |
x (execute) | execute the file | enter the directory (cd) |
- -rwxr–r–: A regular file whose user class has read/write/execute, group class has only read permissions, other class has only read permissions
- drw-rw-r–: A directory whose user class has read/write permissions, group class has read/write permissions, other class has only read permissions
- crwxrw-r–: A character special file whose user has read/write/execute permissions, group class has read/write permissions, other class has only read permissions
Octal notation
Octal (base-8) notation consists of at least 3 digits (sometimes 4, the left-most digit, which represents the setuid bit, the setgid bit, and the sticky bit).
Each of the three right-most digits are the sum of its component bits in the binary numeral system.
For example:
- The read bit (r in symbolic notation) adds 4 to its total
The write bit (w in symbolic notation) adds 2 to its totalThe execute bit (x in symbolic notation) adds 1 to its total
Chmod
The chmod command is used to change the permissions of a file or directory. To use it, we specify the desired permission settings and the file or files that we wish to modify.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here’s how it works:
Where
Now, if we represent each of the three sets of permissions (owner, group, and other) as a single digit, we have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set ‘dummy_file’ to have read and write permission for the owner, but wanted to keep the file private from others, here’s how we would do it
# chmod 600 dummy_file
Here is a table of numbers that covers all the common settings. The ones beginning with “7” are used with programs (since they enable execution) and the rest are for other kinds of files.
Value | Meaning |
---|---|
777 | (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting. |
755 | (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users. |
700 | (rwx——) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others. |
666 | (rw-rw-rw-) All users may read and write the file. |
644 | (rw-r–r–) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change. |
600 | (rw——-) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private. |
Changing File Ownership
We can change the owner of a file by using the chown command. Here’s an example: Suppose we wanted to change the owner of a ‘dummy_file’ from “me” to “new_user”
# sudo chown new_user dummy_file
Note: In order to change the owner of a file, you must be root user or have sudo privileges.
chown works the same way on directories as it does on files.
Changing Group Ownership
The group ownership of a file or directory may be changed with chgrp. This command is used like this:
In the example above, we changed the group ownership of dummy_file from its previous group to “new_group”. We must be the owner of the file or directory to perform a chgrp.
Use the chmod
command to set permissions for a directory or a file. The 2 examples below are setting permissions for a file, and for directory named dir
. This works in any linux distro.
Permission |
Command Examples |
Description
|
---|---|---|
rwx rwx rwx | chmod 777 filename chmod -R 777 dir |
Anybody can read, write, execute.
|
rwx rwx r-x | chmod 775 filename chmod -R 775 dir |
Owner & Group can read, write, execute. Everyone else can read, execute.
|
rwx rwx r– | chmod 774 filename chmod -R 774 dir |
Owner & Group can read, write, execute. Everyone else can read.
|
rwx r-x r-x | chmod 755 filename chmod -R 755 dir |
Owner can read, write, execute. Everyone else can read, execute.
|
rwx — — | chmod 700 filename chmod -R 700 dir |
Owner can read, write, execute. No one else has any rights.
|
rw- rw- rw- | chmod 666 filename chmod -R 666 dir |
Everyone can read, write.
|
rw- rw- r– | chmod 664 filename chmod -R 664 dir |
Owner & Group can read, write. Everyone else can read.
|
rw- r– r– | chmod 644 filename chmod -R 644 dir |
Owner can read, write. Everyone else can read.
|
For more on Linux file permission check out the LPI website.