Grant Read Write Access to Directory Linux

Past design, Linux is a multi-user operating organization. In an enterprise system, in that location would be multiple users accessing the same system. But if any user could access and modify all files belonging to other users or system files, this would certainly be a security chance.

This is why UNIX and thus Linux (Linux is a Unix-like system) has congenital-in security measure in place. This ensures that a file or directory can be accessed, modified or executed by merely desired users.

Which file would be accessed by which user is decided by 2 factors in Linux:

  • File ownership
  • File permission

Understanding file ownership and permission is crucial for a Linux user. I'll explain these terms in particular here.

File ownership in Linux

Note: I might use the term file hither only information technology is applicable to directories as well. I guess you know that directories are files anyway.

Every file and directory in Linux has three kinds of owners:

User

User is the possessor of the file. When yous create a file, you lot go the possessor of the file. The ownership can exist inverse besides, but nosotros'll see that afterward.

Group

Every user is part of a certain grouping(due south). A grouping consists of several users and this is one way to manage users in a multi-user environment.

For instance, if you have dev team, QA team and sysadmin squad accessing the aforementioned system, you should create separate groups for them. This style, you can manage files and security of the system effectively. It saves time considering instead of manually adding permission for each user, you can just add together them to a group and change the permission for the grouping. You'll meet how to do it afterward in this article.

Fifty-fifty if you are the only user of the arrangement, y'all'll still exist part of many groups. Distributions like Ubuntu also create a group with name same equally the user's name.

💡

Run the command groups to see what user-groups y'all belong to.

Other

'Other' can be considered as a super group with all the users on the system. Basically, anyone with access to the system belongs to this grouping.

In other words, 'User' is a single user, Grouping is a collection of users and Other consists of all the users on the arrangement.

File permissions in Linux

Every file and directory in Linux has the post-obit three permissions for all the three kinds of owners:

Permissions for files

  • Read – Tin can view or copy file contents
  • Write – Can modify file content
  • Execute – Can run the file (if its executable)

Permissions for directories

  • Read – Can list all files and copy the files from directory
  • Write – Can add or delete files into directory (needs execute permission too)
  • Execute – Tin can enter the directory

Agreement file permissions and ownership in Linux

Now that yous are aware of the bones terminology of file permissions and ownership, information technology's time to see it in action.

You can use the 'stat command' or the 'ls command' to check the file permissions.

If you employ the ls command with choice -l on a file, you'll see an output like this:

            -rwxrw-r-- 1 abhi itsfoss 457 Aug 10 xi:55 agatha.txt          

Let me explicate this output with a movie:

Linux file permission explained with ls command

Permit me farther explain the entire output in detail:

  • File type: Denotes the type of file. d means directory, – means regular file, l means a symbolic link.
  • Permissions: This field shows the permission assault a file. I'll explain it in particular in the next department.
  • Hard link count: Shows if the file has difficult links. Default count is i.
  • User: The user who owns the files.
  • Group: The grouping that has access to this file. Only 1 group can be the possessor of a file at a time.
  • File size: Size of the file in bytes.
  • Modification time: The appointment and time the file was last modified.
  • Filename: Plainly, the name of the file or directory.

Now that yous take understood the ls -l command output, permit's focus on the file permission office.

In the above command, you lot run across the file permission like this in the nine digit format:

            rwxrw-r--          

Each letter denotes a detail permission:

  • r : Read permission
  • westward : Write permission
  • ten : Execute permission
  • – : No permission set

Permissions are ever in the society of read, write and execute, i.due east., rwx. And then these permissions are set for all three kind of owners (see the ownership department) in the order of User, Group and Other.

This picture will explain things better:

Linux file permission explained

So, if you look at the above picture at present, you can say the post-obit things about the file permissions:

  • The file has read, write and execute permissions for the User owner. But who is this employ owner of the file? You accept this info in the output of ls -50 (i.east. user abhi).
  • The file has read and write permissions for the Group but not execute. Which group is it? Y'all take the grouping info in the output of the control ls -l (i.due east. group itsfoss).
  • The file has only read permission for Other i.due east. everyone that has access to the organisation. You lot don't need to know which other is it because 'other' means all the users.

Now if you see the entire ls -l control once over again, you lot tin can read the file permissions and buying together.

            -rwxrw-r-- 1 abhi itsfoss 457 Aug x 11:55 agatha.txt          

The file agatha.txt is owned by user abhi and abhi has read, write and execute permission. All the members of grouping istfoss take read and write admission to this file while everyone else has merely read access to this file.

Note: Root user has super powers and unremarkably, it has read, write and execute permissions to all the files, even if you lot don't encounter it in file permissions.

A single user may be the member of several groups but only the main grouping of the user is the group owner of a file created past the user. The primary grouping of a user can be institute using the id command like id -gn <username> . Leave the username blank if you are trying to find your ain principal group.

Now that you know how to find out permissions on a file, let'south see how you can change the permission and ownership of a file.

Change file permissions in Linux

You lot tin can utilize chmod command for changing the permissions on a file in Linux.

📚

Permissions used to be called mode of access and hence chmod was the brusk class of change the fashion of access .

There are two ways to apply the chmod command:

  • Absolute way
  • Symbolic mode

chmod 777 or 755? Learn to use chmod Command with Examples

This article volition teach you how to modify permissions in Linux with applied examples of chmod control.

Using chmod in accented mode

In the absolute fashion, permissions are represented in numeric class (octal system to be precise). In this system, each file permission is represented by a number.

  • r (read) = 4
  • westward (write) = 2
  • x (execute) = 1
  • – (no permission) = 0

With these numeric values, you can combine them and thus one number tin be used to represent the unabridged permission ready.

Number Permission
0
1 –x
2 -west-
3 (i.east. 2+1) -wx
iv r–
five (i.e. 4+1) r-10
6 (i.e. iv+2) rw-
7 (i.e. 4+two+ane) rwx

Can you guess the file permission in numbers on agatha.txt file in our example and then far? That'due south right, it's 764.

Now that you know what number represents which permission, let's run into how to alter file permission using this knowledge.

Suppose you lot want to alter the file permission on agatha.txt so that everyone can read and write but no ane tin execute information technology? In that case, yous tin utilise the chmod command like this:

            chmod 666 agatha.txt          

If you list agatha.txt now, y'all'll meet that the permission has been inverse.

            -rw-rw-rw- ane abhishek abhishek 457 Aug 10 eleven:55 agatha.txt          

Using chmod in symbolic style

The problem with the accented style is that you should ever provide three numbers for all the three owners even if you lot desire to modify the  permission prepare for merely ane possessor.

This is where yous tin use the symbolic manner with chmod command.

In symbolic style, owners are denoted with the post-obit symbols:

  • u = user owner
  • g = group owner
  • o = other
  • a = all (user + group + other)

The symbolic mode uses mathematical operators to perform the permission changes:

  • + for calculation permissions
  • – for removing permissions
  • = for overriding existing permissions with new value

Now that you know let'due south encounter how to apply chmod command in symbolic mode.

In our previous instance, if yous want to add execute permission for group owner, you can use chmod control like this:

            chmod yard+x agatha.txt          

If you wait at the permissions on this file at present, yous'll encounter that execute permission has at present been added:

            -rw-rwxrw- 1 abhi itsfoss 457 Aug x 11:55 agatha.txt          

You can also combine multiple permission changes in one command. Suppose yous desire to remove the read and write permission and add together execute permissions for Other. You lot also want to add execute permission for the User owner. You can do all of it one single command:

            chmod o-rw+x,u+x agatha.txt          

The resulting permissions would be similar this:

            -rwxrwx--10 one abhi itsfoss 457 Aug 10 11:55 agatha.txt          

If you desire to change the permissions for all three kinds of users at the same time, you can utilise it in the following fashion:

            chmod a-ten agatha.txt          

This will remove the execute permission for everyone.

            -rw-rw---- one abhi itsfoss 457 Aug x eleven:55 agatha.txt          

Some people find converting the file permissions from 1 mode to another a tiresome job. This why I created this little tool that allows you to calculate Linux file permissions in various modes online.

Change file ownership in Linux

To modify the buying of a file, you lot tin can use the command chown. Y'all may hands guess that chown stands for change owner.

Yous tin can change the user owner of a file in the following mode:

            chown <new_user_name> <filename>          

If y'all want to change the user equally well as grouping, you can utilize chown command like this:

            chown <new_user_name>:<new_user_group> <filename>          

If you just want to change the group, you can either use chown command in this manner:

            chown :<new_user_group> <filename>          

or employ chgrp command specifically used for changing group owner of a file or directory. Yous tin can guess that chgrp stands for change group.

            chgrp <new_user_group> <filename>          

In our example so far, if yous want to change the user owner and group to root, you tin employ the chown command like this:

            sudo chown root:root agatha.txt          

This will change the ownership of the file to root for both user and the group.

            -rw-rw---- ane root root 457 Aug 10 11:55 agatha.txt          

Notice that I had to utilize sudo with chown? It'south because the root is involved here and to deal with root, you need superuser rights.

Tip: Two groups cannot own the same file.

Bonus Tip: Is at that place a precedence in file permissions?

Call back of a state of affairs, where the user owner doesn't accept any permissions, grouping has read permission while others accept read and write permissions.

            ----r--rw- one abhi itsfoss 457 Aug 10 xi:55 agatha.txt          

Now, if the user abhi tries to read the file using cat or less control, will he be able to? The answer is no because it doesn't have the read permission.

Simply user abhi is function of group itsfoss and the group has read access. Heck! other has read and write permission. This should mean that anybody (including user abhi) tin read and write the file, right? Wrong!

In Linux, the precedence takes from user and then group and so to other. Linux organisation checks who initiated the process (true cat or less in our example). If the user who initiated the process is as well the user possessor of the file, the user permission bits are set.

If the owner of the file didn't initiate the process, and then the Linux system checks the grouping. If the user who initiated the process is in the same group as the owner group of the file, grouping permissions flake are set.

If this process owner is non even in the grouping as the file's group possessor, then the other permission $.25 are set.

What is SUID, GUID and Gluey Bit in Linux? How to Use Them?

You encounter an s instead of x in the file permissions? Linux has some special file permissions called SUID, GUID and Sticky Bit. Know more than about them.

What side by side?

I hope you liked the commodity and now yous have a better understanding of how file permissions piece of work in Linux.

In that location are some advanced file permissions similar SUID, GUID and sticky fleck that you may larn next, if you want to.

If yous have whatever questions or suggestions or if yous just want to say thanks, please leave a annotate beneath. If y'all liked the article, please share it on social media or various forums. This will help united states of america and other Linux users as well.

Grant Read Write Access to Directory Linux

Source: https://linuxhandbook.com/linux-file-permissions/

0 Response to "Grant Read Write Access to Directory Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel