Linux for Beginners Part 6 (Understanding File Permission and Ownership)

Linux For Beginners 6 Understanding File Permission and Ownership Main Logo

Linux for Beginners Part 6 (Understanding File Permission and Ownership)

In this video tutorial, we continue to show you how to use Command Line On Linux Operating System.

After you’ve worked with Linux for a while, you are almost sure to get a Permission denied message. Permissions associated with files and directories in Linux were designed to keep users from accessing other users private files and projects important system files.

The nine bits assigned to each file for permission define the access that you and others have to your file. Permission bits for a regular file appear as -rwxrwxrwx. Those bits are used to define who can read, write, or execute the file.

Note. For a regular file, a dash appears in front of the nine-bit permission indicator. Instead of a dash, you might see:

  • d (for directories),
  • 1 (for a symbolic link),
  • b (for a block device),
  • c (for a character device),
  • s (for a socket),
  • p (for a named pipe).

Of the nine-bit permission, the first three bits apply to the owner’s permission, the next three apply to the group assigned to the file, and last three apply to all others.

  • The r stands for reading permissions,
  • The w stands for write permissions,
  • The x stands for execute permissions.

If a dash appears instead of the letter, it means that permission is turned off for that associated read, write, execute bit.

Because files and directories are different types of elements, read, write, and execute permissions on files and directories mean different things.

In this Table, you can find what you can do of each of them.

Linux For Beginners 6 Understanding File Permission and Ownership Photo 1

And as noted earlier, you can see the permission for any file or directory by typing the ls -ld command. The name file or directory appears as the shown in this example:

$ ls -ld file
-rw-rw-r-- 1 pi pi 4096 Oct 11 13:43 file

The dash at the beginning indicates that it’s a file and it has read, write permission for the owner and the group. All other users have read permission, which means they can view the file but cannot change its contents or remove it.

Note.If you had not used the -d options to ls, you would have listed files in your directory instead of permissions of that directory.

Changing permissions with chmod (numbers)

If you own a file, you can use the chmod command to change the permission on it as you please. In one method of doing this, each permission (read, write, execute) is assigned a number:

  • r=4,
  • w=2,
  • x=1.

And you use each set’s total number to establish the permission.

For example, to make permission wide open for yourself as owner, you would set the first number to 7 (4+2+1), and then you would give the group and others read-only permission by setting both the second and third numbers to 4 (4+0+0) so that the final number is 744. Any combination of permissions can result from 0 (no permission) through 7 (full permission).

Here are some examples of how to change permission on file (name file) and what the resulting permission would be:

The following chmod command results in this permission: rwxrwxrwx

$ chmod 777 file

The following chmod command results in this permission: rwxr-xr-x

$ chmod 755 file

The following chmod command results in this permission: ———

$ chmod 000 file

The chmod command can also be used recursively. For example, suppose you want to give an entire directory structure 777 permission (rwxrwxrwx), starting at the home pi/file directory which in my case. To do that, you could use -R option, as follow:

$ chmod -R 777 file

And if you did not watch a previous part where we have discussed a how you can use brace expansion characters command then we highly recommend to check this out!

For this type of project, you will need to install and run the Linux OS of any version that you like.

In our case, we will be using Raspberry PI 3 as a Linux source, since it’s much easier to connect to it than just run a virtual machine on your main OS if you do not use Linux as your primary OS.

What you need for this project:

  • A computer with a Linux OS installed on it.

Note. In this project, we will not explain how to use the Putty software to connect Telnet to your Raspberry Pi.

If you are interested, you can watch the video: