Thứ Ba, 2 tháng 7, 2013

Set Permission - Linux and Unix chmod command

About chmod
Changes the permission of a file.
Syntax
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
-c, --changeslike verbose but report only when a change is made
--no-preserve-rootdo not treat `/' specially (the default)
--preserve-rootfail to operate recursively on `/'
-f, --silent, --quietsuppress most error messages
-v, verboseoutput a diagnostic for every file processed
--reference=RFILEuse RFILE's mode instead of MODE values
-R, --recursivechange files and directories recursively
--helpdisplay this help and exit
--versionoutput version information and exit
Permissions
u - User who owns the file.
g - Group that owns the file.
o - Other.
a - All.
r - Read the file.
w - Write or edit the file.
x - Execute or run the file as a program.
Numeric Permissions: 
CHMOD can also to attributed by using Numeric Permissions:
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody
Examples
The above numeric permissions can be added to set a certain permission, for example, a common HTML file on a Unix or Linux server accessible over the Internet would be given the below permissions.
chmod 644 file.htm
This gives the file read/write by the owner and only read by everyone else (-rw-r--r--).
Files such as scripts that need to be executed need more permissions. Below is another example of a common permission given to scripts.
chmod 755 file.cgi
This would be the following 400+040+004+200+100+010+001 = 755 where you are giving all the rights except the capability for anyone to write to the file.cgi file(-rwxr-xr-x).
chmod 666 file.txt
Finally, another common CHMOD permission is 666, as shown below, which is read and write by everyone.
Tip: The above commands are all done through the command line. However, if you upload a file using FTP these permissions can also be adjusted through many FTP clients by right-clicking the file and choosing permissions.
Additional information
Below is an example of how a file may be listed when typing ( ls -l ) at the prompt as well as information on how to interpret it.
-rw-rw-r-- 1   hope 123   Feb 03 15:36   file.txt
-rwrw-r--1hope123Feb 03 15:36file.txt
Fileownergroupeveryone elselinksownersizemod datefile name
AND MORE

What is chmod?

chmod stands for change mode, which changes the file or directory mode bits. To put it simply, use chmod command to change the file or directory permissions.
Following is a sample of ls -l command output. In this, the 9 characters from 2nd to 10th position represents the permissions for the 3 types of users.
-rw-r--r--  1 john john  272 Mar 17 08:22 test.txt
In the above example:
  • User (john) has read and write permission
  • Group has read permission
  • Others have read permission
Three file permissions:
  • read: permitted to read the contents of file.
  • write: permitted to write to the file.
  • execute: permitted to execute the file as a program/script.
Three directory permissions:
  • read: permitted to read the contents of directory ( view files and sub-directories in that directory ).
  • write: permitted to write in to the directory. ( create files and sub-directories in that directory )
  • execute: permitted to enter into that directory.
Numeric values for the read, write and execute permissions:
  • read 4
  • write 2
  • execute 1
To have combination of permissions, add required numbers. For example, for read and write permission, it is 4+2 = 6.

3 chmod Examples

Give read, write and execute to everybody (user, group, and others)

read, write and execute = 4 + 2 + 1 = 7.
$ chmod 777 file.txt
(or)
$ chmod ugo+rwx file.txt

Give execute privilege to user. Leave other privileges untouched

execute = 1. If you want to just add execute privilege to users and leave all other privileges as it is, do the following.
$ chmod u+x file.txt

Give read, write and execute privileges to group (including all the files in the sub-directories)

Use -R, as shown below to provide the recursive privileges for the directory and sub-directories (including the files in it).
$ chmod -R g+rwx /u01

Syntax and Options

chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…
Short OptionLong OptionOption Description
-c–changeslike verbose but report only when a change is made

–no-preserve-rootdo not treat `/’ specially (the default)

–preserve-rootfail to operate recursively on `/’
-f–silent, –quietsuppress most error messages
-v–verboseoutput a diagnostic for every file processed

–reference=RFILEuse RFILE’s mode instead of MODE values
-R–recursivechange files and directories recursively

–helpdisplay this help and exit

–versiooutput version information and exit

Related Commands


Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...