Linux change rights on directory

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.

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

Читайте также:  Mac os citrix workspace keyboard

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.

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.

Читайте также:  Windows boot manager boot bcd что это

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.

Читайте также:  Steam windows 10 key

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.

Источник

Ubuntu Documentation

Understanding and Using File Permissions

In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user «root» has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.

To change or edit files that are owned by root, sudo must be used — please see RootSudo for details.

If the owner read & execute bit are on, then the permissions are:

There are three types of access restrictions:

Permission

Action

chmod option

There are also three types of user restrictions:

User

ls output

Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.

Folder/Directory Permissions

Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.

Permission

Action

chmod option

(view contents, i.e. ls command)

(create or remove files from dir)

(cd into directory)

read restricts or allows viewing the directories contents, i.e. ls command

write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)

execute restricts or allows changing into the directory, i.e. cd command

» height=»16″ src=»/moin_static198/light/img/icon_cool.png» title=»Info » width=»16″/> Folders (directories) must have ‘execute’ permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus).

Permissions in Action

Using the example above we have the file «/etc/hosts» which is owned by the user root and belongs to the root group.

What are the permissions from the above /etc/hosts ls output?

Changing Permissions

The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.

Источник

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