Linux File Permission-reading, writing and executing

Linux File Permission-reading, writing and executing

Β·

9 min read

In this article we will explore about how Access Management works on linux operating system.

In the below image,

a user ankita cannot create a directory "doc" as the reason specified that "permission denied".

similarly user sagar cannot create a file "sample.txt" as the reason stated that "permission denied" .

why this is happening?πŸ€” Is something wrong?

Being a multi-user Operating System having limited file permissions is sometimes difficult and can be challenging for someone who is new to Linux/Unix operating systems.

Users and Groups in Linux

Before understanding permissions, we should understand the file ownerships.

In Linux file permissions system, there are three types of owners associated with a file or a directory.

  1. Owner - owner referred as the user who created the file or a directory. Simply the owner has full privileges and permissions over the file, including the changing of mode(permissions) of a file or a directory.

  2. Group - Every user is the part of some group(s). so the users belonging to that particular group of a file or a directory have permissions to perform actions on the file.

  3. other - Other are everyone else!. Any users that are not part of the user or group classes belong to this class. The permissions indicate what action all other users can perform on the file.

Viewing the file permissions

You can view the permissions of files and directories by using ls command with option -al .

Understanding the permission indicators

Here is how to understand the output of the ls -al command.

At the top,"Total 40 " represents the total number of files and directories present.

now coming down, there is symbolic representation using a combination of letters: d, r, w and x.

To understand this representation, let's start one by one.

πŸ“ŒFile Type - The very first character indicates the file types.

CharacterType of file
-A regular file
dA directory
lA symbolic link. Its a file system object that points to another file system object.
bA block special file. This file type refers to a device that handles data in blocks such as hard drives, DVD.
cA character special file. This file type refers to a device that handles data as a stream of bytes at a time.

πŸ“ŒPermissions - In the same column starting from second character there are total nine characters, three sets of characters either dash(-) or letters, three times, indicating permissions for owner, group and others.

  • the r represents the read permissions.

  • the w represents the write permissions.

  • the x represents the execution permissions.

  • the - represents explicitly no permissions .

let's understand these three triplets by taking one example of our directory cricket .

here

  1. The first three characters belongs to owner. In our case it sets to rwx that means the owner has full permissions to read, write and execute.

  2. The next three characters after the owner triad belongs to group. Here it sets to r-x which implies that the users belonging to that group will have read and execute permissions.

  3. The final three characters after the group triad belongs to other. Here it sets to r-x which means that all other users on the system has read and write permission over that directory.

πŸ“ŒLink count - Second column belongs to the number of hard links to that file or directory. For e.g. file mi.txt having 1 hardlink and directory sport is having 2 hardlink. let's find out link information for demo_file as ls output shows it as

πŸ“ŒOwner - Third column belongs to owner who owns the file or directory. For eg - for file mi.txt the owner is root and similarly for directory sport also the owner is root only.

πŸ“ŒGroup - And fourth column belongs to Group, the members of that group only will have the access to this file or directory. Only one group can be the owner of a file or directory at a time.

For eg - the root's group has the permission to access the file mi.txt which implies the users of the group "root" are the owner for file.

πŸ“ŒFile size - Fifth column belongs to file size. Here size of files is described in bytes.

πŸ“ŒModification Time/Date - Sixth column belongs to Time/Date. It shows the last modified date and time of that file or directory.

πŸ“ŒFilename - Obviously, the name of that file or directory.

Reading, writing and Executing

File permissions have a different meaning depending on the file type. The combinations of following character have different effects, depending on whether they are set to a file or to a directory.

Character

Effect on files

Effect on directories

r

Allows a file to be opened and read. User's can't modify the file.

allows the user to read the directory contents without any modification in the directory.

w

Allows a file to be written to or modified, but does not allows a file to be deleted or renamed.

allows the directory contents to be modified, (You can create new files; rename or delete existing files and directory) if only if the executes permissions is also set otherwise the permissions has no effect.

x

allows a file to be treated as program and executed.

allows a directory to access details about files in the directory. Directory contents can be accessed with cd .

Examples of Permissions in Linux

Now we understand file permissions. Let's see some examples.

-rwx------ A regular file that is readable, writable and executable by file's owner. No one else has any access.

drwxrwx--- A directory that can be modified and executed by its owner and group.

-rw------- A regular file that is readable and writable by file's owner. No one else has any access.

-rw-rw---- A regular file that is readable and writable by file's owner and members of the file's group owner only.

-rw-r--r-- A regular file that is readable and writable by file's owner. Member of the file's owner group read the file. The file is world readable.

-rwxr-xr-x A regular file that is readable, writable and executable by file's owner. The file is readable and executable by everybody else.

How do I change the permissions?

There is a command named chmod in Linux which is used to change the permissions of file or a directory. Only the file's owner and superuser can change the mode(permissions) of a file or a directory.

There are two methods of changing file permissions using chmod.

1.Octal number representation

2.Symbolic representation

Octal number representation

Each permission is assigned a value as the following table show, and the total of each set of permissions provides a number for that set.

NumberOctal permission representationSet
0No permission---
1Execute permission--x
2write permission-w-
3Execute and write permission; 1 (execute) + 2 (write) = 3-wx
4Read permissionr--
5Read and execute permission; 4 (read) + 1 (execute) = 5r-x
6Read and write permission; 4 (read) + 2 (write) = 6rw-
7All permission ; 4 (read) + 2 (write) + 1 (execute) = 7rwx

The syntax :-

$ chmod <number> <file or directory>

let understand the permissions by taking an example of file sample.txt. Running ls -l on the sample.txt shows the file's permission as below,

here sample.txt is a regular file, having read + write permissions for owner, read permission for group and read permission for other.

  • Assign read + write + execute permissions to owner, read + write permissions to group and read + execute permissions to others on sample.txt .

  • Assign read + write + execute permissions to owner, write permissions to group and no permission permissions to others on sample.txt .

Symbolic Representation

The easiest way for a beginner to modify file or directory permission is to use the symbolic mode.

To specify who is affected, a combination of the characters u, g, o, and a is used.

symbolrepresentation
ustands for "user" but represents the owner of file or a directory.
gstands for "group", represents the members of that specified group only.
ostands for "others" except user and group
astands for "all", represent the combination of u, g and o.

With symbolic permission you can add, delete or specify the Permissions you want by using operators in the following table;

operatorspecifications
+Add the specified permissions to a file or directory
-Remove the specified permissions to a file or directory
=Sets the specified current permissions to a file or directory.

let understand the permissions by taking an example of file ugoa_file. Running ls -l on the ugoa_file shows the file's permission as below,

  • Assign read + write + execute permissions to owner.

  • Remove write + execute permissions from others.

here you can give permissions to u, g and o iby combining the operators in single line,

  • Assign read + write permissions to owner, read + write + execute permissions to group and execute permissions toothers.

  • Assign write permissions to owner, read + write + execute permissions to group and remove execute permissions from others.

  • If no permission input is given after "=" then all the permission will be removed for that class.

Changing Ownership and Groups

while creating an account in Linux, it assigns a owner ID, group ID and groups to each user.

chown

The chown command stands for "change owner" used to change the owner and group owner of a file or directory. Superuser privileges are required to use this command.

The syntax of chown are as follows:

$ chown <owner>:<group> file/directory

  • How do I change the owner associated with samplefile ?

$ chown <username> <filename>

Earlier the owner of "samplefile" was "root" now its changes to user "ankita".

  • How do I change the group associated with samplefile?

$ chown :<groupname> <filename>

similarly samplefile ,the group owner was "root" but now changes to "linux" .

  • How do I change the owner and group at the same time for samplefile?

$ chown <username>:<groupname> <filename>

chgrp

In older versions of Unix, the chown command changed only file ownership, not group ownership. A specific command, chgrp was used for changing group owner of a file or directory.

The syntax are as follows:

$ chgrp <groupname> <filename>

I prefer to memorize single command chown as it applicable to both user and group changing ownerships rather to memorize chown for the user and chgrp for the group.

πŸ¦‹Conclusion

Though we came to end of this article, I hope this article has helped you to understand how Linux file permissions work and how can we change permissions.

Hope you like this article. So Stay Tuned for the next article .

Thank you. Happy learning!πŸ“

And also don't forget to like and share this article.😎

Β