Unix / Linux - File Permission / Access Modes
Hello there, aspiring programmers! Today, we're diving into the fascinating world of Unix and Linux file permissions. Don't worry if you're new to this; by the end of this tutorial, you'll be a file permission pro! Let's embark on this exciting journey together.
The Permission Indicators
Imagine you're the guardian of a treasure chest. You need to decide who can open it, who can put things in, and who can take things out. That's essentially what file permissions are all about in Unix and Linux!
When you list files using the ls -l
command, you'll see something like this:
-rwxr-xr-x 1 john users 2048 Jan 15 2023 myfile.txt
Let's break down those mysterious letters at the beginning:
- The first character indicates the file type (
-
for regular file,d
for directory) - The next 9 characters represent the permissions for user, group, and others
Here's a handy table to remember what each letter means:
Letter | Meaning |
---|---|
r | Read permission |
w | Write permission |
x | Execute permission |
- | No permission |
File Access Modes
Now, let's talk about what these permissions actually allow you to do with files:
- Read (r): View the contents of the file
- Write (w): Modify or delete the file
- Execute (x): Run the file as a program or script
Here's a fun way to remember: Think of "r" as "read the recipe", "w" as "write the recipe", and "x" as "execute the recipe" (cook the dish)!
Directory Access Modes
Directories have similar permissions, but they work a bit differently:
- Read (r): List the contents of the directory
- Write (w): Add or remove files in the directory
- Execute (x): Access the directory and its contents
Imagine a directory as a room. "r" lets you peek inside, "w" lets you add or remove furniture, and "x" lets you enter the room.
Changing Permissions
Now that we understand permissions, let's learn how to change them! We use the chmod
command for this. There are two ways to use chmod
: symbolic mode and absolute mode.
Symbolic Mode
This is like giving instructions to add or remove permissions. Here's the basic syntax:
chmod [who][+,-,=][permissions] filename
-
who
can be u (user), g (group), o (others), or a (all) -
+
adds permission,-
removes it,=
sets it exactly -
permissions
are r, w, or x
For example:
chmod u+x myfile.txt
This adds execute permission for the user.
Using chmod with Absolute Permissions
Absolute mode uses numbers to set permissions. Each permission has a value:
- r = 4
- w = 2
- x = 1
We add these up for each category (user, group, others). Here's a table to help you understand:
Number | Permission |
---|---|
0 | --- |
1 | --x |
2 | -w- |
3 | -wx |
4 | r-- |
5 | r-x |
6 | rw- |
7 | rwx |
For example:
chmod 755 myfile.txt
This sets rwx for the user, and rx for group and others.
Changing Owners and Groups
Sometimes, you need to change who owns a file or which group it belongs to. Let's look at how to do that.
Changing Ownership
To change the owner of a file, we use the chown
command:
chown newowner filename
For example:
chown alice myfile.txt
This changes the owner of myfile.txt to alice.
Changing Group Ownership
To change the group, we use the chgrp
command:
chgrp newgroup filename
For example:
chgrp developers myfile.txt
This changes the group of myfile.txt to developers.
SUID and SGID File Permission
Now, let's talk about some special permissions: SUID (Set User ID) and SGID (Set Group ID). These are like superpowers for files!
- SUID: When set on an executable file, it runs with the permissions of the owner.
- SGID: When set on an executable file, it runs with the permissions of the group.
To set these, we use chmod
with a special number:
chmod 4755 myfile # Set SUID
chmod 2755 myfile # Set SGID
The 4 at the beginning sets SUID, and 2 sets SGID.
And there you have it! You're now well-versed in the world of Unix/Linux file permissions. Remember, with great power comes great responsibility. Use these permissions wisely to keep your files safe and secure.
Practice makes perfect, so don't be afraid to experiment (in a safe environment, of course). Before you know it, you'll be managing file permissions like a pro!
Credits: Image by storyset