chmod是一条在Unix系统中用于控制用户对文件的权限的命令(changemode单词前缀的组合)和函数。只有文件所有者和超级用户可以修改文件或目录的权限。可以使用绝对模式,符号模式指定文件的权限。
一.八进制语法
chmod命令可以使用八进制数来指定权限。文件或目录的权限位是由9个权限位来控制,每三位为一组,它们分别是文件所有者(user)的读、写、执行,用户组(group)的读、写、执行以及(other)其它用户的读、写、执行。历史上,文件权限被放在一个比特掩码中,掩码中指定的比特位设为1,用来说明一个类具有相应的优先级。
chmod的八进制语法的数字说明;
r 4
w 2
x 1
- 0
所有者的权限用数字表达:属主的那三个权限位的数字加起来的总和。如rwx ,也就是4+2+1 ,应该是7。
与文件所有者同属一个用户组的其他用户的权限用数字表达:属组的那个权限位数字的相加的总和。如rw- ,也就是4+2+0 ,应该是6。
其它用户组的权限数字表达:其它用户权限位的数字相加的总和。如r-x ,也就是4+0+1 ,应该是5。
二.符号模式
使用符号模式可以设置多个项目:who(用户类型),operator(操作符)和permission(权限),每个项目的设置可以用逗号隔开。 命令chmod将修改who指定的用户类型对文件的访问权限,用户类型由一个或者多个字母在who的位置来说明,如who的符号模式表所示:
operator的符号模式表:
permission的符号模式表:
符号模式实例
对目录的所有者u和关联组g增加读r和写w权限:
$ chmod ug+rw mydir$ ls -ld mydirdrw-rw---- 2 unixguy uguys 96 Dec 8 12:53 mydir
三.Linux中的文件特殊权限
1、setuid、setgid
先看个实例,查看你的/usr/bin/passwd 与/etc/passwd文件的权限
[root@MyLinux ~]# ls -l /usr/bin/passwd /etc/passwd-rw-r--r-- 1 root root 1549 08-19 13:54 /etc/passwd-rwsr-xr-x 1 root root 22984 -01-07 /usr/bin/passwd
众所周知,/etc/passwd文件存放的各个用户的账号与密码信息,/usr/bin/passwd是执行修改和查看此文件的程序,但从权限上看,/etc/passwd仅有root权限的写(w)权,可实际上每个用户都可以通过/usr/bin/passwd命令去修改这个文件,于是这里就涉及了linux里的特殊权限setuid,正如-rwsr-xr-x中的s
setuid就是:让普通用户拥有可以执行“只有root权限才能执行”的特殊权限,setgid同理指”组“
作为普通用户是没有权限修改/etc/passwd文件的,但给/usr/bin/passwd以setuid权限后,普通用户就可以通过执行passwd命令,临时的拥有root权限,去修改/etc/passwd文件了
2、stick bit (粘贴位)
再看个实例,查看你的/tmp目录的权限
[root@MyLinux ~]# ls -dl /tmpdrwxrwxrwt 6 root root 4096 08-22 11:37 /tmp
tmp目录是所有用户共有的临时文件夹,所有用户都拥有读写权限,这就必然出现一个问题,A用户在/tmp里创建了文件a.file,此时B用户看了不爽,在/tmp里把它给删了(因为拥有读写权限),那肯定是不行的。实际上是不会发生这种情况,因为有特殊权限stick bit(粘贴位)权限,正如drwxrwxrwt中的最后一个t
stick bit (粘贴位)就是:除非文件的属主和root用户有权限删除它,除此之外其它用户不能删除和修改这个文件。
也就是说,在/tmp目录中,只有文件的拥有者和root才能对其进行修改和删除,其他用户则不行,避免了上面所说的问题产生。用途一般是把一个文件夹的的权限都打开,然后来共享文件,象/tmp目录一样。
3、如何设置以上特殊权限
setuid:chmod u+s xxx
setgid: chmod g+s xxx
stick bit : chmod o+t xxx
或者使用八进制方式,在原先的数字前加一个数字,三个权限所代表的进制数与一般权限的方式类似,如下:
suid guid stick bit
1 11
所以:suid的二进制串为:100,换算十进制为:4
guid的二进制串为:010,换算:2
stick bit 二进制串:001,换算:1
于是也可以这样设:setuid:chmod 4755 xxx
setgid:chmod 2755 xxx
stick bit:chmod 1755 xxx
最后,在一些文件设置了特殊权限后,字母不是小写的s或者t,而是大写的S和T,那代表此文件的特殊权限没有生效,是因为你尚未给它对应用户的x权限 (可以参考文章末尾的英文)
四.文件详细信息里各个符号的解释
-rw-r--r-- 1 vivek admin 2558 Jan 8 07:41 filename
-rw-r--r--: file mode+permission(下面有介绍)1- number of links(硬链接数)vivek-Owner name(if user name is not a known user, the numeric user id displayed)admin-Group name(if group name is not a known group, the numeric group id displayed)2558- number of bytes in the file (file size)Jan 8 07:41- abbreviated month, day-of-month file was
last modified, hour file last modified, minute file last modifiedfilename- File name / pathname
ls -l file mode (permissions)
Quoting from the unix ls command man page - the file mode printed under the -l option consists of the entry type and the permissions. The entry type character describes the type of file, as follows:
d:表示是一个目录,事实上在ext2fs中,目录是一个特殊的文件。
-:表示这是一个普通的文件。
l: 表示这是一个符号链接文件,实际上它指向另一个文件。(即软连接)
b、c:分别表示区块设备和其他的外围设备,是特殊类型的文件。
s、p:这些文件关系到系统的数据结构和管道,通常很少见到。
The next three fields are three characters each: owner permissions, group permissions, and other permissions. Each field has three character positions:
If r, the file is readable; if -, it is not readable.If w, the file is writable; if -, it is not writable.The first of the following that applies: S: If in the owner permissions, the file is not executable and set-user-ID mode is set. If in the group permissions, the file is not executable and set-group-ID mode is set.s: If in the owner permissions, the file is executable and set-user-ID mode is set. If in the group permissions, the file is executable and set group-ID mode is set.x: The file is executable or the directory is searchable.-: The file is neither readable, writable, executable, nor set-user-ID nor set-group-ID mode, nor sticky. These next two apply only to the third character in the last group (other permissions). T: The sticky bit is set (mode 1000), but not execute or search permission.t: The sticky bit is set (mode 1000), and is search able or executable. 以上英文参考:UNIX / Linux Find File Owner Name