How to Use Permissions and Ownership in Unix

In Unix, every file and directory has a set of permissions and ownership rules that control who can access or modify them. Understanding this is crucial for managing your system securely and effectively.

In this blog, we’ll explore what permissions are, how to check them, and how to change them—all explained in simple terms.


What Are Permissions in Unix?

Permissions determine who can do what with a file or directory. There are three actions (or rights) that permissions control:

  1. Read (r): Permission to view the contents of a file or list a directory’s contents.
  2. Write (w): Permission to modify or delete a file or directory.
  3. Execute (x): Permission to run a file (if it’s a script or program) or enter a directory.

Each file or directory has permissions for three types of users:

  • Owner: The person who created the file.
  • Group: A set of users who share access.
  • Others: Everyone else on the system.

How to Check Permissions

Use the ls -l command to view permissions for files and directories.

Example:

$ ls -l
-rw-r--r-- 1 user group 1200 Nov 14 08:30 notes.txt
drwxr-xr-x 2 user group 4096 Nov 14 09:00 Documents

What does this mean?

  • The first column (-rw-r--r--) shows the permissions:
    • The first character: - for files, d for directories.
    • The next three characters (rw-): Permissions for the owner.
    • The middle three (r--): Permissions for the group.
    • The last three (r--): Permissions for others.

For the notes.txt file:

  • The owner can read (r) and write (w) but not execute (-).
  • The group and others can only read (r).

How to Change Permissions

The chmod command lets you change permissions.

Method 1: Using Symbols

Use +, -, or = to add, remove, or set permissions:

  • Add execute permission for the owner:
$ chmod u+x notes.txt
  • Remove write permission for others:
$ chmod o-w notes.txt
  • Set group permissions to read-only:
$ chmod g=r notes.txt

Method 2: Using Numbers (Octal Notation)

Each permission is represented by a number:

  • r = 4, w = 2, x = 1, and - = 0.

Combine them to set permissions:

  • 7 = read (4) + write (2) + execute (1)
  • 6 = read (4) + write (2)
  • 5 = read (4) + execute (1)
  • 4 = read (4)

Example:

  • Grant full permissions to the owner, read-only to the group, and none to others:
$ chmod 740 notes.txt

How to Change Ownership

The chown command changes the owner or group of a file.

  1. Change the owner:
$ sudo chown newuser notes.txt
  1. Change the group:
$ sudo chown :newgroup notes.txt
  1. Change both owner and group:
$ sudo chown newuser:newgroup notes.txt

    Practice Time!

    1. Create a new file:
    $ touch myfile.txt
    1. Check its permissions:
    $ ls -l myfile.txt
    1. Modify the permissions to:
      • Allow only the owner to read and write.Deny access to everyone else.
    $ chmod 600 myfile.txt

      Summary

      • Use ls -l to view permissions.
      • Use chmod to change permissions (symbolically or numerically).
      • Use chown to change ownership.

      Permissions and ownership are essential for maintaining security in Unix. In the next blog, we’ll learn about managing files and directories, building on these concepts.

      Leave a Reply

      Your email address will not be published. Required fields are marked *