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!

  • The most efficient way to check file permissions in Linux is 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, we can also use the “stat” command instead of ls. The “stat” command takes a few more options, 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.

 

Understanding file permissions in Linux

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).

 

Let’s see an example.

rw-r--r--

In this example, 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.

This is called Symbolic mode. This mode is used to set permissions for a file or directory using a combination of letters and symbols. 

To learn more about file permissions, I would highly recommend the other guide of ours going in depth of file permissions.

A Beginner’s Guide to Linux File Permissions

Understanding numeric mode of file permissions in Linux

There is another mode of file permissions: numeric mode.

Every file or directory on a Linux system has its own set of permissions and each permission can represented by a number.

The most common numerical representation is 3-digit notation for example 744. It consists of three numbers between 0 – 7 that represent user, group, and other respectively.

Lets see an example.

In the permission value 744, the first digit corresponds to the user, the second digit to the group, and the third digit to others. The first number is 7 then it indicates read write and execute access are granted to the owner of file. 

It looks like this:

Owner: rwx = 4+2+1 = 7
Group: r– = 4+0+0 = 4
Others: r– = 4+0+0 = 4

So its owner  has read, write, and execute permissions for this file and all other users only has read permission. 

Here’s a list of the octal values for different file permissions:

  • 0 = — (no permissions)
  • 1 = –x (execute only)
  • 2 = -w- (write only)
  • 3 = -wx (write and execute)
  • 4 = r– (read only)
  • 5 = r-x (read and execute)
  • 6 = rw- (read and write)
  • 7 = rwx (read, write, and execute)

 

Let’s see another example.

So, if you wanted to set the owner to have read, write, and execute permissions (rwx), the group to have read and execute permissions (r-x), and others to have no permissions (—), you can use an octal value of 740

File permission examples in Linux

Here are more commonly used file permission examples.

Some file permission examples:

777 – all can read/write/execute (full access).
755 – owner can read/write/execute, group/others can read/execute.
644 – owner can read/write, group/others can read only.

Some directory permission examples:

777 – all can read/write/search.
755 – owner can read/write/search, others and group can only search.

Check file permissions with ls command in Linux

The ls -l command is a more efficient way to check file permissions in Linux. It specifically displays file permissions in a detailed format along with other information such as file size, last modification date, owner, group, and file name.

To use this method, simply type “ls -l filename”. Replace “filename” with the name of the file or folder you are looking for.

Let’s say if I want to find the permission of a file called “testfile”, I will type “ls -l testfile”. From 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

  • File type: –
  • Permission settings: rw-r–r–
  • Extended attributes: dot (.)
  • User owner: howtouselinux
  • Group owner: staff

 

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 other 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 third line shows the owner’s permissions, group permissions, and other permissions.

In this case, the file permission is 644( rw-r–r–). 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 see how to change them with chmod command.

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.

 

Command Description
chmod 700 file.txt Set permissions to read/write/execute for owner, no permissions for others
chmod 755 file.txt Set permissions to read/write/execute for owner, read/execute for others
chmod 644 file.txt Set permissions to read/write for owner, read-only for others
chmod +x file.txt Add execute permission for owner, group and others
chmod u+rw,g-w,o-rwx file.txt Add read/write for owner, remove write for group, remove all for others
chmod -R 700 dir Set permissions to read/write/execute for owner, no permissions for others recursively for directory ‘dir’

Case study: file permissions for SSH private key file

Some files in Linux have special files permissions. I will use SSH private key as an example below.

An SSH private key is an encryption key used for secure communication between a client and a server.

It is a part of the SSH (Secure Shell) protocol and is used to establish secure connections over an insecure network, such as the Internet.

The private key is kept on the client machine and is used to decrypt the data that is encrypted with the corresponding public key on the server.

If the private key file has too permissive permissions, it can be read by unauthorized users.

This would allow them to use the private key to gain access to the server, potentially compromising sensitive information.

As we can see that this is a very important file. Allowing others to read the contents of the file can be a security risk.

The permissions for a private SSH key file should be set to 600 (read/write for the owner, no permissions for others).

This ensures that only the owner of the key file can read its contents and use it for authentication.

Case study: Permission denied error in Linux

Permission denied error  is a common issue I met before. This can happen for a variety of reasons, but the most common cause  is that the user does not have the necessary privileges to complete an operation.

I was confused when I got this error at first time because I had always been able to access that directory without any issues.

After the cross checking, I realized that the problem was with the permissions of the directory.

I used the ls -l command to check the permissions of the directory and noticed that the x permission was not set  for some reason. The x permission allows a user to enter a directory.

Then I switched to root account and used the chmod command to add the x permission. 

chmod +x directory_name

Problem solved.

So in order to resolve a permission denied error, the user must acquire the necessary privileges from their system administrator or root user.

Conclusion

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

I hope you will find this guide helpful. If you face any issue while command execution or want to suggest what should I cover next, let me know in the comments.

 

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn