How to change the permissions of a file or directory in Linux

In this manual we will learn how to change the permissions of any file in a GNU/Linux operating system through the command line.

Before starting the tutorial, don't you have a Cloud server with Linux? Don't worry! With just one click, you can easily deploy it with SW Hosting.

cta:cloud_so

However, before mentioning any kind of command, we should learn about the functioning of the permissions system in GNU / Linux operating systems. We will show you below:

1. Types of permissions

Specifically there are three types of permissions that a file or directory in linux can be assigned:

  • Read permission: allows the user to read the file or in directories list the contents.
  • Write permission: allows the user to modify the file or in directories to add files or modify the directory itself.
  • Execute permission: allows to execute files (for example BASH scripts) or in directories to have access to it (but not necessarily to list the content if the user does not have read permission).

2. User types

The three types of permissions mentioned above can be enabled or disabled on each file/directory for any of the three types of users mentioned below:

  • Owner: designated as the owner of the file/directory.
  • Group: group of users to which the file/directory belongs.
  • Other: any other user in the system.

3. Combinations of permissions and users

As explained in the previous section, these three types of users and permissions can be freely assigned.

For example, the owner of the file/directory can have read, write and execute permissions, while other system users can only have read permissions. Any combination of these three permissions and three users is feasible giving a total of 512 possible combinations.

Before we start with the tutorial, do you not have a Cloud server with Linux? Don't worry! With just one click you can easily deploy it with SW Hosting.

cta:cloud_so

4. Change permissions of a file or directory

Finally, to assign these permissions we will use the chmod command.

info In order to change the permissions of a file or directory you must be the owner or a user with administrative privileges. Only the owner or root users can change the permissions of a file / directory.

The chmod command has the following syntax:

chmod [permission to assign or remove] filename

The symbols used to describe a permission are as follows:

Permissions:

  • r: read
  • w: write
  • x: execute

Users:

  • u: owner
  • g: group
  • o: other
  • a: all of the above

Thus, the abbreviation u+w would indicate that you want to assign write permission to the owner.

Other examples and combinations:

  • o+x (assigns the execute permission to "other").
  • a-x (removes execute permission to all users)
  • g-w (removes execute permission to the members of the group to which the file belongs)
  • u+r (grants read permissions to the owner)

Knowing these abbreviations, we can now use the chmod command as follows:

chmod u+x filename

This command would grant execute permission to the file filename and to the owner of the file.

Multiple permissions can be granted to multiple users at the same time. Thus:

chmod ug+wrx filename

This command would grant read, write and execute permissions (full permissions) to both the owner and group members to the file filename.

Finally, it is worth noting the usefulness of the -R option which applies these permissions recursively to any file inside a directory. For example:

chmod -R u+wx directoryname

This command applies write and execute permissions for the owner to any file or directory contained within directoryname, including files/directories contained at lower levels.

info the chmod command has a so-called octal mod that allows assigning permissions using numbers. This information will be detailed in another user manual.

Remember that if you do not yet have a Cloud server with Linux, you can easily deploy it with SW Hosting.

cta:cloud_so


2024 SW Hosting. All rights reserved. The prices shown on the website do not include any applicable taxes.