Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The chmod
command in Linux (short for "change mode") is a utility used to change the permissions of files and directories. In this tutorial, we will discuss how to use the chmod
command effectively, along with various options and examples.
Understanding Linux permissions:
Linux permissions are based on three types of access: read (r
), write (w
), and execute (x
). These permissions are set for three different categories of users: the owner of the file, the group that the file belongs to, and others (everyone else). Permissions are usually represented using a combination of these characters (e.g., rwxr-xr-x
).
Basic usage of the chmod
command:
The chmod
command can be used with either symbolic or numeric (octal) representations of permissions.
Symbolic representation:
To change permissions using symbolic representation, use the chmod
command followed by the permissions changes and the path of the file or directory:
chmod [ugoa][+-=][rwx] file.txt
The first part [ugoa]
represents the user categories: user (owner) u
, group g
, others o
, and all a
. The second part [+-=]
represents the operations: add +
, remove -
, and set =
. The third part [rwx]
represents the permissions: read r
, write w
, and execute x
.
Numeric (octal) representation:
To change permissions using numeric representation, use the chmod
command followed by a three-digit octal number and the path of the file or directory:
chmod 755 file.txt
Each digit in the octal number represents the permissions for a specific category of users: the first digit is for the owner, the second is for the group, and the third is for others. The digit is the sum of the permissions' numeric values: read 4
, write 2
, and execute 1
.
Examples of using the chmod
command:
Grant read, write, and execute permissions to the owner, and read and execute permissions to the group and others:
chmod u=rwx,g=rx,o=rx file.txt
or
chmod 755 file.txt
Remove write permission from the group and others:
chmod go-w file.txt
Add execute permission for all users:
chmod a+x file.txt
Changing permissions recursively:
To change the permissions of a directory and its contents recursively, use the -R
or --recursive
option:
chmod -R 755 /path/to/directory
This command will change the permissions of the specified directory and all its files and subdirectories to 755
.
By following this tutorial, you should now have a good understanding of how to use the chmod
command in Linux to change the permissions of files and directories. The chmod
command is an essential tool for managing file access and ensuring that the right users have the appropriate permissions for files and directories.
How to use chown command in Linux:
chown
command is used to change the owner and/or group of files and directories in Linux.chown [options] owner[:group] file
Changing file ownership in Linux with chown:
chown new_owner filename
Assigning a new owner to files and directories:
chown user1 file1 file2
Changing both owner and group with chown:
chown user:group filename
Recursive ownership change with chown in Linux:
chown -R user:group directory
Viewing current file ownership with chown:
ls -l filename
Preserving file permissions during ownership change:
chown --preserve=file_mode new_owner:new_group filename
Using chown to transfer ownership between users:
chown --from=old_owner --to=new_owner file
Combining chown and chgrp commands in Linux:
chown
and chgrp
.chown user:group file chgrp new_group file