howtouselinux

2 ways to check file permissions in Linux

Table of Contents

In Linux, file permissions are an important part of keeping your system secure. In this blog post, we will discuss two ways to check file permissions in Linux- using the “ls” command and using the “stat” command.

We will also discuss what each of the permissions means, and how to change them if necessary. Stay safe out there!
There are two ways to check file permissions in Linux.

  • You can easily check file permissions in Linux using the “ls -l filename” command. This command will list all the information about this file, including the permissions. Each permission is represented by a single letter- r for read, w for write, and x for execute. 
  • To get a more detailed view of the file permissions, use the “stat” command instead of ls. The “stat” command takes a few more options than ls, so be sure to read the man pages for more information.

 

Procedure to check file permissions in Linux

  1. Open the terminal application.
  2. Type ls -l command
  3. Press Enter to run the command.
  4. This will give you a list of all the files in the current directory, along with their permissions. Each permission is represented by a single letter- r for read, w for write, and x for execute.

 

Understanding file permissions in Linux

File permissions are important because they help to protect your system from unauthorized access. In Linux, there are three types of permissions- read, write, and execute. Each type of permission is represented by a letter- r for read, w for write, and x for execute.
Every file or directory has three levels of ownership:

  • User owner (u).
  • Group owner (g).
  • Others (o).

 
Each level of ownership can be assigned the following permissions:

  • Read (r).
  • Write (w).
  • Execute (x).

 
In this example -rw-r-r, the owner of the file test.txt has access to “Read and write”, while other members of its group, as well as all other users, have “Read-only” access. Therefore, they can only open the file, but cannot make any modifications.

Check file permissions with ls command in Linux

The ls command can be used to find file permissions in Linux.  It is a common Linux command that is used to list the contents of a directory. To do this, simply type “ls -l” at the command prompt. 

To use this method, simply type “ls -l filename”. Replace “filename” with the name of the file or folder you are looking for. For example, if I wanted to find the owner of a file called “testfile”, I would type “ls -l testfile”. In the following example, the permission of testfile is rw-r–r–.

% ls -l testfile
-rw-r–r– 1 howtouselinux staff 0 3 3 21:04 testfile

The first dash indicates that this is a regular file, and not a directory. The next three characters (rw-) indicate the owner’s permissions- in this case, the owner can read and write to the file, but cannot execute it.

The next three characters (r–) indicate the group permissions- in this case, members of the group can only read the file, and cannot write to or execute it. The last three characters (r–) indicate the world permissions- anyone can only read the file, and cannot write to or execute it.

Find file Permissions with stat command in Linux

The “stat” command is another Linux command that can be used to get file permissions. To use “stat”, simply type “stat filename” at the command prompt. This will give you a detailed output of all the permissions for the file.

For example, the output of “stat test.txt” would look like this:
File: ‘test.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 262094 Links: I-Node uid=1001(user) gid=1001(user) mode=0644(rw-r–r–) . . .

The first line shows the name of the file, its size, and what type of file it is. The next line shows the device number and inode number. The inode number is a unique number that identifies each file on a Linux system.

The next line shows the owner’s permissions, group permissions, and other permissions. In this case, the owner can read and write to the file, members of the group can read the file, and anyone can read the file.

Change file permissions in Linux

Now that we know how to check file permissions, let’s discuss how to change them. There are two ways to change file permissions- using the “chmod” command or using octal notation.

The “chmod” command stands for “change mode”. To use “chmod”, simply type “chmod +x filename” to give the file executable permissions, or “chmod u+w filename” to give the owner write permissions.

You can also use “chmod -x filename” to remove executable permissions, or “chmod u-w filename” to remove write permissions.

In this blog post, we discussed two ways to check file permission in Linux- using the ls command and using the stat command. We also discussed what each of the ls output means and how you can change file permissions. Stay safe out there!

Procedure to check file permissions in Linux

Instructions

  1. Open the terminal application.
  2. Type ls -l command
  3. Press Enter to run the command.
  4. This will give you a list of all the files in the current directory, along with their permissions.
  5. Each permission is represented by a single letter- r for read, w for write, and x for execute.

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.

Skip to Instructions