How to Change File Permissions in Ubuntu Terminal?

Managing file permissions is a fundamental aspect of Linux security and administration. In Ubuntu, file access is controlled by three permission types (read, write, and execute) assigned to three different categories of users (the owner, the group, and others). This guide provides a quick overview of how to view these permissions using the ls -l command and how to modify them using the chmod command, utilizing both symbolic and absolute (numeric) methods.

Understanding Ubuntu File Permissions

Before changing permissions, it is essential to understand how they are structured. When you run the ls -l command in the terminal, you will see a 10-character string representing the file type and access rights.

The standard permission characters are:

Viewing Current Permissions

To check the current permissions of files in your directory, open your terminal (Ctrl + Alt + T) and type the following command:

ls -l

This will output a list where permissions look similar to -rw-r--r--, indicating that the owner can read and write, while the group and others can only read the file.

Changing Permissions Using the chmod Command

The chmod (change mode) command is the primary tool used to modify file permissions in Ubuntu. There are two common ways to use it: symbolic mode and absolute (numeric) mode.

1. Symbolic Mode (Text-Based)

Symbolic mode uses letters to represent the users and the operations.

User Categories:

Operations:

Examples of Symbolic Mode:

chmod u+x filename.sh
chmod go-w document.txt
chmod a+x script.sh

2. Absolute Mode (Numeric-Based)

Absolute mode uses a three-digit octal number to represent permissions for the Owner, Group, and Others. Each digit is calculated by adding up the values of the permissions you want to grant.

Numeric Values:

To calculate a permission digit, add the numbers together. For example, read ($4$) + write ($2$) = $6$. Read ($4$) + write ($2$) + execute ($1$) = $7$.

Common Permission Combinations:

Examples of Absolute Mode:

chmod 700 private.txt
chmod 644 index.html

Modifying Permissions for Directories Recursively

If you need to change the permissions of a directory and all of the files and subdirectories inside it, you must use the recursive flag (-R).

chmod -R 755 /path/to/directory

Handling Permission Denied Errors

If you attempt to change permissions on a system file or a file owned by another user, you will receive a “Permission denied” error. To bypass this, prepend the sudo command to run the operation with administrative privileges:

sudo chmod 644 /etc/environment

You will be prompted to enter your user password to confirm the action.