The chmod
command in Linux/Unix systems is used to change the permissions of a file or directory. It stands for "change mode", and allows you to specify who can read, write, or execute a file. The command can be used with either symbolic or numeric modes to modify file permissions.
Here’s a comprehensive guide to using chmod
:
chmod [options] mode file
mode
: Specifies the permissions to be set.file
: The file or directory whose permissions you want to change.options
: Optional flags like-R
for recursive changes.
Each file or directory has three types of permissions:
- Read (
r
): Allows reading the contents of the file. - Write (
w
): Allows modifying the contents of the file. - Execute (
x
): Allows running the file as a program.
These permissions are assigned to three categories of users:
- Owner (User): The user who owns the file.
- Group: Users who are members of the file's group.
- Others: All users who are not the owner or part of the group.
Permissions are displayed as a string of characters, such as rwxr-xr--
, where:
- First three characters (
rwx
) are for the owner. - Next three characters (
r-x
) are for the group. - Last three characters (
r--
) are for others.
In symbolic mode, you specify the user category and the permissions you want to add, remove, or set.
chmod [user][operation][permissions] file
-
user: Specifies the user category:
u
: Owner (user).g
: Group.o
: Others.a
: All users (owner, group, and others).
-
operation: Defines the action:
+
: Add permissions.-
: Remove permissions.=
: Set exact permissions (overwrites existing permissions).
-
permissions: The permissions to add/remove/set:
r
: Read.w
: Write.x
: Execute.
-
Add read permission for the group:
chmod g+r filename
-
Remove write permission for others:
chmod o-w filename
-
Set read and execute permissions for the owner, and read-only for others:
chmod u=rx,o=r filename
-
Add execute permission for everyone:
chmod a+x filename
-
Remove all permissions for others:
chmod o= filename
In numeric mode, you specify the permissions using a three-digit octal number, where each digit represents the permissions for the owner, group, and others. The digits correspond to a sum of the values for read, write, and execute:
Permission | Value |
---|---|
Read (r ) |
4 |
Write (w ) |
2 |
Execute (x ) |
1 |
- The total value for each user category (owner, group, others) is the sum of the values for the permissions you want to grant.
chmod [permissions] file
- The first digit is for the owner, the second is for the group, and the third is for others.
7
: Read, write, and execute (rwx
).6
: Read and write (rw-
).5
: Read and execute (r-x
).4
: Read-only (r--
).3
: Write and execute (wx
).2
: Write-only (w--
).1
: Execute-only (x--
).0
: No permissions (---
).
-
Owner has read, write, and execute permissions; group has read and execute; others have read only:
chmod 755 filename
-
Owner has read and write; group has read only; others have no permissions:
chmod 640 filename
-
Set permissions so that only the owner can read, write, and execute:
chmod 700 filename
-
Make a file readable and executable by everyone:
chmod 555 filename
To apply the chmod
changes recursively to directories and their contents, you can use the -R
option.
chmod -R mode directory
-
Apply read, write, and execute permissions for the owner on all files and subdirectories in a directory:
chmod -R u+rwx directory/
-
Remove execute permissions for others from all files in a directory:
chmod -R o-x directory/
In addition to the standard read, write, and execute permissions, there are special permissions that can be set using chmod
:
-
When the setuid bit is set on an executable file, the program runs with the privileges of the file’s owner, rather than the user who runs it.
- Syntax:
chmod u+s filename
- Numeric value:
4
- Example:
chmod u+s /path/to/executable
- Syntax:
-
When the setgid bit is set on a file, the file runs with the privileges of the file's group.
-
When set on a directory, files created within that directory inherit the group of the directory, rather than the user’s default group.
- Syntax:
chmod g+s filename
- Numeric value:
2
- Example:
chmod g+s /path/to/directory
- Syntax:
-
When the sticky bit is set on a directory, only the file owner, the directory owner, or the root user can delete or rename files within that directory.
- Syntax:
chmod +t directory
- Numeric value:
1
- Example:
chmod +t /path/to/directory
- Syntax:
-
-v
(verbose): Show the changes made to the file permissions.chmod -v 755 filename
-
-c
(changes): Only report changes (if any).chmod -c 755 filename
-
-f
(silent): Suppress most error messages.chmod -f 755 filename
-
--reference=RFILE
: Change the permissions of a file to match those ofRFILE
.chmod --reference=file1 file2
Command | Description |
---|---|
chmod 755 filename |
Set permissions to rwxr-xr-x (owner read/write/exec, group and others read/exec). |
chmod u+x filename |
Add execute permission for the owner. |
chmod g-w filename |
Remove write permission for the group. |
chmod 644 filename |
Set permissions to rw-r--r-- (owner read/write, others read). |
chmod -R 755 directory/ |
Recursively set permissions to rwxr-xr-x for all files in a directory. |
chmod +t /tmp |
Set sticky bit on the /tmp directory. |
The chmod
command is powerful for controlling file and directory permissions in Unix-based systems. Mastering both symbolic and numeric modes is key to efficiently managing access control in a system.
The chmod
command in Unix-like operating systems (Linux, macOS, etc.) is used to change the permissions of files and directories. Permissions control the ability to read, write, and execute a file for different users (owner, group, others). Here’s an in-depth guide to the chmod
command and how to use it.
In Unix-like systems, each file or directory has associated permissions. There are three types of permissions:
- Read (r): The ability to view the content of a file or list the contents of a directory.
- Write (w): The ability to modify the contents of a file or add/remove files in a directory.
- Execute (x): The ability to run a file as a program or script.
Permissions are typically set for three categories of users:
- Owner (u): The user who owns the file.
- Group (g): Other users who belong to the same group as the file.
- Others (o): All other users who do not own the file or belong to the group.
chmod [options] mode file
mode
: Specifies the permissions you want to set. It can be expressed either symbolically (letters) or numerically (octal).file
: The file or directory to which the permissions should be applied.
In symbolic mode, permissions are set using combinations of letters representing the user categories and permissions:
- User categories:
u
(user),g
(group),o
(others),a
(all). - Permissions:
r
(read),w
(write),x
(execute). - Operators:
+
(add),-
(remove),=
(set exactly).
-
Add execute permission for the user:
chmod u+x filename
This adds execute permission to the file for the owner.
-
Remove write permission for the group:
chmod g-w filename
This removes the write permission from the group.
-
Set read and execute permissions for others:
chmod o+rx filename
-
Set permissions for all (user, group, others):
chmod a+r filename
This gives read permission to all categories of users.
-
Set exact permissions:
chmod u+x,g+w,o-r filename
This adds execute permission to the user, write permission to the group, and removes read permission from others.
-
Remove all permissions from others:
chmod o= filename
In numeric mode, permissions are represented by three digits. Each digit is a sum of the values for the permissions:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
The numeric representation consists of three digits:
- The first digit sets permissions for the user (owner).
- The second digit sets permissions for the group.
- The third digit sets permissions for others.
Each digit is the sum of the permissions:
- 7 = read (4) + write (2) + execute (1) = rwx
- 6 = read (4) + write (2) = rw-
- 5 = read (4) + execute (1) = r-x
- 4 = read (4) = r--
- 3 = write (2) + execute (1) = wx
- 2 = write (2) = w--
- 1 = execute (1) = x--
- 0 = no permissions = ---
-
Set read, write, and execute permissions for the user, and read and execute for group and others:
chmod 755 filename
This sets:
- User: rwx (7)
- Group: r-x (5)
- Others: r-x (5)
-
Set read and write permissions for the user, and read permissions for the group and others:
chmod 644 filename
This sets:
- User: rw- (6)
- Group: r-- (4)
- Others: r-- (4)
-
Remove all permissions for others and set full permissions for the owner:
chmod 700 filename
This sets:
- User: rwx (7)
- Group: --- (0)
- Others: --- (0)
In addition to the basic permissions, there are three special permissions:
- Setuid (s): Sets the user ID on execution. The file will run with the permissions of the file's owner.
- Setgid (s): Sets the group ID on execution. The file will run with the permissions of the file's group.
- Sticky bit (t): Used on directories, ensuring that only the file owner can delete or rename the file in that directory.
The setuid permission is used primarily on executable files. When a file with the setuid bit is executed, the process will run with the permissions of the file's owner (rather than the user executing it).
- Example:
chmod u+s filename
The setgid permission is used primarily on executable files and directories. When a file with the setgid bit is executed, the process runs with the permissions of the file's group. For directories, it ensures that new files created inside the directory inherit the directory's group.
- Example:
chmod g+s filename
When applied to a directory, the sticky bit ensures that only the owner of a file can delete or rename it within that directory (even if other users have write permissions on the directory).
- Example:
chmod +t directory
The -R
option allows you to change the permissions of a directory and all of its contents (subdirectories and files).
- Change permissions for all files and subdirectories in a directory:
chmod -R 755 directory/
This will set the permissions to rwx
for the user, r-x
for the group, and r-x
for others, recursively for all files and subdirectories inside directory/
.
To view the current permissions of files and directories, use the ls -l
command. It lists the permissions in the format rwxrwxrwx
for each file or directory.
- Example:
ls -l filename
This output shows the permissions, owner, group, size, and last modification date of the file:
-rwxr-xr-x 1 user group 1234 Jan 1 12:00 filename
Command | Description |
---|---|
chmod u+x filename |
Add execute permission for the user. |
chmod g-w filename |
Remove write permission for the group. |
chmod o+r filename |
Add read permission for others. |
chmod 755 filename |
Set rwx for user, r-x for group and others. |
chmod 644 filename |
Set rw- for user, r-- for group and others. |
chmod -R 755 directory/ |
Recursively set permissions for all files and directories inside directory/ . |
chmod u+s filename |
Set the setuid bit for the file. |
chmod g+s filename |
Set the setgid bit for the file. |
chmod +t directory/ |
Set the sticky bit for the directory. |
The chmod
command is a powerful and flexible tool for managing file permissions. By understanding how to use symbolic and numeric modes, as well as the special permissions, you can tailor file access control to suit your needs.
The chmod
command in Linux and Unix-like systems is used to change the file or directory permissions. It controls who can read, write, or execute a file.
Before diving into chmod
, it’s important to understand how file permissions work:
- Read (
r
): Permission to read the content of the file. - Write (
w
): Permission to modify the content of the file. - Execute (
x
): Permission to run the file as a program or script.
Permissions are assigned to three categories of users:
- Owner (User): The user who owns the file.
- Group: The group of users associated with the file.
- Others: All other users who are not the owner or part of the group.
Each permission is represented by a symbol:
r
(Read) = 4w
(Write) = 2x
(Execute) = 1
These values are combined in the permission string, so a file can have a combination of read, write, and execute permissions for the owner, group, and others.
For example:
rwxr-xr--
means:- Owner: read, write, execute (7)
- Group: read, execute (5)
- Others: read (4)
The basic syntax of the chmod
command is:
chmod [options] mode file
mode
: Defines the permissions you want to set (either numeric or symbolic).file
: Specifies the file or directory for which you want to change the permissions.
Permissions are assigned using numbers where each permission is represented by a digit:
- 4 for read (
r
) - 2 for write (
w
) - 1 for execute (
x
)
To assign permissions, you add up these values:
- 7 (read, write, execute) = 4 + 2 + 1
- 6 (read, write) = 4 + 2
- 5 (read, execute) = 4 + 1
- 4 (read only)
- 3 (write, execute) = 2 + 1
- 2 (write only)
- 1 (execute only)
- 0 (no permission)
-
chmod 755 filename
:- Owner: read, write, execute (7)
- Group: read, execute (5)
- Others: read, execute (5)
-
chmod 644 filename
:- Owner: read, write (6)
- Group: read (4)
- Others: read (4)
The symbolic mode allows you to add, remove, or set permissions with symbols. Here's the syntax:
r
: Readw
: Writex
: Execute+
: Adds a permission-
: Removes a permission=
: Sets the permission explicitly (removes all other permissions)
You can also specify the user categories:
u
: User (owner)g
: Groupo
: Othersa
: All (user, group, others)
chmod u+x filename
: Add execute permission to the owner of the file.chmod g-w filename
: Remove write permission from the group.chmod o=r filename
: Set the file permissions of others to read-only.chmod a+x filename
: Add execute permission for everyone (user, group, and others).chmod u+x,g+x filename
: Add execute permission for both the owner and the group.chmod u=rwx,g=rx,o=r filename
: Explicitly set the permissions (owner has read/write/execute, group has read/execute, others have read).
The -R
option allows you to apply permissions recursively to a directory and its contents (files and subdirectories).
chmod -R 755 directory/
: This will set the permissions755
for all files and subdirectories insidedirectory/
.chmod -R u+w directory/
: This adds write permission to the owner for all files and directories insidedirectory/
.
While chmod
changes the permissions of a file, chown
changes the ownership of a file or directory (who owns it and which group it belongs to).
chown user:group filename
This command changes the ownership of the file filename
to user
and assigns it to the group
.
chmod u+rwx filename
This grants full read, write, and execute permissions to the file owner.
chmod g-w filename
This removes the write permission for the group.
To make a script file executable:
chmod +x script.sh
chmod o=r filename
This command makes the file read-only for all others.
chmod 777 filename
This gives full read, write, and execute permissions to the owner, group, and others. Be cautious with this, as it grants access to everyone.
To allow the owner to read, write, and execute, while others can only read and execute:
chmod 755 directory/
To check the current permissions of files and directories, use the ls -l
command. This shows the file permissions, owner, group, and other details.
ls -l filename
Output might look like this:
-rwxr-xr-- 1 user group 1234 Dec 29 10:00 filename
rwxr-xr--
: The file's permissions.1
: Number of links.user
: Owner of the file.group
: Group associated with the file.1234
: File size in bytes.Dec 29 10:00
: Last modification time.filename
: File name.
Option | Description |
---|---|
u |
User (Owner) |
g |
Group |
o |
Others |
a |
All (User, Group, Others) |
+ |
Add permission |
- |
Remove permission |
= |
Set permission exactly |
r |
Read permission |
w |
Write permission |
x |
Execute permission |
-R |
Apply recursively to directories and their contents |
The chmod
command is a powerful tool for managing file and directory permissions. By understanding numeric and symbolic modes, as well as the recursive option (-R
), you can control who has access to files and directories on your system. Always be cautious when setting permissions to avoid giving excessive access to sensitive files.
The chmod
command in Unix-like systems (such as Linux and macOS) is used to change the permissions of files and directories. It controls who can read, write, or execute a file, and also manages the access control of files in the system.
Here’s a comprehensive guide to chmod
and its usage:
Files and directories have three types of permissions that can be granted:
- Read (
r
): Permission to view the content of the file or list the contents of the directory. - Write (
w
): Permission to modify the content of the file or add/remove files in a directory. - Execute (
x
): Permission to run the file as a program/script or enter the directory.
Permissions can be applied to three types of users:
- User (
u
): The file owner. - Group (
g
): Users who are members of the file's group. - Others (
o
): All other users on the system. - All (
a
): User, group, and others together.
The general syntax of the chmod
command is:
chmod [options] mode file
[options]
: Optional flags to modify the behavior ofchmod
.mode
: Specifies the permission changes, which can be in symbolic or numeric mode.file
: The file or directory whose permissions you want to change.
In symbolic mode, you use letters to represent permissions and operators to modify them.
- Add permissions: Use
+
- Remove permissions: Use
-
- Set exact permissions: Use
=
chmod [who][operator][permission] file
who
: Specifies the users for whom you want to change the permissions (u
,g
,o
, ora
).operator
: Determines how the permissions are modified (+
,-
, or=
).permission
: Defines the permissions to modify (r
,w
,x
).
-
Add execute permission for the user:
chmod u+x filename
This gives the file's owner (
u
) execute permission (x
). -
Remove write permission for the group:
chmod g-w filename
This removes write permission (
w
) for the group (g
). -
Give read and execute permissions to everyone:
chmod a+rx filename
This adds read (
r
) and execute (x
) permissions for all users (a
). -
Set exact permissions for the file (read and write for user, read for group and others):
chmod u=rw,g=r,o=r filename
In numeric mode, permissions are represented as a 3-digit octal number. Each digit represents a set of permissions for the user, group, and others. Each permission is assigned a value:
- Read (r): 4
- Write (w): 2
- Execute (x): 1
- No permission: 0
To set permissions, you sum the values for each user type. For example:
- User (u): 4 (read), 2 (write), 1 (execute)
- Group (g): 4 (read), 2 (write), 1 (execute)
- Others (o): 4 (read), 2 (write), 1 (execute)
-
Set read and write for user, and read-only for group and others:
chmod 644 filename
Here’s how it breaks down:
- 6 (4 + 2): Read and write for the user.
- 4: Read for the group.
- 4: Read for others.
-
Set full permissions (read, write, and execute) for user, group, and others:
chmod 777 filename
- 7 (4 + 2 + 1): Full permissions for the user, group, and others.
-
Set read, write, and execute for the user, and no permissions for the group and others:
chmod 700 filename
- 7: Read, write, and execute for the user.
- 0: No permissions for the group.
- 0: No permissions for others.
In addition to basic file permissions, there are special permissions that can be applied to files and directories:
- Setuid (
s
): When set on an executable file, it allows the file to be run with the privileges of the file owner (often root).- Numeric value: 4 (in the first digit).
- Example:
chmod 4755 filename
- Setgid (
s
): When set on a directory, files created within the directory will inherit the group ownership of the directory.- Numeric value: 2 (in the second digit).
- Example:
chmod 2755 directoryname
- Sticky bit (
t
): When set on a directory, it ensures that only the file's owner can delete or rename files within that directory.- Example:
chmod +t directoryname
- Example:
-
Set setuid:
chmod 4755 filename
This will set the setuid bit, which causes the file to run with the owner's permissions.
-
Set setgid:
chmod 2755 directoryname
This will set the setgid bit, so files created in the directory will inherit the group.
-
Set sticky bit:
chmod +t directoryname
This is often used on directories like
/tmp
to prevent users from deleting each other's files.
-
-R
(Recursive): Apply changes recursively to all files and subdirectories within a directory.chmod -R 755 directoryname
This sets
755
permissions (read, write, execute for user; read, execute for group and others) on the directory and all its contents. -
-v
(Verbose): Show the files whose permissions are being changed.chmod -v 644 filename
-
-c
(Changes): Only display files whose permissions have been changed.chmod -c 755 filename
-
Make a file executable:
chmod +x script.sh
This adds execute permission for the user, group, and others.
-
Give read and write permissions to the owner and read-only to others:
chmod 644 file.txt
-
Make a directory accessible for everyone to read, write, and execute:
chmod 777 directoryname
-
Remove write permission for others:
chmod o-w file.txt
-
Make a script executable only by the owner:
chmod 700 script.sh
- Symbolic Mode:
chmod u+x filename
— Add execute permission to the user.chmod g-w filename
— Remove write permission from the group.chmod a+r filename
— Add read permission for everyone.chmod u=rwx,g=rx,o=r filename
— Set specific permissions for user, group, and others.
- Numeric Mode:
chmod 755 filename
— Full permissions for the user, read and execute for group and others.chmod 644 filename
— Read and write for user, read-only for group and others.chmod 777 filename
— Full permissions for everyone.
- Special Permissions:
chmod +t directoryname
— Set the sticky bit on a directory.chmod 4755 filename
— Set the setuid bit.chmod 2755 directoryname
— Set the setgid bit.
The chmod
command is a powerful tool for controlling access to files and directories in a Unix-like operating system. By understanding both symbolic and numeric modes, you can effectively manage file permissions to ensure appropriate access control and security in your system.
The chmod
command is crucial for managing file and directory permissions in Unix-like systems such as Linux and macOS. It allows users to specify who can read, write, or execute files and directories, offering fine-grained control over file security and accessibility.
Let’s dive deeper into the nuances of chmod
:
File permissions in Unix-like systems determine who can access and modify files and directories. Permissions are applied to three categories of users:
- User (
u
): The owner of the file or directory. - Group (
g
): Other users who are part of the file's group. - Others (
o
): All other users on the system who aren't the owner or part of the group. - All (
a
): Refers to all of the above categories.
Each file or directory has three types of permissions:
- Read (
r
): Allows viewing the content of a file or listing files in a directory. - Write (
w
): Allows modifying the content of a file or adding/removing files in a directory. - Execute (
x
): Allows running a file as a program/script or accessing a directory (i.e., entering the directory).
In symbolic mode, you specify who (u
, g
, o
, or a
), what operation (+
, -
, =
), and which permission (r
, w
, x
) you want to set. The syntax is:
chmod [who][operator][permission] file
who
: Specifies the category of users (u
,g
,o
,a
).operator
: Specifies the action to take (+
to add,-
to remove,=
to set exactly).permission
: The permission being modified (r
for read,w
for write,x
for execute).
-
Add read permission for everyone:
chmod a+r file.txt
This adds read permission for all users (owner, group, and others).
-
Remove write permission for others:
chmod o-w file.txt
This removes write permission for others.
-
Give read and execute permissions to the group:
chmod g+rx file.txt
This adds read and execute permissions for the group.
-
Set exact permissions for the user, group, and others:
chmod u=rw,g=r,o=r file.txt
This sets the file's permissions to:
- User: read and write
- Group: read only
- Others: read only
-
Setuid: The setuid (Set User ID) special permission allows a user to execute a file with the file's owner's privileges, usually used for system administration programs. The
setuid
permission is represented by4
(in the first digit when using numeric mode) ors
in symbolic mode.- Example (setuid in symbolic mode):
chmod u+s program.sh
- Example (setuid in symbolic mode):
-
Setgid: The setgid (Set Group ID) special permission ensures that new files created within a directory inherit the group of the directory, rather than the user's default group. In symbolic mode, it's represented by
g+s
, or numerically by2
in the second digit.- Example (setgid):
chmod g+s directoryname
- Example (setgid):
-
Sticky Bit: The sticky bit is a special permission used on directories. When set on a directory, it allows only the file owner to delete or rename files within that directory, even if other users have write permissions. This is commonly used on directories like
/tmp
.- Example (sticky bit):
chmod +t directoryname
- Example (sticky bit):
In numeric (octal) mode, file permissions are represented by a 3-digit octal number. Each digit corresponds to a user category: user, group, and others. The numeric value for each permission type is:
- Read = 4
- Write = 2
- Execute = 1
To set permissions, you sum the values for the desired permissions. Each of the three digits corresponds to the permissions for:
- User (Owner)
- Group
- Others
Permission | Value |
---|---|
Read (r) | 4 |
Write (w) | 2 |
Execute (x) | 1 |
For example:
- 755 (User: read, write, execute; Group: read, execute; Others: read, execute)
chmod 755 file.txt
- 644 (User: read, write; Group: read; Others: read)
chmod 644 file.txt
Special permissions (setuid, setgid, sticky bit) are represented in the first digit of the octal number:
- Setuid: 4
- Setgid: 2
- Sticky bit: 1
For example:
-
4755 (Setuid + 755 permissions):
chmod 4755 program.sh
-
2755 (Setgid + 755 permissions):
chmod 2755 directoryname
-
1777 (Sticky bit + full permissions for everyone):
chmod 1777 /tmp
To make a script file executable for the user:
chmod u+x script.sh
This gives the user execute permission for the script.
To prevent other users from reading or writing a sensitive file, you can set the permissions like this:
chmod 700 private.txt
This sets read, write, and execute permission for the user, and no permission for group or others.
If you want to give all users read and write access to a directory, but only the owner can delete files, use:
chmod 1777 shared_directory
This sets the sticky bit on the directory, ensuring that only the owner can delete their own files.
To allow the group to write to a file, but not execute it, you can use:
chmod g+w file.txt
This adds write permission to the group, while keeping the other permissions intact.
-
-R
(Recursive): Apply the permission changes recursively to all files and subdirectories in a directory.chmod -R 755 directoryname
-
-v
(Verbose): Output the files whose permissions are being modified.chmod -v 644 file.txt
-
-c
(Changes): Only display files whose permissions were changed.chmod -c 755 file.txt
-
--reference
: Use the permissions of another file as a reference to set the permissions of the target file.chmod --reference=file1.txt file2.txt
chmod 700 file.txt
chmod +x script.sh
chmod o-w file.txt
chmod -R 755 /path/to/directory
chmod 777 /path/to/directory
The chmod
command is a powerful tool for modifying file permissions, helping you control access to files and directories. It can be used with symbolic or numeric modes, and includes special permissions like setuid, setgid, and the sticky bit to manage complex access control scenarios. By mastering chmod
, you can ensure that your files and directories are securely accessible only to the appropriate users.