Understanding File Permissions π
In the world of file permissions, three main characters take center stage: the Owner, the Group, and the Others (everyone else). Each character can have three distinct permissions: Read, Write, and Execute.
π§βπ€βπ§ Owner: This is you, the creator of the file. You have the ultimate say in what happens to it.
π₯ Group: A collection of users who share the file. You might be part of a group if you're working on a team project.
π Others: Everyone else on the system. Changing Permissions with chmod π
But where i can find this permission? and how π€
By using the following approach,
ls -ltr: command is used in Unix-like operating systems, including Linux, to list files and directories in a directory in a specific order and with additional details.
Break it down of that command π:
ls: This is the command for listing files and directories.
-l: This option tells the ls command to provide a long listing format, which includes additional information about each file or directory, such as permissions, owner, group, size, and modification time.
-t: This option instructs the ls command to sort the listed items by modification time, with the most recently modified items displayed at the top.
-r: This option reverses the order of sorting so that the oldest items appear at the top and the most recently modified items appear at the bottom.
Above, You can also see the file.txt, as I mark there -rw-rw-r--
Let's, understand it :
r - read
w - write
x - execute
They contain a total of 10 letters, but at first, there is "-" in some file permissions and some not. who having a "d" letter at first denotes that means they are directories.
Now, remaining the 9 letters stand like permission for the user.
First 3 Middle 3 Last 3
- - - - - - - - -
First 3 stands for Owner
Middle 3 stands for Group
The last 3 stands for Others.
As the file.txt having permission -rw-rw-r-- means
There is no "d" present means it is not a directory.
Owner having: rw- --> Read & Write permission but not for Execute
Group having: rw- --> Read & Write permission but not for Execute
Other having: r-- --> Read permission but not for Write & Execute
Change File Permissions π
To change file permissions, you'll need the chmod command, followed by a three-digit code or symbolic notation.
Numeric Notation π’
Numeric Notation: The numeric code is a combination of 3 digits, each representing a permission group (Owner, Group, Other).
4 represents Read π
2 represents Write π
1 represents Execute π
Add these values together to set the desired permissions.
For example,
So as per the numerical notation, our file.txt has permission 664
but the requirement is,
Owner should have Read+Write+Execute.
Group should have Read+Write.
Others should have Read.
so it will be,
Owner --> Read+Write+Execute (4+2+1)
Group --> Read+Write (4+2)
Others --> Read (4)
chmod 764 file.txt
So, the file permission was changed by Numerical Notation way
Symbolic Notation π£
Symbolic Notation (Alphabetical Notation): Alternatively, you can use symbols to modify permissions. The format is like this:
Owner --> u
Group --> g
Others --> o
The current state of the file Permission: -rwxre-r--
as the current requirement is,
Owner should have Read+Write.
Group should have Read+Write.
Others should have Read.
so,
users owner group others
requirement rw- rw- r--
chmod u=rw g=rw o=r file.txt
chmod u=rw,g=rw,o=r file.txt
So, the file permission was changed in Symbolic Notation (Alphabetical Notation) way.
Want to make a script executable by anyone?
Use the following approach :
chmod +x demoscripts.sh
Change the file ownership π΄
As we know file.txt has permission as the
-rw-rw-r-- 1 ubuntu ubuntu 0 Sep 8 14:10 file.txt
-rw-rw-r--: This is the permission string that indicates the file's permission settings
1: This number typically represents the number of hard links to the file. In this case, there is only one hard link to the file.
ubuntu: This is the username of the file's owner. The file is owned by the user "ubuntu."
ubuntu: This is the name of the group associated with the file. The file belongs to the group "ubuntu."
0: This is the file's size in bytes. In this case, the file "file.txt" is empty, so its size is 0 bytes
Sep 8 14:10 : This is the timestamp indicating the file's last modification time. In this example, the file was last modified on September 8th at 14:10 (2:10 PM)
file.txt: This is the name of the file.
The following way will create a group and add a user to that group
sudo groupadd unique
Check whether the group is created or exists:
getent group unique
Syntax: getent group groupname
To find the users in the Linux system, use the following command :
getent passwd
The "getent passwd" command is used in Unix-like operating systems to query and display user account information stored in the system's user database, typically from the "/etc/passwd" file and other relevant sources like Network Information Service (NIS).
getent: This is the command used to retrieve entries from various databases, including user accounts, group memberships, and more.
passwd: This is an argument passed to getent to specify the database you want to query. In this case, it tells getent to retrieve entries from the user account database.
You can see that we have the users Vipul and Anshul so we will add the users to a unique group.
Using usermod to Add Existing Users to a Group
Syntax: sudo usermod -aG group_name username
The -aG flags stand for: -->
-a: Append (add the user to the group without removing them from other groups).
-G: Specify the group name.
sudo usermod -aG unique Vipul
sudo usermod -aG unique Anshul
but our goal is the I want to change the group ownership of file. It can be changed by following the way.
above is the Group of files is ubuntu so our requirement is the file.txt should behave as the group owner "unique"
Syntax: chgrp new_group file_or_directory
chgrp unique file.txt
So, in this way, you learn about how to create a group, How the users will add in groups and how to change the group permission of the file.
Congratulationsπ, today you learned various ways of file permissions.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like β€, leaving a comment π¬, and spreading the word π’ to your friends and colleagues π