Linux File: File Permissions and Linux Commands for Beginners

Introduction:

Unix is a multi-user operating system that may be used by several people at the same time. Linux is a clone of Unix. The Linux operating system may also be utilized in mainframes and servers without any adjustments. Nevertheless, this creates security problems, as an unauthorized or malicious user might modify or delete critical data. Linux separates authorization into two tiers for optimal security, i.e. Ownership and Permission.

Table of contents:

  • Linux file ownership
  • Linux file permissions
  • Changing a file’s permission
  • Conclusion

Linux File Ownership

A Unix/Linux system has three sorts of owners for each file and directory, as shown below.

  1. User – An individual user has full control over the file. A file’s owner is automatically assumed to be the person who generated it. Consequently, a user can also be referred to as an owner.
  1. Group – Multiple users can be part of a single user group. The file will be accessible to all members of a group with the same Linux group permissions. Let’s say you’re working on a project that calls for many people to have access to the same file. Instead of providing permissions one-by-one to individual users, you could create a group with all of your users and then provide the members of that group read-only or read/write access to the files.
  1. Other – Any additional user that has access to a file. This individual did not create the file and is not a member of a user group that might possess it. To put it another way, it refers to anybody and everyone else. As a result, when you provide rights to others, you are also granting permissions to the world.

Now, the main question is: how can Linux discriminate between these three categories of users such that a user ‘A’ cannot modify a file that holds essential information and data for another user ‘B? It’s as if you don’t want your Linux-using coworker to see your photographs. Permissions play a key role in determining how a user behaves.

Checkout more for Linux Interview Questions to crack the interview easily.

Linux File Permissions

UNIX/Linux system files and directories have the following three permissions for each of the three owners stated above:

  1. Read – You can open and read a file with this permission. A directory’s read permission enables you to view its contents.
  1. Write – This allows you to make changes to a file’s content if you have the write permission. You can add, remove, and rename files in a directory if you have “write” permissions. A situation like this could arise when the directory in which the file is kept doesn’t let you write access. There is an option to edit the contents of the file. But you won’t be able to rename, transfer, or delete the file.
  1. Execute – Executable programs in Windows have the extension “exe” and may be readily launched. Unix/Linux does not allow you to launch a program unless you have the right to execute it. Even if you have the ability to see and alter the program’s source code, you may not be able to actually run it.

Changing a File’s Permissions

The “chmod” command is used to modify a file’s / directory’s permissions. The chmod command requires two arguments, as seen below:

chmod (permission-to-be-assigned) (path-to-file)

The “+” and “-” symbols are used to assign permissions. Consider the following examples:

  1. To provide user rights, use “u+” in conjunction with the chmod command, as demonstrated below:

    chmod u+x demo.sh

chmod u+rw demo.sh

chmod u+rwx demo.sh

  1. To revoke the user’s access, use the “u-” command as shown below. This will remove all of the specified permissions.

    chmod u-x demo.sh

chmod u-rw demo.sh

chmod u-rwx demo.sh

Similarly, you may substitute “g” and “o” for “u” when providing permissions to groups and others.

  1. To simultaneously give rights to ugo, use the following syntax:

    chmod ugo+x demo.sh
  1. Change Permissions Recursively

At some time, you may wish to modify the folder’s, subfolder’s, and files’ permissions. For instance, suppose you’ve uploaded the website’s contents to the apache webserver and wish to modify the permissions on all the files and folders in the pictures folder. In this scenario, you must apply the permission modification. As illustrated below, recursively use the “-R” flag with the “chmod” command.

chmod -R 755 demodir

  1. Sticky bit Permission

The sticky bit is used to specify the permissions for a file or folder. Once configured, only the owner of the file or directory and root will be able to delete it. This privilege is extremely useful in circumstances when numerous users need to share a folder. A sticky bit prevents other users from purposefully or accidentally deleting files. Explore Linux File Permissions for more information.

  1. Stickybit Configuration

Using “t” or “1”, you may add a sticky bit to a file or folder. Here is an illustration:

chmod +t demo.sh

chmod 1755 demo.sh

In the above command, 1 denotes the sticky bit, and 755 denotes the standard file permissions. To verify that the sticky bit has been assigned, run “ls -la” and check whether a T flag has been added to the permission block, as shown below:

-bash-4.2$ ls -l

-rwxr-xr-t. 1 vagrant vagrant 0 May  2 02:51 demo.sh

Conclusion 

This blog describes all of the file permissions and Linux commands that you must know in order to protect your data’s confidentiality. We hope this blog proves beneficial to you as you continue your Linux learning journey.

Author Bio

Sai Priya Ravuri is a Digital Marketer, and a passionate writer, who is working with MindMajix, a top global online training provider. She also holds in-depth knowledge of IT and demanding technologies such as Business Intelligence, Machine Learning, Salesforce, Cybersecurity, Software Testing, QA, Data analytics, Project Management and ERP tools, etc.