File Ownership in Linux: How read, write and execute works for files and directories

published on Sun Mar 29 2020

As we’ve pointed out before,

Everything in Linux is a file.

Users

These files have permissions and users associated with them. There are three kinds of users in the Linux security model:

Permissions

In Linux, access rights to files and directories are defined in terms of read access, write access and execution access.

The three kinds are:

Permission File Directory
r Allows a file to be opened and read Allows a directory’s contents to be listed if the execute attribute is also set
w Allows a file to be written to or truncated; however, this attribute does not allow files to be renamed or deleted. The ability to delete or rename files is determined by directory attributes Allows files within a directory to be created, deleted and renamed if the execute attribute is also set
x Allows a file to be treated as a program and executed. Program files written in scripting languages must also be set as readable to executed Allows a directory to be entered using cd

ls Output

If we look at an example output of the ls command we see,

-rwxrwxr-- 1 saikat saikat    0 Mar 29 19:20 dummy.sh

The first 10 characters of the listing are the file attributes. The first character is the file type. Common file types are:

Remaining characters of the file attributes are called the file mode. They represent the read, write and execute permissions for the file’s owner, the group owner and everybody else. In this case, they are:

Changing permissions

We use chmod to can change permissions of a particular file or directory in Linux. There are two ways of specifying mode changes:

I use the symbolic method because it’s more visual and only changes the permissions that you wish to change and not the entire mode. You also don’t need to perform complicated binary to octal conversion to figure out the mode to pass.

Symbolic notation has 3 parts:

Example:

chmod u=rwx,g=rx,o=r myfile

chmod modes examples

Changing Ownership

Ownership of files and directories are changed with the chown command. Syntax is:

chown [owner][:[group]] file...

chown can change the file owner and/or the file group owner depending on the first argument of the command.

chown argument examples