Chmod +rwx Command on Linux Systems. (Permissions)
The command name chmod
stands for "change mode." It restricts the way a file can be accessed. In Linux, access to the files is managed through the file permissions, attributes, and ownership. This ensures that only authorized users and processes can access files and directories. That makes chmod
command one of the most powerful and important commands in Linux.
Understanding File Permissions
On a Linux system, each file and directory is assigned access rights for:
- The file owner.
- The group members.
- Others (everybody else).
and three file permissions types apply to each class:
- The read permission.
- The write permission.
- The execute permission. (i.e. run the file as a program).
This allows you to specify which users are allowed to read the file, write to the file, or execute the file.
File permissions can be viewed using the ls
command:
$ ls -l monkey.txt
Output
-rw-r--r-- 12 snubmonkey batman 24.0K Feb 12 08:28 monkey.txt
|[-][-][-]- [------] [---]
| | | | | | |
| | | | | | +-----------> 7. Group
| | | | | +-------------------> 6. Owner
| | | | +--------------------------> 5. Alternate Access Method
| | | +----------------------------> 4. Others Permissions
| | +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type:
- indicates regular file
d indicates directory
In our above example (rw-r--r--
) means that the file owner has read and write permissions (rw-
), the group and others have only read permissions (r--
).
The first character identifies the type of entry that is being listed. If it is a dash (-
) it is a file. If it is the letter d
it is a directory.
The next nine characters represent the settings for the three sets of permissions.
- The first three characters show the permissions for the user who owns the file (user permissions).
- The middle three characters show the permissions for members of the file’s group (group permissions).
- The last three characters show the permissions for anyone not in the first two categories (other permissions).
There are two ways to represent these permissions: with symbols (Alphanumeric characters), or with Octal numbers (the digits 0 through 7).
Symbolic (Text) Method #
In the symbolic mode, you can modify the permissions of a specific owner. It makes use of mathematical symbols to modify the Unix file permissions.
Operator Description
+ Adds a permission to a file or directory
- Removes the permission
= Sets the permission and overrides the permissions set earlier.
The various owners are represented as -
User Denotations
u user/owner
g group
o other
a all
Now, let's look into an example so we can have a better understanding.
$ ls -l monkey.txt
-rw-r--r-- 12 snubmonkey batman 24.0K Feb 12 09:22 monkey.txt
*let's set permissions to the 'other' users.
$ chmod o=rwx monkey.txt
ls -l
-rw-r--rwx 12 snubmonkey batman 24.0K Feb 12 09:24 monkey.txt
*let's set an execute permission to the usergroup.
$ chmod g+x monkey.txt
ls -l
-rw-r-x-rwx 12 snubmonkey batman 24.0K Feb 12 09:27 monkey.txt
*let's remove 'read' permission for 'user'.
$ chmod u-r monkey.txt
ls -l
--w-r-x-rwx 12 snubmonkey batman 24.0K Feb 12 09:29 monkey.txt
*let's Remove the read, write, and execute permission for all users except the file’s owner:
$ chmod og-rwx monkey.txt
ls -l
--w------- 12 snubmonkey batman 24.0K Feb 12 09:31 monkey.txt
*let's add the file’s owner permissions to the permissions that the members of the file’s group have
$ chmod g+u monkey.txt
ls -l
--w--w---- 12 snubmonkey batman 24.0K Feb 12 09:34 monkey.txt
Octal (numbers) Method #
Simply put, in this mode, file permissions are not represented as characters but as a three-digit octal number.
The table below gives numbers for all permissions types.
Number Permission Type Symbol
0 No Permission ---
1 Excetute --x
2 Write -w-
3 Excetue + Write -wx-
4 Read r--
5 Read + Excecute r-x
6 Read + Write rw-
7 Read + Write + Excecute rwx
*let's check the current file and the chmod
permissions command in action.
ls -l
--w--w---- 12 snubmonkey batman 24.0K Feb 12 09:44 monkey.txt
*let's chmod 764
and check permissions again.
chmod 764 monkey.txt
-rwxrw-r-- 12 snubmonkey batman 24.0K Feb 12 09:46 monkey.txt
'764' octal code says the following:
- Owner can read, write and execute
- Usergroup can read and write
- World can only read
This is shown as '-rwxrw-r--'
Recursively Change the File’s Permissions
To recursively operate on all files and directories under the given directory, use the -R
(--recursive
) option:
For example, to change the permissions of all files and subdirectories under the /var/www
directory to 755
you would use:
chmod -R 755 /var/www
This is how you can change user permissions in Linux on file by assigning a Symbolic value or an octal number.
Thank you for reading and hope you've learned something.
To learn more about chmod
type man chmod
in terminal.