Explain Unix File Permissions

Carvia Tech | May 05, 2019 | 3 min read | 27 views


  1. What does specific unix permission means

  2. How to change the file permissions

  3. 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:

  1. Owners

  2. Group Members

  3. Everyone else (the world)

unix permissions
Decoding the unix permissions
Consider the following:
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:

Permission Attributes
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 Examples
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.

changing file ownership using chown
sudo chown -R nexus:nexus /opt/oss

The above command recursively changes ownership of all files present in directory /opt/oss to user nexus.

There are broadly two distinct ways of changing file permissions using chmod:

  1. symbolic representation

  2. octal number representation

symbolic representation

We need to learn few more symbols other than r,w,x.

Symbol meanings
Symbol Meaning

u

file or directory owner

g

group owner

o

others/world

a

short for all of the above

chmod needs two inputs:

  1. who is affected (user/group/world/all)

  2. what will be permissions (r/w/x)

Add execute permission to the owner on foo.bar
chmod u+x foo.bar
Add execute permission to everyone (owner/group/world)
chomd a+x foo.bar
Remove execute permissions from owner
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 Notation for File Modes
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.

To set 600 on foo.bar
chmod 600 foo.bar

Top articles in this category:
  1. Morgan Stanley Java Interview Questions
  2. Cracking core java interviews - question bank
  3. Sapient Global Market Java Interview Questions and Coding Exercise
  4. BlackRock Top Java Interview Questions: Investment Banking Domain
  5. Top 20 Java Concurrency Interview Questions and Answers
  6. Citibank Java developer interview questions
  7. UBS Top 10 Java Interview Questions



Find more on this topic:
Java Interviews image
Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info