Lecture # 20 - File Ownership and Permissions

Lecture # 20 - File Ownership and Permissions

File Ownership and Permissions in Linux

File Ownership:

  • Change User Ownership:

    To change the user ownership of a file sudo chown [user-name] [file-name] is used.

  • Change Group Ownership:

    To change group ownership of a file sudo chown :[group-name] [file-name] is used.

  • Change User and Group Ownership in one Command:

    To change user and group ownership in a single command sudo chown [user-name]:[group-name] [file-name] is used.

File Permissions:

Permission Categories:

There are three kind of permissions:

  • Read Permissions (r):

    -> For Files: Open and read the files.

    -> For Directories: View contents of directories.

  • Write Permissions (w):

    -> For Files: Edit, delete, and save files.

    -> For Directories: Modify its contents (create, delete, and rename files inside the directory) and modify the contents of files that has write permissions.

  • Execute Permissions (x):

    -> For Files: Run an executable script.

    -> For Directories: Access the directories and details about the files in that directory.

Types of Entities:

Permissions are granted to:

  • User (u):

    Owner of the file or directory.

  • Group (g):

    Group of users to have similar permissions.

  • Others (o):

    Anyone else other than owner and the group.

Permissions Representation:

Permissions are represented with 10 characters.

( - ) shows the file type.

Left most (r w x) is for the Owner User (u).

Middle (r w x) is for the Owner Group (g).

Right most (r w x) is for Others (o)*.*

Important Letters:

Changing Permissions:

  1. Symbolic Mode

We make a combination of letters and operators to set the permissions.

  • User Permissions:

    There are many ways to give permissions:

    Give read permission to the user: sudo chmod u+r [file-name]

    Give write permission to the user: sudo chmod u+w [file-name]

    Give execute permission to the user: sudo chmod u+x [file-name]

    Give read and write permissions to the user: sudo chmod u+rw [file-name]

    Give read and execute permissions to the user: sudo chmod u+rx [file-name]

    Give write and execute permissions to the user: sudo chmod u+wx [file-name]

    Give read, write, and execute permissions to the user: sudo chmod u+rwx [file-name]

    Remove read permission from the user: sudo chmod u-r [file-name]

    Remove write permission from the user: sudo chmod u-w [file-name]

    Remove execute permission from the user: sudo chmod u-x [file-name]

    Remove read and write permissions from the user: sudo chmod u-rw [file-name]

    Remove read and execute permissions from the user: sudo chmod u-rx [file-name]

    Remove write and execute permissions from the user: sudo chmod u-wx [file-name]

    Remove read, write, and execute permissions from the user: sudo chmod u-rwx [file-name]

  • Group Permissions:

    Give read permission to the group: sudo chmod g+r [file-name]

    Give write permission to the group: sudo chmod g+w [file-name]

    Give execute permission to the group: sudo chmod g+x [file-name]

    Give read and write permissions to the group: sudo chmod g+rw [file-name]

    Give read and execute permissions to the group: sudo chmod g+rx [file-name]

    Give write and execute permissions to the group: sudo chmod g+wx [file-name]

    Give read, write, and execute permissions to the group: sudo chmod g+rwx [file-name]

    Remove read permission from the group: sudo chmod g-r [file-name]

    Remove write permission from the group: sudo chmod g-w [file-name]

    Remove execute permission from the group: sudo chmod g-x [file-name]

    Remove read and write permissions from the group: sudo chmod g-rw [file-name]

    Remove read and execute permissions from the group: sudo chmod g-rx [file-name]

    Remove write and execute permissions from the group: sudo chmod g-wx [file-name]

    Remove read, write, and execute permissions from the group: sudo chmod g-rwx [file-name]

  • Others Permissions:

    Give read permission to others: sudo chmod o+r [file-name]

    Give write permission to others: sudo chmod o+w [file-name]

    Give execute permission to others: sudo chmod o+x [file-name]

    Give read and write permissions to others: sudo chmod o+rw [file-name]

    Give read and execute permissions to others: sudo chmod o+rx [file-name]

    Give write and execute permissions to others: sudo chmod o+wx [file-name]

    Give read, write, and execute permissions to others: sudo chmod o+rwx [file-name]

    Remove read permission from others: sudo chmod o-r [file-name]

    Remove write permission from others: sudo chmod o-w [file-name]

    Remove execute permission from others: sudo chmod o-x [file-name]

    Remove read and write permissions from others: sudo chmod o-rw [file-name]

    Remove read and execute permissions from others: sudo chmod o-rx [file-name]

    Remove write and execute permissions from others: sudo chmod o-wx [file-name]

    Remove read, write, and execute permissions from others: sudo chmod o-rwx [file-name]

  1. Octal Mode:

    We specify the permissions using three digit number. e.g. 764, where 7 is user ownership, 6 is group ownership, and 4 is for others.

    777: (rwx) for user, (rwx) for group, (rwx) for others. sudo chmod 777 [file-name]

    000: (---) for user, (---) for group, (---) for others. sudo chmod 000 [file-name]

    763: (rwx) for user, (rw-) for group, (-wx) for others, sudo chmod 763 [file-name]

    542: (r-x) for user, (r--) for group, (-w-) for others, sudo chmod 542 [file-name]

    There are multiple combinations for the octal mode. e.g. 765, 111, 124, 521, etc.