Set owner permissions 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.

Читайте также:  Self paced training kit mcts 70 505 windows forms application development

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.

Читайте также:  Acronis true image не клонирует диск windows

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.

Источник

How to Change Permissions and Owners via Command Line

In this tutorial, you will learn how to change permissions and owners in Linux using chmod and chown commands. By doing so, you’ll have better management in team-based projects.

Why You Need to Change Permissions and Owners in Linux

Linux is a multi-user operating system, so more than one person can work on the same computer at the same time. What’s great, the system can be accessed locally or remotely. That’s why developers often use this OS for group projects.

In such a large environment, we need to set file permissions and ownership, so only specific users can access our data. This way, we can protect sensitive information and prevent unwanted changes from happening.

Fortunately, thanks to chmod and chown commands, it is easy to change permissions and owners in Linux. But before we begin to learn how to use them, make sure you have access to the command line. You can launch it by pressing Ctrl + Alt + T.

How to Change File and Folder Permissions

We will be using the chmod command to change file and folder permissions in Linux. But first, you need to be aware that there are three types of users who can interact with a file:

  • Owner — the user who creates and owns a file or folder.
  • Group — all users who are members of the same group.
  • Others — all other users on the system who are neither the owner nor members of a group.

To see permissions and owners of a specific file, you can run this command:

The result will look like this:

Let’s break the output down to see what each field means:

  • “-rwxrw-rw-“ — this part of the line represents the file permissions. To understand it better, we have to divide it into four groups: (), (rwx), (rw-), and (rw-).
    • The first group indicates the file type. Our example shows a hyphen, which represents a regular file. If we are inspecting a directory, the hyphen will be replaced by d.
    • The three characters after the file type represent the owner’s file permissions. In this example, we can see that the owner can read (r), write (w), and execute (x) the file.
    • The next three characters are the group’s file permissions. We can conclude that the group can read (r) and write (w), but cannot execute the file. This is because the last character is a hyphen instead of the letter x.
    • The last group is others’ file permissions. Based on our example, this type of user cannot execute the file, but they are allowed to read and write.
  • 1 – the number of hard links. A hard link is an additional name for an existing file.
  • user user – the owner and group owner of the file.
  • 0 – the size of the file in bytes.
  • Jan 19 12:59 – the last modification date.
  • myfile.txt – the name of the file/folder.
Читайте также:  Драйвера для тачпада asus x550c windows 10

How to Use chmod Command

Let’s say we want to change Linux file permissions from -rwxrw-rw- to -rwx-r–r–. Simply enter this line:

By executing this command, the owner can read, write, and execute the file (rwx). However, group and others are only allowed to read (r–).

At this point, you might wonder why we are using a three-digit number (744) after the chmod command.

The number determines the file permissions. Read, write, and execute are represented by a numerical value:

  • r (read) – 4
  • w (write) – 2
  • x (execute) – 1

So if you want to give all permissions (rwx) to a user, we need to add read (4), write (2), and execute (1). Therefore, rwx is equal to 7.

Meanwhile, since group and others are only allowed to read the file, we give them 4.

Remember, the owner’s permissions always come first, then followed by group and others. That’s why we enter 744.

If you don’t want to give any permission to a user, enter 0 into the corresponding spot.

Here is a list of the most common file permissions:

Common permissions for directories:

Changing the Owners of Files and Folders

To change the owner of a file and folder, we will be using the chown command. This is the basic syntax:

Let’s say we have a file named “myfile.txt.” If we want to set the owner of the file to “hostinger,” we can use this command:

However, if we want to change the group owner of the file to “clients,” we’ll enter this line instead:

Notice that we use a colon (:) before “clients” to indicate that it is a group owner.

Now, to change both the owner and group owner at the same time, the syntax would be like this:

The main rule is that the owner should come before the group owner, and they have to be separated by a colon.

Using Options with chmod and chown Commands

Option is an additional command to change the output of a command.

One of the most popular options that you can combine with chmod and chown is -R (Recursive). This Linux option allows you to change permissions or owners of all files and subdirectories inside a specific directory.

If you want to use an option, you have to place it right after the chmod/chown command.

Take a look at this example:

After you enter the above command, the owner can read, write, and execute all files and subdirectories inside the /etc/myfiles directory. The command also gives read and execute permissions to group and others.

Be extra careful with this option. Improper use of the command may cause critical failure, and it requires a great deal of work to reverse the changes.

Aside from -R, the following options are often used with chmod and chown commands:

  • -f or force. The command line will ignore any errors and apply the chmod and chown commands.
  • -v (verbose) option gives you diagnostics of all files that are processed by the command.
  • -c (changes) is similar to the -v option. However, it will only provide information when changes were successfully made.

Conclusion

In this tutorial, you have learned how to use chmod and chown commands to change permissions and owners in Linux. We also provided the basic syntax and several useful options that you can combine with either of these commands.

To learn more about Linux command line, you can read our article on basic bash commands.

If you have any questions, feel free to comment below!

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Источник

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