Linux: File Permissions

Linux: File Permissions

Hello there! Today, we're delving into the world of file permissions in Linux. But before we dive in, let me ask you a question

does that 'drwxrwxr-x' scare you? If so, fear not! After reading this blog, you'll have a solid understanding. Anyway, let's get started!

Linux is like a sibling to UNIX, a multi-user operating system that multiple people can use at the same time. It works on big computers like mainframes and servers without needing changes. But, there's a security worry – a sneaky or harmful user could mess with important data. To tackle this, Linux has two levels of authorization:

  • Ownership
  • Permission

Understanding Linux file ownership is crucial. In Linux, every file or directory has three types of owners: User, Group, and Other.

  1. User:

    • The person who creates a file is its owner by default.

    • Users are often referred to as owners.

  2. Group:

    • A user group can include multiple users.

    • All users in a group share the same Linux group permissions for a file. This is useful for projects where several people need access without assigning permissions individually.

  3. Other:

    • Any user with access to a file but doesn't own it or belong to the group.

    • Essentially, everyone else. Setting permissions for others is like setting permissions for the world.

Now, Let's talk about permissions.

Linux File Permissions:

Every file and directory in Linux has three permissions for the three owners mentioned above.

  • Read(r): Lets you open and read a file. On a directory, it allows listing its content.

  • Write(w): Authorizes modifying a file's content. For a directory, it allows adding, removing, and renaming files. However, if you have write permission on a file but not on the directory, you can modify the file but can't rename, move, or remove it.

  • Execute(x): In Linux, you can't run a program unless execute permission is set. If it's not set, you can still see/modify the program code (provided read & write permissions are set) but can't run it.

Understanding these permissions is crucial for defining user behavior in Linux.

Let’s see file permissions in Linux with examples:

ls -l

ls – l on terminal gives

Here, I have highlighted ‘drwxrwxr-x‘and this weird looking code is the one that tells us about the Linux permissions given to the owner, user group and others.

Here,

And

The characters are pretty easy to remember.

r = read permission
w = write permission
x = execute permission
= no permission

Let us look at it this way.

The first part of the code is ‘rw-‘. This suggests that the owner can:

  • Read the file

  • Write or edit the file

  • He cannot execute the file since the execute bit is set to ‘-‘.

The second part is ‘rw-‘. It for the user group and group-members can:

  • Read the file

  • Write or edit the file

The third part is for others which means any user. It says ‘r--‘. This means others can only:

  • Read the file

Changing file/directory permissions in Linux Using ‘chmod’ command

Say you do not want your colleague to see your personal images. This can be achieved by changing file permissions.

We can use the ‘chmod’ command which stands for ‘change mode’. Using the command, we can set permissions (read, write, execute) on a file/directory for the owner, group and the world.

Syntax:

chmod permissions filename

There are 2 ways to use the command –

  1. Absolute mode

  2. Symbolic mode

Absolute(Numeric) Mode in Linux

In this mode, file permissions are not represented as characters but a three-digit octal number.

The table below gives numbers for all for permissions types.

Let’s see the chmod permissions command in action.

In the above-given terminal window, we have changed the permissions of the file ‘file1 to ‘764’.

‘764’ absolute code says the following:

  • Owner can read, write and execute

  • Usergroup can read and write

  • World can only read

This is shown as ‘-rwxrw-r–

This is how you can change user permissions in Linux on file by assigning an absolute number.

Symbolic Mode in Linux

In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you can modify permissions of a specific owner. It makes use of mathematical symbols to modify the Linux file permissions.

The various owners are represented as –

Here's an example:

Changing Ownership and Group in Linux

For changing the ownership of a file/directory, you can use the following command:

chown user filename

In case you want to change the user as well as group for a file or directory use the command

chown user:group filename

In case you want to change group-owner only, use the command

chgrp group_name filename

chgrp’ stands for change group.

Well, that's all from me. Stay cool, stay humble. Have a nice day!