Changing linux folder permissions

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.

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:

Читайте также:  Как пароль восстановить при входе windows

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.

Источник

How To Change File & Folder Permissions on Linux Using Chmod

When working with some files or folders on Linux you might have seen an error saying Permission Denied .

This is a common error that is associated with the permissions for the specific file/folder.

In this tutorial, you will learn different ways to change the file and folder permissions for any users or groups on Linux.

Linux is an operating system that supports multiple users working on the same system. Thus, it is essential to manage the permissions such as who can see or modify the files and folders for every one of them.

In some GUI versions of Linux such as Ubuntu, you can change the permissions from file managers. Here, we will be focusing on a more general way to do it using any Linux terminal or command line.

Table of Contents

Basic Ideas

Before we move on to the methods to change the permissions, you need to have some basic ideas of how the file/folder permissions work on Linux.

There are 3 separate user classes on Linux, who could use a file in different ways. The user classes are:

  1. Owner: This is the user who created the file/folder.
  2. Group: The users who are in a group.
  3. Others: Any user outside the Group and the Owner. This user class is often called World.

Any files or folders can have three different kinds of permissions associated with them –

  1. Read permission: A user can read files with this permission.
  2. Write permission: This permission allows a user to modify the file.
  3. Execution permission: It allows a user to execute the file.

Now that you know the user and permission types, an example will help you understand the whole picture. Let’s see how we can view the permissions of a file or a folder.

Читайте также:  Mount iso linux write

Use the ls -l command to list the files, directories, and their permissions:

You can also see the permissions of a specific file using:

After running the ls -l command, you will see something like this:

Right now we’ll focus on the text on the left, drwxr-xr-x or -rw-r—r— in lines 3 and 4 respectively. These are the permissions for each of the directories and files that are listed to the right.

Don’t worry, they are pretty easy to understand even though they might look intimidating at first. Now let’s analyze these two examples to understand what they mean.

If you count the characters here, you will find every one of these codes has 10 characters.

That leaves us to decode the next 9 characters for both examples. If you simply divide these characters into groups of 3s, decoding them becomes much simpler. Take a look:

For both of the examples, we have just divided the last 9 characters into three groups, each consisting of 3 characters.

Let’s analyze the first example:

d: indicates that this is a directory
rwx: are the permissions for the Owner.
r-x: are the permissions for the Group user class.
r-x: are the permissions for the Other users.

These characters stand for:

r = Enables permission for reading

w = Enables permission for writing

x = Enables permission for execution

When a class of user has all the permissions, there will be rwx written in the position of that user class.

So, in the above example, the Owner of the directory has all the permissions. The Group users and Other users have the same permission to read & execute, denoted by r-x.

Notice that these classes of users do not have permission to write. Thus, a — is used in the place of w .

Likewise, having a — means the user class does not have any permission for the file/directory whatsoever.

Let’s also take a look at our second example of permissions from earlier:

Here, the Owner has permission to read & write the file, while the Group and Other user classes have only the permission to read.

The Chmod Command

Now we’re ready to learn the commands to change the permissions of files and folders.

The chmod command changes the file permissions. There are two methods for changing permissions with chmod . By using:

  1. Symbolic chmod commands. (The notation we’ve used in the examples above, drwxr-xr-x , -rw-r—r— )
  2. Numeric chmod commands. (We’ll cover this a bit further down. Examples 777 , 644 )

Symbolic Chmod Commands

In Linux systems “mode” refers to permissions. The command chmod stands for “change mode”.

The easiest way of using the chmod command is the symbolic or text commands. The command usually takes at least three inputs and the file/directory name. The syntax can be written in a simple format as:

The first input [user class] can be:

  • u – The owner of the file/directory.
  • g – Users who are members of the same group.
  • o – Other users.
  • a – All users, same as ugo

The second input is the [operation] having the values of:

  • + For adding specified permissions.
  • For removing specified permissions.
  • = Sets the current permissions to the specified ones. If nothing is specified after = sign, all the current permissions of the specified user class get removed.

The general values for the third input [permissions] is:

  • r – Sets the permissions to read.
  • w – Sets the write permissions.
  • x – Sets the permission for execution.

Let’s look at some examples.

I’ll run the following command on a file on my computer, Default_MAC.txt. This command will add write permissions to the Other user class.

Here’s how the permissions look for Default_MAC.txt initially.

Now I’ll run the command on Default_MAC.txt :

Here’s the resulting output when listing files with their permissions. Keep in mind that you might have to use the sudo command to run it successfully.

Some more examples with the chmod command:

This command will remove execution permission from the Owner.

It will set the permission to read-only for the Group user class.

This command will remove all the permissions from the Other user class.

Multiple inputs can be used together for [user class] and [permissions] flags. Take a look at some more examples:

Using this command will add read, write, and execution permissions to the Owner and Group user class.

These three commands are equivalent. Running any of them will remove all the permissions from all the user classes.

Читайте также:  Windows path где прописать

If you want to change the permissions for a directory, just replace the file name with the directory name. This concludes the symbolic method for changing the permissions of a file or a folder.

Numeric Chmod Commands

This is an elegant technique that will allow you to change file or directory permissions while typing less than using the symbolic approach.

The Basics

We’ll start with the basics. Each permission has a digit or value associated with it. The digits are:

  • r = 4, Read permission has a value of 4.
  • w = 2, Write permission has a value of 2.
  • x = 1, Execution permission has a value of 1.
  • – = 0, no permission has a value of 0.

We add different values together to get different sets of permissions we want to have.

Here are the combinations:

  • 4 + 2 = 6, means that 6 is rw
  • 4 + 1 = 5, means that 5 is rx
  • 2 + 1 = 3, means that 3 is x
  • 4 + 2 + 1 = 7, means that 7 is rwx

Let’s say we want a user to have all the permissions.

We will add the values of these permissions (rwx) together, 4+2+1, which gives us 7.

If we want permissions to only be read & write (rw), we will have 4+2+0 = 6.

Likewise, the digit 5 means read & execution (rx) permissions, 3 means write & execution (wx), and so on.

This is how you can represent permissions with the help of a single digit.

Assigning Permissions

Now we’ll see how to assign these digits to different user classes.

We’ll take the digits representing the permissions and form a 3-digit number with them.

The position of those digits in the number will represent three different user classes.

As an example, we’ll use the number 643:

  • The first number, 6, represents the permissions for the Owner
  • The second digit, 4 is the permissions for the Group user class
  • The third digit, 3 designates the permissions for the Others user class

In other words, 643 means that the Owner will have the read & write (6) permissions. Likewise, the Group user class will have reading (4) permission, and the Other user class will have write & execution (3) permissions.

Here is a little image trying to explain this visually.

Let’s take another example, for practice reasons:

This time we take the number 751:

  • 7 means that the Owner has all the permissions (read, write, & execution)
  • 5 designates that the Group user class has read & execution permissions
  • 1 marks the Others user class only has execution permission.

The syntax of chmod command with the numeric method is:

Doesn’t that look cleaner than when using the symbolic notation? I believe you’ll encounter numeric notation more often, however you are free to use whichever notation feels more natural.

Finally, let’s look at some of the examples with the command:

This command will give full permissions ( -rwxrwxrwx ) to all the user classes. I’ll use the files on my desktop again, and will run the command on Default_MAC.txt :

Now Default_MAC.txt has read/write/execute ( -rwxrwxrwx ) permissions for all user classes.

Now let’s take away all of its permissions. To do this we set the permissions to 000 :

Now the file has no permissions for any of the user classes.

Here are a few more random examples, in hopes that it helps better understand the numeric notation:

This command will give read-only permission to the Owner and no permission for Group and Other user classes.

This will allow full permission to the Owner and Other user classes, read-only permission to the Group user class.

This command will remove all the permissions for the Owner and Group user classes, and allow the execution permission to Other users.

Recursively change permissions of all files within a directory

A frequent situation you’ll encounter is having to recursively change permissions of files within a directory.

You can use the recursive option with the chmod command for doing it. The -R option enables the chmod command to change permissions recursively.

This command will change all the file and subdirectory permissions recursively within the directory you specified.

Conclusion

In this tutorial we hope you’ve understood how to change permissions of files and directories in Linux using the chmod command.

If you face any problems, take a look at the manuals page for the chmod command . The manual is also available in the terminal, when you type man chmod .

If you encounter any issues or have any feedback for us, then feel free to leave a comment or contact us, and we’ll get back to you as soon as we can.

Источник

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