KevsRobots Learning Platform
28% Percent Complete
By Kevin McAleer, 5 Minutes
File permissions and ownership are critical for system security and multi-user environments. This lesson will teach you how to view and modify file permissions and ownership.
chmod
.chown
.Every file and directory in Linux has a set of permissions that determine who can read, write, or execute it. These are represented by a series of letters and dashes, like -rwxr-xr-x
.
Permissions are divided into three groups:
In Unix-like operating systems (like Linux), every file and directory has a set of permissions that determine who can read, write, or execute them. These permissions control what different users can do with a file or directory.
r
- Read:
r
), you can open and view the contents of a file or list the contents of a directory.w
- Write:
w
), you can modify the contents of a file or make changes within a directory.x
- Execute:
x
), you can run the file as a program or script.cd
command) and access the files within it.Permissions are set for three types of users:
Let’s say you have a file with the following permissions: rwxr-xr--
.
rwx
): The owner can read, write, and execute the file. This means they can do anything with the file—open it, edit it, run it as a program, etc.r-x
): The group can read and execute the file, but they cannot modify it. They can view the file and run it if it’s a script or program, but they can’t change its contents.r--
): Everyone else can only read the file. They can’t modify or execute it.Permissions ensure that files and directories are secure and that only the right people can access or modify them. Understanding these basics will help you manage your files better and keep your system safe.
r
): You can look at the contents.w
): You can change or delete the contents.x
): You can run the file as a program or enter the directory.Learning to work with these permissions is an essential skill when using the command line, especially in multi-user environments like Linux servers or shared systems.
To view the permissions of a file, use the ls -l
command:
ls -l example.txt
The chmod
command is used to change file permissions. For example, to make a file executable by the owner, use:
chmod u+x example.sh
the u+x
means add execute permission for the owner. You can also use g
for group and o
for others:
chmod g+r example.sh
chmod o-w example.sh
You can also use numeric values to set permissions:
chmod 755 example.sh
The numeric values are calculated as follows:
The sum of these values gives the permission level. For example, 755
means:
The chown
command changes the ownership of a file. For example, to change the owner of example.txt to pi:
sudo chown pi example.txt
In this example, pi
is the new owner.
To change the group ownership:
sudo chown :pi example.txt
In this example, pi
is the new group owner.
Command | Description |
---|---|
chmod |
change file permissions - change mode |
chown |
change file ownership - change owner |
In this lesson, you learned how to view and modify file permissions and ownership using the chmod
and chown
commands. Properly managing permissions is essential for securing your Raspberry Pi.
You can use the arrows ← →
on your keyboard to navigate between lessons.