File Permissions and Ownership in Linux: chmod and chown commands

File permissions and ownership are two key features in Linux that help protect files and maintain system security. Every file and folder has rules that decide who can read it, who can change it, and who can run it as a program. These rules are managed using two built-in tools: chmod to change permissions and chown to change ownership.

If you have followed the earlier article on Understanding the Linux File System (FHS), you already know that Linux has a well-structured way of organizing files. That structure goes hand in hand with permissions and ownership, making sure that only the right people or services can access or modify certain files.

How File Ownership Works in Linux

In Linux, ownership is assigned to every file or directory as soon as it is created. There are two main types of ownership: user, which refers to the person who owns the file, and group, which refers to a set of users who can be given shared access. This structure is especially useful in multi-user environments, where access must be limited to certain individuals or roles.

Changing file ownership with Chown command

When you list file details using ls -l, you will see the name of the user and the group that owns the file.

Seeing File Permissions
Seeing File Permissions

These fields help define what level of control different users have. If you want to change the owner, the group, or both, you can use the chown command as shown below.

# Change file owner to 'aadesh'
sudo chown aadesh file1.txt

# Change owner to 'aadesh' and group to 'wheel'
sudo chown aadesh:wheel file1.txt

# Change ownership recursively for a directory and all contents
sudo chown -R aadesh:wheel /home/shared_folder
Changing File Permissions Using Chown Command
Changing File Permissions Using Chown Command

These changes are useful when you are preparing shared folders or giving control of files to services or other users on the system.

What Permissions Mean and How They Work

Linux permissions are divided into three categories: one for the user (owner), one for the group, and one for everyone else on the system. Each category can be assigned three types of permissions: read, write, and execute. Read permission allows a file’s content to be viewed or a folder’s content to be listed. Write permission allows modifications, which includes changing file content or adding and removing files in a folder. Execute permission allows running a file as a program or script, or entering a folder using navigation commands.

The ls -l output displays a 10-character string that shows these permissions in order.

Seeing File Permissions
Seeing File Permissions

Each section of three characters describes what the owner, group, and others are allowed to do. These permissions can be modified using the chmod command.


Using chmod to Change Permissions

There are two main methods for changing permissions using chmod: symbolic mode and numeric mode.

In symbolic mode, you can add or remove specific permissions using short letters. The letters u, g, and o represent user, group, and others. The letters r, w, and x stand for read, write, and execute permissions.

# Add execute permission for the owner (user)
chmod u+x script.sh

# Remove write permission for the group
chmod g-w notes.txt

# Give others read access
chmod o+r file.txt

# Combine multiple changes in one command
chmod u+rwx,g+rx,o+r file.txt
File Permission Is Changed
File Permission Is Changed

In numeric mode, each type of permission is assigned a value: read is 4, write is 2, and execute is 1. These values are added together for each group of users, forming a three-digit number. For example, full access (read, write, execute) adds up to 7. Read and execute add up to 5.

# User: read/write/execute, Group: read/execute, Others: read/execute
chmod 755 script.sh

# User: read/write, Group: read, Others: read
chmod 644 file.txt
Changing File Permission Using Numeric Mode
Changing File Permission Using Numeric Mode

These modes offer flexibility depending on whether you want to be precise or efficient when setting permissions.

Why Permissions and Ownership Matter

Using permissions and ownership correctly helps protect your data and your system. Incorrect permissions can allow anyone to read or modify sensitive files. This can lead to system errors or security problems. On the other hand, setting permissions too strictly might block access to programs or users who need it.

If you remember from the earlier article on Linux Desktop vs Server, server systems often do not have a graphical interface. You will rely on the terminal to manage everything. This includes file access, service configuration, and software setup. Knowing how to use chmod and chown effectively gives you complete control over who can see, edit, or run each file on your system.

Understand the Permissions

Linux uses a permission model that defines what the file owner, the group, and other users can do with each file or directory. Permissions include read, write, and execute, and these can be modified using the chmod command. Ownership is managed using the chown command, which changes the user and group responsible for a file. Both of these tools help secure your system, especially when working in environments where multiple users or processes interact with the same files.

If you need a refresher on how to move between directories and create files using the terminal, see our earlier article Command Line & Shell Usage. If you are not yet familiar with where files are located or what each folder means in Linux, be sure to review Understanding the Linux File System (FHS).