Homes and permissions in linux

How to manage Linux permissions for users, groups, and others

Photo by Min An from Pexels

Managing access to resources is a fundamental task for sysadmins. This responsibility consists of three components: identities, resources, and permissions. This article covers several user, group, and file management commands to control access to resources. The article uses a «How do I…?» format, and it assumes you have a few resources to work with. Specifically, I cover the following topics:

  • Creating directories and files
  • Managing ownership and associated groups
  • Setting permissions with absolute and symbolic modes

Linux security

Setting up a playground

I’ve been in IT for about 25 years, and most of that time was spent as a technical trainer. That means that the things that I write are usually structured as some sort of lab or other hands-on opportunity. It’s just how I cover material. With that in mind, I’ll assume you have a couple of identities and resources to experiment with as you read the rest of the article. You can use the following commands to set up a playground. It’s best to do this on a virtual machine rather than your personal Linux box, but these tasks are relatively harmless.

Create two new users and two new groups to work with. Note that you do not need to configure passwords for the users in this exercise, as you won’t log on with those accounts.

Note: You would use the passwd user01 command to set the user’s password.

In your home directory, create a new directory named playground :

Change into the

/playground directory by using the cd command. You are ready to work with the commands and concepts below.

When you’ve completed the article and learned the techniques I’ve covered, delete the two user accounts, the groups, and the playground directory. Use rm -fR /playground , userdel user01 , and groupdel groupA to remove the resources.

How do I create directories and files?

Use the mkdir command to create directories. The touch command is one of many ways to create files.

How do I create a directory named Resources ?

How do I create a directory path (a series of directories that don’t yet exist)?

Note: The goal here is to create the 2020data directory, but the given path’s data directory does not yet exist. The -p option creates parent directories as needed to complete the path.

How do I create a file named file1 ?

How do I create several files at once?

How do I manage ownership and groups?

In the playground directory, display the current owner and group associated with the Resources directory and the files.

How do I display permission, owners, and groups?

The ls -l command displays directory contents in long format. The long format contains both permissions and ownership. You can see that the user account that created the resources also owns those resources. The group association is also that user’s primary group.

How do I change the user/owner associated with file1 ?

How do I change the group associated with file1 ?

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

There is a specific chgrp command, but I prefer only to memorize one command ( chown ) and apply it to both functions (user and group associations) rather than chown for the user and then have to recall chgrp for the group.

So how do I use chgrp ?

How do I change the user/group for a directory and all of its contents?

The above task provides a recursive configuration. Technically, recursive commands are repeated on each specified object. Effectively, recursive means «this and everything in it.» In the above example, you are configuring the related user/group for the Resources directory and everything in it. Without the -R option, you would only affect the Resources directory itself, but not its contents.

Читайте также:  Линукс папка с шрифтами

How do I manage permissions?

The change mode or chmod command sets permissions. The syntax is straight-forward:

Here are two examples of manipulating permissions for file2 :

But wait! Those appear to be radically different examples (they’re not, actually). What are all those letters and numbers?

We need to discuss absolute mode and symbolic mode.

How do I use absolute mode?

Absolute mode is one of two ways of specifying permissions. I’ve seen this mode referred to as octal or numeric mode, but the term I learned was absolute. That term also makes the most sense to me because it’s an absolute statement of the desired permissions. I always told my students that this seemed like the most complex of the two modes but is actually the simplest. Usually, they agreed.

Each access level (read, write, execute) has an octal value:

Access level Octal value
Read 4
Write 2
Execute 1

Each identity (user, group, others) has a position:

Identity Position
User First or left-most
Group Middle
Others Last or right-most

More Linux resources

The absolute mode syntax states the desired permissions from left to right.

How do I grant the user (owner) read, write, and execute, the group read-only, and all others no access to file2 by using absolute mode?

The three permissions values are associated with identities:
ugo
740

  • The 7 is assigned to the user and is the sum of 4+2+1 or read+write+execute (full access)
  • The 4 is assigned to the group and is the sum of 4+0+0 (read-only)
  • The 0 is assigned to others and is the sum of 0+0+0 (no access)

In this example, the user has rwx, the group has r only, and all others have no access to file2 .

Let’s look at one more example.

How do I grant the user (owner) read and write, the group read-only, and all others read-only to file2 ?

  • The user has 6 (read and write)
  • The group has 4 (read-only)
  • All others have 4 (read-only)

I find this easier because there are no calculations involved. I’m not concerned with adding or subtracting specific permissions based on the current settings. Instead, I say, «set the permissions to be this,» and that’s the end result I get. It’s an absolute statement.

How do I set permissions for the Resources directory and all of its contents by using absolute mode?

How do I use symbolic mode?

Symbolic mode uses more symbols, but the symbols are simpler to understand. That’s attractive to sysadmins that are new to standard Linux permissions.

Each access level has a symbol:

Access level Symbol
Read r
Write w
Execute x

Each identity has a symbol:

Identity Symbol
User u
Group g
Others o

There are also operators to manipulate the permissions:

Task Operator
Grant a level of access +
Remove a level of access
Set a level of access =

The general chmod command syntax is the same:

Here is an example:

How do I remove the read permissions from others for file2 by using symbolic mode?

This example removes ( — ) the read ( r ) permission from others ( o ) for file2 .

Here’s another simple example:

How do I grant the read and write permissions to the group for file2 ?

This one gives ( + ) read and write ( rw ) to the group ( g ) for file2 .

How do I set permissions for a directory and all of its contents by using symbolic mode?

Special permissions and Access Control Lists

The above discussion covers standard Linux permissions—applying rwx to the user, group, and all others. Linux has far more flexibility, however. Special permissions permit users to run applications with other credentials, control the inheritance of group associations, and keep files from being changed accidentally. Check out this great article on special permissions.

Linux also has a way of enforcing different permissions for different users and groups. Access Control Lists (ACLs) permit sysadmins to define permissions for more than just one user and one group, which adds a great deal more flexibility to standard permissions. For example, user01 can be granted rw- to file1 , while user02 can be granted r— to file1 . Here is a great article on ACLs.

Wrap up

Creating resources, managing users, and setting permissions are fundamental tasks for Linux users. My goal was to provide a quick and easy guide based on common questions or tasks that we must all accomplish regularly. If you’re new to Linux, having a solid grasp of the eight commands discussed above will make your sysadmin life much easier.

Читайте также:  Hklm software wow6432node microsoft windows currentversion uninstall

Источник

Linux Permissions

We will check how many permissions are there and how you can change permissions accordingly. With Linux, you will get two different levels of authorization that include ownership and permission. We will see how different ownership and permission will work for Linux users.

Ownership of Linux Files

The Linux system handles three different types of owners for files and directories. These are users, groups, and others. First, we will see how permissions vary for different ownership.

User: A person who initially created a file or directory will become the owner of that. By default, you can call a user the owner.
Group: any group on the Linux system may contain several users with the same features and access to the file. It will save the time of the admin by assigning the different users with access individually. Then, they simply add the user to the group, and all access will automatically update all access for that user.
Other: This group of users do not belong to any particular group and do not create the file but can access the file. If you are setting any access permission for this group, it will be implemented to all the users by default.

Linux system has the permission parameter to distinguish between different types of ownership available. It helps prevent one file from being accessed by another user on the system, maintaining the system’s integrity. In addition, permission helps in defining user behavior.

Permissions in Linux

You can use three different types of permission in a Linux system that can be implemented for the ownership, as mentioned earlier.

Read: if you have the read permission, then you can easily open and read the file. However, in the case of a directory, you can only get the details of the directory.
Write: if you have to write permission for any file, you can edit that file. However, you will add, remove, and rename the files present in the directory in the case of a directory. Even if you have permission for the file but no the directory, you can only edit the file’s content but not rename, move, or delete the file name.
Execute: if you want to execute any file, its extension must be “.exe”, allowing you to run the file. If you do not have the execute permission, you may not run that file in the Linux environment. So, to set the system’s permission, the admin’s can help you with that.

Checking the File and Directory Access and Permission

You can check the permissions of the file via its mode bits. These bits help in identifying the actions you can carry out using a specific user account.

If you want to list all the files present in the current directory with detailed descriptions, including access and permission, run the below command.

The above sequence of rwx represents the read, write, and execute access for the user, group, and others. The leading “-” will indicate the type of the file. The “-” indicates the regular file. Here, rwx represents the following:

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

There are various file types present in the Linux system, and the leading character will represent the below file types.

  • – = represents Regular File
  • d = represents Directory
  • l = represents Symbolic Link
  • b = represents Block Special Device
  • c = represents Character Device
  • s = represents Unix Socket (local domain socket)
  • p = represents Named Pipe

In the below example, you can see the leading character is “d,” representing.

Explaining the Permission

Explaining the permission for the above example.

In the above example, the leading character represents the directory, followed by the permissions for the user (owner), group, and others.

Then, the first part, “rwx,” represents the access for the owner. It means that the owner can do actions like read, write, and execute on the directory.

Читайте также:  Windows 10 проблемы с сетевым принтером

Then, the second part, “r-x”, represents the access for the group. It means that the group can read and execute only on the current directory. But the other users present within the group will not write action to the directory.

Then, the last part, “r-x”, represents the access for the others. It means the remaining users will be able to implement read and execute actions on the directory. But the users will not be able to write to the directory.

Changing Permissions Using The “chmod” Command

If you want some files to be personal and not be shared with other users on the Linux system, you can achieve this by changing the permission to that particular file.

Linux provides you a “chmod” command to change the access and permission for a file or directory. Also, you can set the read, write, and execute access according to the user roles.

You can use the “chmod” command-line command with the help of the following syntax:

You can use the “chmod” command in two different ways- absolute mode and symbolic mode.

Using Absolute Mode

In this mode, the access and permissions are represented in the octal number. You can check the table as shown below for various access combinations.

Number Permission Type Symbol
0 represents No Permission
1 represents Execute –x
2 represents Write -w-
3 represents Execute and write -wx
4 represents Read r–
5 represents Read and execute r-x
6 represents Read and write rw-
7 represents Read, Write and Execute rwx

Now, we will discuss these octal representations with examples.

First, the file permission is shown below.

After we changed the access to (764) with “chmod” command is shown below.

The changed permission will specify that the owner is able to perform read, write, and execute actions now. User Group will be able to read and write. And the remaining users can only read.

Using Symbolic Mode

Unlike Absolute mode, Symbolic mode allows you to change the permission for a particular owner. You can use the mathematical operators for changing these permissions.

Operator Description
+ It will add an access or permission to a file or directory
It will remove the access
= It will set the access and overrides the permissions set earlier.

You can use the below notations for specifying the ownership.

User Denotations
u user/owner
g group
o other
a all

Here, we are considering a “sample” file that is having the below-mentioned permission.

Now we are setting the permission as shown below.

In the below example, we are about to add the execute permission to the user group.

In the below example, we are removing the “read’ permission from the user.

How to Change Ownership and Group?

Run the below-mentioned command-line command. Using the below command, you can change the ownership of a file or directory.

For changing the user along with the group for a particular file or directory, run the below-mentioned command-line command.

Run the “ls -l” command to check the ownership for a file or directory as shown below.

For changing the file owner for the above file to n100, you will require the sudo access.

The ownership has been changed, as shown below.

If you want to change the user and group to the root, run the below command.

You will get the output as below.

Conclusion

Linux is the most stable distribution among all operating system. You will get complete security over your files and directories. If you are a Linux admin or have sudo access, then you can play around Linux changing and setting access to various users and groups. These access will distinguish you from other roles and maintain privacy of someone’s data.

It is highly beneficial when you are working in a strict project and crucial data needs to be saved from unnecessary access.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.

Источник

Оцените статью