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.
- First character: Represents the file type (e.g.,
-for a regular file,dfor a directory). - Next three characters: Represent the Owner (User) permissions.
- Middle three characters: Represent the Group permissions.
- Last three characters: Represent Others (everyone else) permissions.
The standard permission characters are:
r(Read): Permission to view the file contents.w(Write): Permission to modify or delete the file.x(Execute): Permission to run the file as a program or script.
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 -lThis 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:
u: User (Owner)g: Groupo: Othersa: All (User, Group, and Others)
Operations:
+: Add a permission-: Remove a permission=: Set exact permissions
Examples of Symbolic Mode:
- To grant execute permission to the file owner:
chmod u+x filename.sh- To remove write permission from the group and others:
chmod go-w document.txt- To make a script executable for everyone:
chmod a+x script.sh2. 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:
4: Read ($r$)2: Write ($w$)1: Execute ($x$)0: No permission
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:
777: Everyone can read, write, and execute (highly unsecure).755: Owner can read, write, and execute; group and others can only read and execute (standard for scripts and directories).644: Owner can read and write; group and others can only read (standard for regular files).
Examples of Absolute Mode:
- To set a file so only the owner has full access, and others have no access ($700$):
chmod 700 private.txt- To set standard web server permissions for a public file ($644$):
chmod 644 index.htmlModifying 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/directoryHandling 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/environmentYou will be prompted to enter your user password to confirm the action.