Linux file permissions explained

Linux file permissions explained

Overview

In the Linux operating system, file permissions play a crucial role in ensuring data security and controlling access to files and directories. Understanding how file permissions work is essential for both system administrators and regular users. This comprehensive guide will explain the concepts of file permissions in Linux, covering the different permission types, symbolic and numeric representation, and practical examples of how to manipulate file permissions using commands.

Permission Types

In Linux, each file or directory has three types of permissions: read (r), write (w), and execute (x). These permissions can be granted to three different user groups: the owner (u), the group (g), and others (o). Together, they form a 3×3 matrix, representing the permissions for each user group.

The symbolic representation of file permissions uses a combination of letters and symbols. The three permission types are represented by ‘r’ (read), ‘w’ (write), and ‘x’ (execute). The user groups are represented by ‘u’ (owner), ‘g’ (group), and ‘o’ (others). Additionally, a ‘-‘ (dash) symbol is used to indicate the absence of a particular permission.

Each permission type is assigned a numeric value: read (4), write (2), and execute (1). To calculate the numeric representation for each user group, the sum of the values for the allowed permissions is calculated.

Viewing and Changing File Permissions

To view file permissions in Linux, you can use the ls -l command. The output will display the file permissions, owner, group, file size, and other metadata. The chmod command is used to change file permissions in Linux. It can be used with symbolic or numeric representation.

Changing Permissions Using Symbolic Representation:

To modify file permissions using the symbolic representation, the chmod command is followed by the permission type (+ or -), the user group (u, g, o), and the permission type (r, w, x). For example:

chmod u+x myfile.txt   # Grants execute permission to the owner.
chmod go-w myfile.txt  # Removes write permission for the group and others.

Changing Permissions Using Numeric Representation:

To modify file permissions using the numeric representation, the chmod command is followed by a three-digit number, where each digit represents the permission value for the user group (u, g, o). For example:

chmod 640 myfile.txt   # Grants read and write permission to the owner, read-only for the group, and no access for others.
chmod 755 myscript.sh # Grants read, write, and execute permissions to the owner, and read and execute permissions to the group and others.

Changing Ownership and Group

chown Command:

The chown command is used to change the ownership of a file or directory. It takes the user name or user ID as an argument.

chown username myfile.txt   # Changes the ownership of myfile.txt to the user 'username'.

chgrp Command:

The chgrp command is used to change the group ownership of a file or directory. It takes the group name or group ID as an argument.

chgrp groupname myfile.txt   # Changes the group ownership of myfile.txt to the group 'groupname'.

Setuid (SUID) and Setgid (SGID):

Setuid and setgid are special permissions that allow a file to be executed with the privileges of the owner or group, respectively. They can be set using the chmod command with symbolic or numeric representation.

chmod u+s myscript.sh   # Sets the Setuid permission on myscript.sh, allowing it to be executed with the owner's privileges.
chmod g+s mydirectory    # Sets the Setgid permission on mydirectory, causing new files created within it to inherit the group ownership.

Sticky Bit:

The sticky bit is a permission that can be applied to directories. When the sticky bit is set, only the owner of a file within that directory can delete or rename it. The sticky bit can be set using the chmod command with symbolic or numeric representation.

chmod +t shared_directory   # Sets the Sticky Bit permission on the directory 'shared_directory', restricting file deletion or renaming to only the file owner.
chmod 1777 /tmp            # Sets the Sticky Bit permission on the /tmp directory using numeric representation (1777), ensuring only the file owner can delete or rename files.

Conclusion

File permissions are an integral part of Linux systems, providing security and control over file access. Understanding how to view, modify, and manage file permissions is essential for effective system administration and data protection. By using these example commands, you can effectively change ownership, and group ownership, and apply advanced permissions to files and directories in Linux.

Leave a Reply