Today, we're diving into a fundamental concept in Linux: file permissions.
Understanding file permissions is crucial for maintaining security
and proper access control in any Linux environment.
So, let's break it down.
First, let's talk about ownership.
In Linux, every file or directory is assigned three types of owners:
the User, the Group, and Others.
The user is the owner who created the file or directory.
They have the most control over it.
The group consists of users who share the same permissions to access the file or directory.
Others are users who are neither the owner nor members of the group.
They have the least control.
To manage access, Linux uses three types of permissions: Read, Write, and Execute.
Read allows a user to open and read file contents.
Write allows modifying or editing file contents.
Execute allows the file to be run as a program.
Permissions are represented in three ways: Binary,
Octal, and String Representation. Consider the permission `rwx`.
In Binary, it’s 111, where each bit represents a permission:
Read is 1, Write is 1, and Execute is 1.
In Octal, this is represented as 7, with Read being 4, Write being 2,
and Execute being 1, added together.
In String Representation, it's simply `rwx`.
Let’s consider a practical example.
Suppose you have a file where the owner has full permissions, which is `rwx` and translates to 7.
The group can read and write, which is `rw-` and translates to 6.
Others can only read, which is `r--` and translates to 4.
In octal, you would represent this as `764`.
Using the `chmod` command, you can set these permissions like this. `chmod 764 filename`.
The owner can read, write, and execute the file,
the group can read and write, and others can only read.
Now, let's talk about special permission bits: setuid, setgid, and the sticky bit.
These provide additional control over files and directories.
The setuid bit, when set on an executable file, allows it to run with the permissions of the
file owner, not the user running the file. This is useful for tasks that require higher privileges.
For example, the passwd command, which changes user passwords, has the setuid bit set.
This allows regular users to change their passwords,
which requires modifying sensitive system files.
When the setuid bit is set, the execute bit for the owner shows as s.
The setgid bit can be set on files or directories.
On an executable file, it allows the file to run
with the permissions of the group that owns the file.
On a directory, it ensures that new files and subdirectories created
within that directory inherit the group ownership of the directory.
This is useful for shared project directories, ensuring all files created
within have the same group ownership, allowing team members the necessary access permissions.
When the setgid bit is set, the execute bit for the group shows as s.
The sticky bit, when set on a directory, ensures that only the file owner, the directory owner,
or the root user can delete or rename the files within the directory.
This is commonly used on directories like `/tmp` to prevent users from deleting each other’s files.
When the sticky bit is set, the execute bit for others is shown as t.
To set these special permissions, you can use the chmod command with specific
symbols or numeric representations.
For example, let's say you want to set the setuid bit on an executable file named important_script,
along with the typical permissions of read, write, and execute for the owner,
and read and execute for the group and others.
In this command, 4 sets the setuid bit, 7 gives the owner read, write,
and execute permissions, and 5 gives the group and others read and execute permissions.
Understanding these permissions and special bits is essential for managing access and
ensuring security in your Linux environment.
Correctly setting file permissions and special bits can prevent unauthorized access
and accidental modifications, safeguarding your data and ensuring smooth collaboration.
That’s it for today’s video on Linux file permissions.
If you like our videos, you might like our system design newsletter as well.
It cover topics and trends in large scale system design.
Trusted by 500,000 readers.
Subscribe at blog.bytebytego.com.