How to Change the Permission of Folder and Directories in Linux/Unix

Nilesh Katuwal Feb 02, 2024
  1. File Ownership in a Unix/Linux System
  2. View File Permissions in Linux
  3. Change File Permissions in a Unix/Linux System
  4. Changing Groups and Owner in Linux
  5. Changing Ownership Command in Linux
  6. Use the chmod in Absolute Mode in Linux
How to Change the Permission of Folder and Directories in Linux/Unix

The file permissions system is one of the essential security elements of the Linux operating system.

These features allow us to adjust file permissions and access modes. We provide permissions based on proper permissions to avoid possible vulnerabilities when a user is not given the appropriate access to files and folders.

File Ownership in a Unix/Linux System

In a Unix/Linux system, there are three types of ownership for files and directories. The file’s allocated owner has the authority to alter it based on the permissions it receives.

  • Permission Of Owner: The permissions of the file’s owner specify what functions the file’s owner can perform. Since the user who creates a file becomes its owner, a user is also known as an owner.
  • Permissions by Group: The permissions of a group determine what activities a user can perform on a file if they are a member of that group.
  • Additional Permissions: Other users’ permissions define what actions they can take with the file.

Each file had three access options at first. They are as follows:

  • The Read (r): It allows you to retrieve the file’s contents but not write them. It will enable you to receive a list of files and directories found in a directory.
  • The Write (w): It enables you to add new data to files or alter existing ones, as well as create and modify files and directories.
  • The Execute (x): If a program does not have the execute flag, it cannot be run. This property is set for all programs and scripts. The system uses it to determine whether or not the file should be launched as a program.

View File Permissions in Linux

Of course, you can use a file manager on Linux to search for file permissions. They all support this functionality, but you will receive partial information this way.

Use the ls command with the -l argument to get complete details on all flags. All files in the directory will be shown, along with their properties and bits.

Run the following command in the folder where the files are stored to find out the permissions on a Linux file:

$ ls -l

I created a folder and two files, test and testfile inside it.

Output:

total 0
-rw-rw-r-- 1 user user 0 Jan 14 01:00 test
-rw-rw-r-- 1 user user 0 Jan 14 01:00 testfile

The first column indicates the various access modes or the permissions associated with a file or directory. Then there are groups of rights, first for the owner, then for the group, and everyone else.

Let’s take a closer look at what the conditional values of the permission flags mean:

  • --- - no rights at all;
  • --x - only file execution is allowed, as a program, but not changing or reading;
  • -w- - only writing and modifying the file is allowed;
  • -wx - allow modification and execution, but in the case of a directory, you cannot view its contents;
  • r-- - read-only rights;
  • rx - read and execute only, no write access;
  • rw- - read and write permissions, but no execution;
  • rwx - all rights;
  • --s - When the SUID or SGID bit is set, the owner’s SUID is displayed in the field, while the group’s SGID is displayed in the field;
  • --t - sticky-bit is set, which means users cannot delete this file.

Change File Permissions in a Unix/Linux System

The chmod is a change mode command that changes a file or directory’s permissions. The two ways to use chmod are symbolic and absolute modes.

Use the chmod in Symbolic Mode

The symbolic mode is the most straightforward approach for a learner to adjust file or directory permissions.

Using the operators in the following table, you can add, delete or specify the permission set you want with symbolic permissions.

chmod operator description
+ The specified permission(s) are added to a file or directory.
- Removes a file’s or directory’s specified permission(s).
= Sets the permission for the specified user (s).

Let’s look at an example. When you run ls -l on the test, you’ll see that the permissions are as follows:

$ ls -l test

Output:

-rw-rw-r-- 1 user user 0 Jan 14 01:00 test

Then run each of the sample chmod commands from the previous table on the test, followed by ls -l to see the permission changes.

$ chmod o+wx test
$ ls -l test

Here, o denotes other users, w write, and x execute. + is used to add specified permissions to a test file.

Output:

-rw-rw-rwx 1 user user 0 Jan 14 01:00 test

We can see the differences in the two outputs above. -rw-rw-r-- is changed into -rw-rw-rwx. The above command added rw permissions to other users.

Changing Groups and Owner in Linux

When they create an account on Unix, each user is given an owner ID and a group ID. All permissions above are also assigned based on the Owner and Group.

There are mainly two commands for changing the groups and owners.

  • chown: The chown command, which stands for change owner, is used to change a file’s owner.
  • chgrp: The chgrp command, which stands for change group, is used to change a file’s group.

The permission groups used are:

  • u – Owner
  • g – Group
  • o – Others
  • a – All users

Changing Ownership Command in Linux

The chown command is used to change a file’s ownership.

The following is the basic syntax:

$ chown [name] [filename]

The name of a system user or the user id (UID) of a system user can be used as the value of the user.

Similarly, to change group-owner of file:

$ chgrp [group_name] [filename]

Use the chmod in Absolute Mode in Linux

The file permissions can also be changed in numeric code. The equivalent number codes for access mode are as follows:

Numbers Access Mode
0 ---
1 -x
2 -w-
3 -wx
4 r-
5 r-x
6 rw-
7 rwx

For example, chmod 711 filename gives the owner all rights and all other users only the execute right.

Let’s look at the permissions of file testfile.

$ ls -l testfile

Output:

-rw-rw-r-- 1 user user 0 Jan 14 01:00 testfile

Now, using numeric code with chmod.

$ chmod 711 testfile
$ ls -l testfile

Output:

-rwx--x--x 1 user user 0 Jan 14 01:00 testfile

As seen on the output, file permission is changed.