Changing folder permission linux

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

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 )
Читайте также:  Как настроить английский windows 10

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.

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.
Читайте также:  Linux отключить ipv6 centos

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.

Источник

How to Manage File and Folder Permissions in Linux

For many users of Linux, getting used to file permissions and ownership can be a bit of a challenge. It is commonly assumed, to get into this level of usage, the command line is a must. Although there is always far more power and flexibility to be had, running seemingly complicated command isn’t alwaysa necessity. With the help of some of the most user-friendly desktop interfaces available, you can get away with little to no command line usage. Even with file permission and ownership.

That’s right, much to the surprise of many a new user, managing files and folders can be done from within the file managers. But before we get to the GUI, it’s always best to have a solid understanding of what it’s doing. So, we’ll start with the command line first.

Command line: File permissions

The commands for modifying file permissions and ownership are:

chmod – change permissions

chown – change ownership.

Neither command is difficult to use. It is important, however, that you understand the only user that can actually modify the permissions or ownership of a file is either the current owner or the root user. So, if you are user Bethany, you cannot make changes to files and folders owned by Jacob without the help of root (or sudo). For example:

A new folder was created on a data partition called /DATA/SHARE. Both users Bethany and Jacob need read and write access to this folder. There are a number of ways this can be done (one of which would be to join the users to a special group – we’ll go over managing groups in another post). If Bethany and Jacob are the only users on the system (and you know your network is safe – very important), you can change the permissions of the folder to give them access. One way to do this would be to issue the command:

The breakdown of the above command looks like:

sudo – this is used to gain admin rights for the command on any system that makes use of sudo (otherwise you’d have to ‘su’ to root and run the above command without ‘sudo’)

Читайте также:  Postgresql для windows бэкап

chmod – the command to modify permissions

-R – this modifies the permission of the parent folder and the child objects within

ugo+rw – this gives User, Group, and Other read and write access.

As you can probably surmise, this command opens wide the SHARE folder such that anyone on the system can have access to that folder. As I mentioned earlier, a more secure method would be to use groups. But we’re just using this for the purpose of demonstration.

The breakdown of permissions looks like this:

The ‘other’ entry is the dangerous one, as it effectively gives everyone permission for the folder/file. The permissions you can give to a file or folder are:

Using the -R switch is important. If you have a number of sub-folders and files within the SHARE directory, and you want the permissions to apply from the parent object (the containing folder) to the child objects (the sub-folders and files), you must use the -R (recursive) switch so the same permissions are applied all the way to the deepest folder, contained within the parent.

Command line: File ownership

Changing the ownership of a file or folder is equally as simple. Say Jacob moved a folder for Bethany into the SHARE directory – but Jacob still has ownership. This can be changed with a simple command:

Let’s break this down.

sudo – admin rights must be used since we are dealing with a folder that belongs to another user

chown – the command for changing ownership

-R – the recursive switch to make sure all child objects get the same ownership changes

bethany – the new owner of the folder

/DATA/SHARE – the directory to be modified

Should Bethany send the folder back to Jacob, the ownership would need to again be changed (again, this will be simplified with the use of groups).

GUI: File permissions

I’m going to demonstrate changing file permissions using the Nautilus file manager on an Ubuntu 13.10 system.

Let’s say you need to allow everyone to gain read/write permissions to the folder TEST. To do this, within the Nautilus file manager, follow these steps:

  1. Open Nautilus
  2. Navigate to the target file or folder
  3. Right click the file or folder
  4. Select Properties
  5. Click on the Permissions tab
  6. Click on the Access files in the Others section
  7. Select “Create and delete files”
  8. Click Change Permissions for Enclosed Files
  9. In the resulting window, Select Read and Write under Files and Create and delete files under Folders (Figure A)
  10. Click Change
  11. Click Close.

The trick comes when you need to change the permissions of a folder which does not belong to you. It can be done, but Nautilus must be started with admin access. To do this, follow these steps:

Open up a terminal window

Issue the command sudo -i

Issue the command nautilus

The sudo -i command gives you persistent access to sudo, until you enter the exit command to remove that access. Once Nautilus is open, you can change the permissions of the folder or file as described above – even if you are not the owner of the folder or file.

NOTE: If you’re using a distribution that doesn’t use sudo, alter the above instructions to:

Open up a terminal window

Issue the command su

Type your root password and hit Enter

Issue the command nautilus.

After you’ve completed the task, close the Nautilus window and then the terminal window.

GUI: Change ownership

Changing the ownership of a file or folder will most often require the use of admin rights. So for this, you’ll need to start Nautilus in the method described above.

For changing ownership of a folder or file through Nautilus, do the following:

In the Nautilus window (opened with admin rights), locate the folder or file in question

Right click the folder (or file)

Click on the Permissions tab

Select the new owner from the Owner drop-down (below)

That’s all there is to it. At this point you shouldn’t have any problems changing permissions or ownership for a file or folder with either the command line or the GUI. The use of groups will empower you to alter permission and ownership with more power and security – we’ll cover that soon. Until then, enjoy modifying your files and folders!

Источник

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