DEV Community

Cover image for Master Linux File Permissions While Your Coffee Brews
Nedim Hadzimahmutovic
Nedim Hadzimahmutovic

Posted on

Master Linux File Permissions While Your Coffee Brews

Linux allows multiple users to access and use the system simultaneously. File permissions are crucial in a multi-user system to protect user privacy. It ensures that only authorized users can access and modify files.

The three sets of permissions are:

  • Owner permissions: Apply to the user who owns the file.
  • Group permissions: Apply to group members that own the file.
  • Other permissions: Apply to all other users who are not the owner or a member of the group.

The ls command

The ls by default is used to list the contents of a directory.

Example to display contents of the current directory

ls
Enter fullscreen mode Exit fullscreen mode
ls .
Enter fullscreen mode Exit fullscreen mode

However, it can be used to list file permissions and ownerships or to find hidden files and directories.

Example to check the permissions of files

You can use the -l option which is known as long listing format. The complete command would be ls -l.

ls -l

total 552
-rw-rw-r-- 1 coolin coolin 493743 Oct 23 10:58 book.pdf
drwxrwxr-x 5 coolin coolin   4096 Oct 15 08:27 chapters
Enter fullscreen mode Exit fullscreen mode

Infographics that explain each column in a long listing representation

For example the following case.

drwxrwxr-x 5 coolin coolin   4096 Oct 15 08:27 chapters
Enter fullscreen mode Exit fullscreen mode

Image description

Access the man page for more info.

man ls
Enter fullscreen mode Exit fullscreen mode

Hidden Files

To view hidden files in a directory use the -a or --all option with the ls command. This option tells ls to list all files, including those that are hidden.

An example can be found below

ls -a ~
.  ..  .bash_history  .bashrc  .profile
Enter fullscreen mode Exit fullscreen mode

Or

ls -a -l ~
total 76
drwx------  8 root root  4096 Oct 23 14:47 .
drwxr-xr-x 23 root root  4096 Aug  5 17:07 ..
-rw-------  1 root root 24062 Oct 22 21:20 .bash_history
-rw-r--r--  1 root root   161 Apr 22  2024 .profile
Enter fullscreen mode Exit fullscreen mode

Refer to the ls command's manual pages for more details.

man ls
Enter fullscreen mode Exit fullscreen mode

Directory Permissions

Directories are file types that are marked with the letter d. You set the permissions the same way as with files, but directories behave differently than files when it comes to permissions.

Image description

The Read Permission

  • Allows a user to view the contents of a directory, such as listing files and subdirectories.
  • A user with 'r' permission can not read the contents of individual files within a directory.
  • The 'r' permission only grants access to the directory's contents, not the individual files.

The Write Permission

  • Allows a user to modify the contents of a directory, including creating, deleting, and renaming files.
  • A user with 'w' permission can change the permissions of any file within a directory, regardless of their permissions or ownership.
  • The 'w' permission grants the ability to change file permissions within the directory.

The Execute Permission

  • Allows a user to enter or access a directory.
  • The 'x' permission does not grant access to listing the contents of a directory.
  • The 'x' permission only allows entry into the directory. To list the contents, the 'r' permission is also required.

To remove all permissions use the command below.

chmod 0000 myfile
Enter fullscreen mode Exit fullscreen mode

File Permissions

To understand security, you need to master Linux file permissions. As
they control who can access files, and modify them it is crucial to
understand how they work and how to correctly set file permissions.

Image description

A dash (-) represents the lack of a particular permission.

The chmod command

Using the chmod command you change file mode bits meaning you can
modify file permissions.

There are two modes to change permissions:

  • Symbolic mode,
  • Numeric mode.

Symbolic Mode

In this mode, permissions are represented by letters. The symbolic mode
offers a detailed approach to modifying permissions, allowing you to add
or remove specific permissions.

In this example we will make a file readable and executable by everyone, you would use the following example

chmod a+rx file.txt
Enter fullscreen mode Exit fullscreen mode

This is an example of how you use the symbolic mode to add read and write permissions for the user and group, but revoke all permissions for others

chmod ug+rw-x,o-rwx text.txt
Enter fullscreen mode Exit fullscreen mode

To check if permissions were set correctly use the following command.

ls -al text.txt
-rw-rw---- 1 kulin kulin 0 Dec 24 20:16 text.txt
Enter fullscreen mode Exit fullscreen mode

Numeric Mode

In this mode, permissions are represented using numbers. In this mode
permissions are represented as follows: read is 4, write is
2
, and execute is 1.

Basic Overview of Permissions

A basic permissions demonstration can be found in the next table.

Image description

Detailed Overview of Permissions

A demonstration of detailed permissions can be found in the next table.

Image description

Commonly Used Permissions

A common practice when setting permissions to files and directories is
as follows:

  • Directories: 755 or 750,
  • Files: 644 or 640,
  • Sensitive files containing credentials: 600.

Image description

- Warning

*The **777 is a world-readable type of permission meaning everyone gets all permissions. It should be used with extreme caution.***

Refer to the chmod command's manual pages for more details.

man chmod
Enter fullscreen mode Exit fullscreen mode

The stat command

This command is used to status files. We will cover the basic use cases
that are useful in the context of this chapter.

Display Permissions in Octal Mode

Example to easily get a file's permissions in octal mode

stat -c %a /etc/passwd
644
Enter fullscreen mode Exit fullscreen mode

Display Permissions in Human Readable Form

Example to easily get a file's permissions in human-readable form

stat -c %A /etc/passwd
-rw-r--r--
Enter fullscreen mode Exit fullscreen mode

You can combine stat options as shown below.

stat -c "%n is a %F, permissions are %A, in octal %a" /etc/passwd
/etc/passwd is a regular file, permissions are -rw-r--r--, in octal 644
Enter fullscreen mode Exit fullscreen mode

Refer to the stat command's manual pages for more details.

man stat
Enter fullscreen mode Exit fullscreen mode

This article is part of my book:

Master Linux Permissions and File Types While Your Coffee Brews


Top comments (0)