This "chmod" command is be used quite a lot. and I cry for understanding and being familiar with this command. While I am looking its helpful, it cannot satisfy me. And then I make a decision to collect it belows:
reference:
1, "man chmod" under liunx
2, https://help.ubuntu.com/community/FilePermissions
In linux and Unix, everything is a file. The super user "root" has the ability to access any file on the sytem, while it is unsafe for most users by using "root". Each file has access restriction with permissions, user restrictions with owner/group association. and permissions are referred to as bits.
This "chmod" command is used to change file mode bits, so this command is able to change the access to any file.
There are three types of access restrictions:
Permission
Action
chmod option
read
(view)
r or 4
write
(edit)
w or 2
execute
(execute)
x or 1
There are also three types of user restrictions:
User
ls output
owner
-rwx------
group
----rwx---
other
-------rwx
here givt the functions use "chmod" command:
(1) permissions in action
----how to get the file's permissions and "/etc/hosts" as a example
$ ls -l /etc/hosts
and u will get "-rw-r--r-- 1 root root 259 2011-01-01 08:37 /etc/hosts", this results means
owner = Read & Write (rw-) group = Read (r--) other = Read (r--)
(2) changing permissions
Here give the usage about "chmod" command: chmod {options} filename
Options
Definition
u
owner
g
group
o
other
x
execute
w
write
r
read
+
add permission
-
remove permission
=
set permission
and then give some examples below:
First create some empty files:
$ touch file1 file2 file3 file4 $ ls -l and get the results: -rw-r--r-- 1 user user 0 Nov 19 20:13 file1 -rw-r--r-- 1 user user 0 Nov 19 20:13 file2 -rw-r--r-- 1 user user 0 Nov 19 20:13 file3 -rw-r--r-- 1 user user 0 Nov 19 20:13 file4Add owner execute bit:
$ chmod u+x file1 $ ls -l file1 and get the results: -rwxr--r-- 1 user user 0 Nov 19 20:13 file1Add other write & execute bit:
$ chmod o+wx file2 $ ls -l file2 and get the results: -rw-r--rwx 1 user user 0 Nov 19 20:13 file2Remove group read bit:
$ chmod g-r file3 $ ls -l file3 and get the results -rw----r-- 1 user user 0 Nov 19 20:13 file3Add read, write and execute to everyone:
$ chmod ugo+rwx file4 $ ls -l file4 and get the results: -rwxrwxrwx 1 user user 0 Nov 19 20:13 file4Options
Definition
#--
owner
-#-
group
--#
other
1
execute
2
write
4
read
Owner, Group and Other is represented by three numbers. To get the value for the options determine the type of access needed for the file then add. For example if you want a file that has -rw-rw-rwx permissions you will use the following:Owner
Group
Other
read & write
read & write
read, write & execute
4+2=6
4+2=6
4+2+1=7
$ chmod 667 filename Another example if you want a file that has --w-r-x--x permissions you will use the following:Owner
Group
Other
write
read & execute
execute
2
4+1=5
1
(3) recursive permission changes ----to change the permissions of multiple files and directions with one command Recursive chmod with -R and sudoTo change all the permissions of each file and folder under a specified directory at once, use sudo chmod with -R
$ sudo chmod 777 -R /path/to/someDirectory $ ls -l and gets the results: -rwxrwxrwx 1 user user 0 Nov 19 20:13 file1 drwxrwxrwx 2 user user 4096 Nov 19 20:13 folder -rwxrwxrwx 1 user user 0 Nov 19 20:13 file2