Set permissions for directories and files in linux

Set permissions for directories and files under linux, including recursive settings for subdirectories

The chmod command can change the permissions of all subdirectories. There are two ways to 
change the permissions of a file: chmod mode file|dir 
Change the permissions of all subdirectories: chmod mode dir -R Note that the -R parameter is added after the 
parameter is the permission mode mode = 777 or 752, 666,,, 
The three numbers of mode represent the permissions of owner, group, and others respectively. 
1 = x execute 2 = w write 4 = r read, such as owner has all permissions, 1+2+4=7, 
and group has read and execute permissions 1+4 = 5

Change the owner of the directory /his and all files and subdirectories under it to wang, and the group to users. 
    $ chown –R wang.users /his

I uploaded a Wordpress, and after decompressing it online, I found that the file does not have permission to write, so I used chmod 777 
filename 
but only changed the file. The file in the file still does not have permission to write. I checked it online and 
modified the linux file permission command: chmod 
Usage: chmod XXX filename

××× (owner\group users\other users)

×=4 permission to read 
×=2 permission to write 
×=1 permission to execute

Commonly used commands to modify permissions:

sudo chmod 600 ××× (only owner has read and write permissions) 
sudo chmod 644 ××× (owner has read and write permissions, group users only have read permissions) 
sudo chmod 700 ××× (owner only have read and write and execute permissions)

sudo chmod 666 ××× (everyone has read and write permissions)

sudo chmod 777 ××× (everyone has read and write and execute permissions)

For example: 
    - rw- r–r–

    Ordinary file files The main group user and other users 
    have the access rights of the file sobsrc.tgz, indicating that sobsrc.tgz is a common file; 
the owner of sobsrc.tgz has read and write permissions; the user in the same group as the owner of sobsrc.tgz has only read permissions ; other users also only have read access. 
    After determining the access rights of a file, users can use the chmod command provided by the Linux system to 
reset different access rights. You can also use the chown command to change the owner of a file or directory. use chgrp

command to change the user group of a file or directory. 
    These commands are described below. 
    chmod command 

    The chmod command is very important to change the access permissions of a file or directory. It is used by users to control files or

access rights to the records. 
    There are two uses for this command. One is a literal set containing letters and operator expressions; the other is a 
numerical set containing numbers. 
    1. Text setting method 
    chmod [who] [+ | –| =] [mode] filename?

    命令中各选项的含义为: 
    操作对象who可是下述字母中的任一个或者它们的组合: 
    u 表示”用户(user)”,即文件或目录的所有者。 
    g 表示”同组(group)用户”,即与文件属主有相同组ID的所有用户。 
    o 表示”其他(others)用户”。 
    a 表示”所有(all)用户”。它是系统默认值。

    操作符号可以是: 
    + 添加某个权限。 
    - 取消某个权限。 
    = 赋予给定权限并取消其他所有权限(如果有的话)。 
    设置mode所表示的权限可用下述字母的任意组合: 
    r 可读。 
    w 可写。 
    x 可执行。 
    X 只有目标文件对某些用户是可执行的或该目标文件是目录时才追加x 属性。 
    s 在文件执行时把进程的属主或组ID置为该文件的文件属主。方式”u+s”设置文件 
的用户ID位,”g+s”设置组ID位。 
    t 保存程序的文本到交换设备上。 
    u 与文件属主拥有一样的权限。 
    g 与和文件属主同组的用户拥有一样的权限。 
    o 与其他用户拥有一样的权限。

    文件名:以空格分开的要改变权限的文件列表,支持通配符。 
    在一个命令行中可给出多个权限方式,其间用逗号隔开。例如:chmod g+r,o+r 
example 
    使同组和其他用户对文件example 有读权限。 
    2. 数字设定法 
    我们必须首先了解用数字表示的属性的含义:0 表示没有权限,1 表示可执行权限,2 
表示可写权限,4 表示可读权限,然后将其相加。所以数字属性的格式应为3个从0 到7 
的八进制数,其顺序是(u)(g)(o)。 
    例如,如果想让某个文件的属主有”读/写”二种权限,需要把4 (可读)+2 (可写) 
=6 (读/写)。

    数字设定法的一般形式为: 
    chmod  [mode]文件名?

    例子: 
     (1)文字设定法: 
    例1:$ chmod a+x sort 
    即设定文件sort 的属性为: 
    文件属主(u)增加执行权限 

    与文件属主同组用户(g)增加执行权限 
    其他用户(o)增加执行权限 
    例2:$ chmod ug+w,o-x text 
    即设定文件text的属性为: 
    文件属主(u)增加写权限 
    与文件属主同组用户(g)增加写权限 
    其他用户(o)删除执行权限 
    例3:$ chmod u+s a.out 
    假设执行chmod后a.out 的权限为(可以用ls –l a.out命令来看): 
    -rws–x–x 1 inin users 7192 Nov 4 14:22 a.out 
    并且这个执行文件要用到一个文本文件shiyan1.c,其文件存取权限为”-rw——-”,

即该文件只有其属主具有读写权限。 
    当其他用户执行a.out这个程序时,他的身份因这个程序暂时变成inin(由于chmod 
命令中使用了s选项),所以他就能够读取shiyan1.c这个文件(虽然这个文件被设定为 
其他人不具备任何权限),这就是s 的功能。 
    因此,在整个系统中特别是root本身,最好不要过多的设置这种类型的文件(除非必 
要)这样可以保障系统的安全,避免因为某些程序的bug而使系统遭到入侵。 
    例4:$ chmod a-x mm.txt 
    $ chmod -x mm.txt 
    $ chmod ugo-x mm.txt 
    以上这三个命令都是将文件mm.txt 的执行权限删除,它设定的对象为所有使用者。 
     (2)数字设定法: 
    例1:$ chmod 644 mm.txt 
    $ ls -l 
    即设定文件mm.txt的属性为: 
    -rw-r–r–1 inin users 1155 Nov 5 11:22 mm.txt 
    文件属主(u)inin 拥有读、写权限 
    与文件属主同组人用户(g)拥有读权限 
    其他人(o)拥有读权限 
    例2:$ chmod 750 wch.txt 
    $ ls -l 
    -rwxr-x— 1 inin users 44137 Nov 12 9:22 wchtxt 
    即设定wchtxt这个文件的属性为: 
    文件主本人(u)inin 可读/可写/可执行权 
    与文件主同组人(g)可读/可执行权 
    其他人(o)没有任何权限 
    chgrp命令

    功能:改变文件或目录所属的组。 
    语法:chgrp  [选项]group filename? 
    该命令改变指定指定文件所属的用户组。其中 group 可以是用户组 ID,也可以是 
/etc/group 文件中用户组的组名。文件名是以空格分开的要改变属组的文件列表,支持

通配符。如果用户不是该文件的属主或超级用户,则不能改变该文件的组。 
    该命令的各选项含义为: 
    - R 递归式地改变指定目录及其下的所有子目录和文件的属组。 

    例1:$ chgrp –R book /opt/local /book 
    改变/opt/local /book/及其子目录下的所有文件的属组为book。 
    chown 命令 
    功能:更改某个文件或目录的属主和属组。这个命令也很常用。例如root用户把自己 
的一个文件拷贝给用户xu,为了让用户xu能够存取这个文件,root用户应该把这个文件 
的属主设为xu,否则,用户xu无法存取这个文件。 
    语法:chown  [选项]用户或组文件 
    说明:chown将指定文件的拥有者改为指定的用户或组。用户可以是用户名或用户ID。 
组可以是组名或组ID。文件是以空格分开的要改变权限的文件列表,支持通配符。

    该命令的各选项含义如下: 
    - R 递归式地改变指定目录及其下的所有子目录和文件的拥有者。 
    - v 显示chown命令所做的工作。 
    例1:把文件shiyan.c 的所有者改为wang。 
    $ chown wang shiyan.c 
    例2:把目录/his及其下的所有文件和子目录的属主改成wang,属组改成users。 
    $ chown –R wang.users /his

    Linux下Web 目录和文件安全权限设置 
在Linux下,web 目录和文件权限必须从整体上考虑系统的安全。一般情况下,对目录, 
需要设置r(读取)和x(执行)权限,有的目录同时还需要w(写入权限);对文件,需要r(读 
取),有的文件需要w(写入)权限或x(执行)权限。 
在 Linux 系统中,使用命令umask 设置创建文件或目录的默认rwx 权限,系统默认的 
umask 设置是022,这个权限的计算相当于文件、目录权限的掩码,例如此时创建的目录 
权限755 (rwxr-xr-x),那么其umask权限相当于相对777 的掩码022;而此时创建 
的文件权限为644 (rw-r–r–),其umask权限相当于相对666的掩码022。 
当然,这样的权限设置很不安全,同一台server上的不同用户(可能相同也可能不同用户 
组)/虚拟主机用户能够互相窥探到对方的源码,umask值必须修改的比较严格,以使得除 
root权限之外,不能随意互相窥探其他人的源码、数据库资料等。 
设置方法是:去掉同用户组和其他用户组的 r(读取)权限,具体做法是设置目录权限为 
500(读取+执行)同时文件权限为400(读取),此时umask应设置为277,设置目录权限 
为700(读取+写入+执行)同时文件权限为500(读取+执行),此时umask应设置为177。 
例如对于后者,我们可以使用命令 umask 177 设置当前对话下的默认目录、文件创建权 
限,如果要永久设置,就要修改/root/.bash_profile 以及所有用户home 的录下 
的.bash_profile文件,将其中的umask 022改为umask 177。

从以上可以看出,如果要设置较为安全的目录、文件权限,几个基本原则就是:

1、尽可能减少web路径下可写入目录的数量。 
2、文件的写入和执行权限只能选择其一,避免同时出现写入和执行权限。 

 

 

转:http://blog.csdn.net/lenix/article/details/7548325

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326504724&siteId=291194637