Explain Unix File Permissions
Upasana | May 05, 2019 | 3 min read | 76 views
-
What does specific unix permission means
-
How to change the file permissions
-
How to change the ownership of a file/directory
Unix file permissions are quite different than that of windows/MS DOS.
Unix Security Model
In unix security model, a user may own files and directories. Permissions in unix security model are granted to three different entities:
-
Owners
-
Group Members
-
Everyone else (the world)
ls -ltr rwx-xr-r 18 MunishChandel staff 576 Apr 30 11:14 orders
Permission Attributes
There are 3 different permission attributes r, x and w. These 3 attributes have the following effect on files and directories:
Attribute | Files | Directory |
---|---|---|
r |
Allows a file to be opened and read. |
Allows a directory’s contents to be listed if the execute attribute is also set. |
x |
Allows a file to be treated as a program and executed. Program files written in scripting languages must also be set as readable to be executed. |
Allows a directory to be entered, e.g., cd directory. |
w |
Allows a file to be written to or truncated, however this attribute does not allow files to be renamed or deleted. The ability to delete or rename files is determined by directory attributes. |
Allows files within a directory to be created, deleted, and renamed if the execute attribute is also set. |
Few real life examples of file permission attributes are listed in the below table.
Permission Attributes | Meaning |
---|---|
-rw------- |
Only File owner can read and write the file. Others have no access. |
-rwxr-rx-rx- |
A regular file that is readable, writable and executable by file owner. others can read and execute it. |
Changing file permissions: chmod
chmod command is used to change the permissions of a file. Only file’s owner or the superuser can change the mode of a directory/file. To gain ownership of a file or directory you would like to execute chown
command first.
There are broadly two distinct ways of changing file permissions using chmod:
-
symbolic representation
-
octal number representation
symbolic representation
We need to learn few more symbols other than r,w,x.
Symbol | Meaning |
---|---|
u |
file or directory owner |
g |
group owner |
o |
others/world |
a |
short for all of the above |
chmod needs two inputs:
-
who is affected (user/group/world/all)
-
what will be permissions (r/w/x)
chmod u+x foo.bar
chomd a+x foo.bar
chmod u-x foo.bar
Please be noted here that specifying +x or -x just sets a single attribute (x) without disturbing other attributes of file permission (r & w). The same is not possible in octal notation.
Octal notation for chmod
A single digit octal notation is enough to specify 3 different permission attributes (rwx) combination for an entity. Overall we need to specify 3 digit octal numbers to specify file mode for owner, group and world.
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 |
Few examples of octal notation:
- 600
-
owner has file mode 6 (rw), group and world has 0.
- 644
-
owner has file mode 6 (rw), group and world has read permission (4-4)
- 700
-
owner has file mode 7 (rwx), group and world have no permissions.
chmod 600 foo.bar
Top articles in this category:
- Morgan Stanley Java Interview Questions
- Sapient Global Market Java Interview Questions and Coding Exercise
- Top 50 Spring Interview Questions
- Cracking core java interviews - question bank
- Java Concurrency Interview Questions
- Goldman Sachs Java Interview Questions
- UBS Java Interview Questions