Changing permissions on folders and directories in Linux/Unix
The file permission system is one of the fundamental security elements of the Linux operating system.
These features allow us to adjust file permissions and access modes. We provide permissions based on appropriate rights to avoid vulnerabilities that can occur when users are not given appropriate access rights to files and folders.
File ownership in Unix/Linux systems
In Unix/Linux systems, there are three types of ownership of files and directories. The assigned owner of a file has the right to change it according to the permissions it receives.
- Owner Permissions: The permissions of a file owner specify the functions that the file owner can perform. Since the user who creates a file becomes its owner, the user is also called the owner.
- Permissions by group: A group's permissions determine what activities a user can perform on a file if they are a member of that group.
- Additional permissions: Other users' permissions define what actions they can perform on the file.
Each file initially has three access options. They are as follows:
-
Read (r)
: It allows you to retrieve the contents of files, but not write to them. It will enable you to receive a listing of files and directories found in a directory. -
Write (w)
: It enables you to add new data to files or change existing data, as well as create and modify files and directories. -
Execute (x)
: If a program does not have the execute flag, it cannot be run. Set this property for all programs and scripts. The system uses it to determine whether a file should be started as a program.
Check File Permissions in Linux
Of course, you can use the file manager on Linux to search for file permissions. They all support this feature, but you will receive partial information this way.
Use the command with -l
the -h flag ls
to get full details of all flags. All files in the directory will be displayed, along with their attributes and bits.
Run the following command in the folder where the file is stored to find the Linux file permissions:
$ ls -l
I created a folder and two files in it , test
.testfile
Output:
total 0
-rw-rw-r-- 1 user user 0 Jan 14 01:00 test
-rw-rw-r-- 1 user user 0 Jan 14 01:00 testfile
The first column indicates the various access modes or permissions associated with the file or directory. Then the rights groups, first the owner, then the group, then everyone else.
Let's take a closer look at what the conditional values for the permission flags mean:
-
---
- No rights at all; -
--x
- Allow the file to be executed only as a program, but not changed or read; -
-w-
- Only writing and modifying files is allowed; -
-wx
- Allows modification and execution, but in case of directories, you cannot view its contents; -
r--
- Read-only permission; -
rx
- Only read and execute, no write permission; -
rw-
- Read and write permissions, but not execute; -
rwx
- All rights; -
--s
- When theSUID
orSGID
bit is set, the owner'sSUID
is displayed in the field and the group'sSGID
is displayed in the field; -
--t
- The sticky-bit is set, which means that users cannot delete this file.
Changing file permissions in Unix/Linux systems
chmod
is a change mode command that is used to change the permissions of a file or directory. chmod
The two modes of use are symbolic mode and absolute mode.
Use in symbolic modechmod
Symbolic mode is the most direct way for learners to adjust file or directory permissions.
Using the operators in the following table, you can add, remove, or specify the permission sets that you want with symbolic permissions.
chmod Operators |
describe |
---|---|
+ |
The specified permissions are added to the file or directory. |
- |
Removes specified permissions from a file or directory. |
= |
Set permissions for the specified user. |
Let's look at an example. When you run in a test ls -l
, you'll see the permissions look like this:
$ ls -l test
Output:
-rw-rw-r-- 1 user user 0 Jan 14 01:00 test
Then run each of the examples in the table above in a test chmod
command, and then run ls -l
to see the permissions change.
$ chmod o+wx test
$ ls -l test
Here, o
represents other users, w
represents write, and x
represents execute. +
It is used to add the specified permissions to test
the file.
Output:
-rw-rw-rwx 1 user user 0 Jan 14 01:00 test
We can see the difference in the two outputs above. The -rw-rw-r--
above -rw-rw-rwx
command adds rw
permissions for other users.
Changing Group and Owner in Linux
When they create an account on Unix, each user is given an owner ID and a group ID. All the above permissions are also assigned based on the owner and group.
There are two main commands for changing the group and owner.
-
chown
:chown
Command, stands forchange owner
, used to change the owner of a file. -
chgrp
:chgrp
Command, stands forchange group
, used to change the group of a file.
The permission groups used are:
-
u
– Owner -
g
- Group -
o
- other -
a
- All users
Change Ownership Command in Linux
chown
command is used to change the ownership of a file.
Following is the basic syntax:
$ chown [name] [filename]
The value for user can be the name of a system user or the user ID (UID) of a system user.
Likewise, to change the group owner of a file:
$ chgrp [group_name] [filename]
Use in absolute mode in Linuxchmod
File permissions can also be changed using numeric codes. The equivalent numeric codes for access methods are as follows:
number | Access Mode |
---|---|
0 |
--- |
1 |
-x |
2 |
-w- |
3 |
-wx |
4 |
r- |
5 |
r-x |
6 |
rw- |
7 |
rwx |
For example, chmod 711 filename
grant all permissions to the owner, and only execute permissions to all other users.
Let's look at the file testfile
permissions.
$ ls -l testfile
Output:
-rw-rw-r-- 1 user user 0 Jan 14 01:00 testfile
Now, use chmod
the numeric code with .
$ chmod 711 testfile
$ ls -l testfile
Output:
-rwx--x--x 1 user user 0 Jan 14 01:00 testfile
As shown in the output, the file permissions have been changed.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u