【Linux常用命令】 chmod

1. chmod

Usage: chmod [OPTION]... MODE[,MODE]... FILE...
  or:  chmod [OPTION]... OCTAL-MODE FILE...
  or:  chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.

  -c, --changes           like verbose but report only when a change is made
      --no-preserve-root  do not treat `/' specially (the default)
      --preserve-root     fail to operate recursively on `/'
  -f, --silent, --quiet   suppress most error messages
  -v, --verbose           output a diagnostic for every file processed
      --reference=RFILE   use RFILE's mode instead of MODE values
  -R, --recursive         change files and directories recursively
      --help     display this help and exit
      --version  output version information and exit

Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.

$ chmod --version
chmod (GNU coreutils) 5.97

mode 权限设定字符串。其中:

u:User,即文件或目录的拥有者。
g:Group,即文件或目录的所属群组。
o:Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围。
a:All,即全部的用户,包含拥有者,所属群组以及其他用户。

+ 表示增加权限、- 表示取消权限、= 表示唯一设定权限。

r:读取权限,数字代号为"4"。 w:写入权限,数字代号为"2"。 x:执行或切换权限,数字代号为"1"。 -:不具任何权限,数字代号为"0"。
X:表示只有当该档案是个子目录或者该档案已经被设定过为可执行。
s:
t:
S:
T:
关于s,t:小写表示能执行,大写表示不能执行。
(这两个请参考:
1.
http://hi.baidu.com/reyleon/blog/item/0a0a6c115dab83e7c2ce7933.html
2. http://hi.baidu.com/d8t8/blog/item/908e9251e1a846100df3e341.html

举例:

[loong@localhost Desktop]$ ll c
-rw-rw-r-- 1 loong loong 31 Apr 19 19:33 c
[loong@localhost Desktop]$ chmod -c u=r,g=r,o=- c
mode of `c' changed to 0440 (r--r-----)
[loong@localhost Desktop]$ chmod -c 664 c
mode of `c' changed to 0664 (rw-rw-r--)

1.1 -c 使用示例:仅在mode改变时,输出提示

[loong@localhost Desktop]$ ll a.txt 
-rw-rw-r-- 1 loong loong 30 Apr 19 19:03 a.txt
[loong@localhost Desktop]$ chmod -c 664 a.txt 
[loong@localhost Desktop]$ chmod -c 666 a.txt 
mode of `a.txt' changed to 0666 (rw-rw-rw-)
[loong@localhost Desktop]$ ll a.txt 
-rw-rw-rw- 1 loong loong 30 Apr 19 19:03 a.txt

1.2 -v 使用示例:

[loong@localhost Desktop]$ ll a.txt 
-rw-rw-rw- 1 loong loong 30 Apr 19 19:03 a.txt
[loong@localhost Desktop]$ chmod -v 666 a.txt 
mode of `a.txt' retained as 0666 (rw-rw-rw-)
[loong@localhost Desktop]$ chmod -v 664 a.txt 
mode of `a.txt' changed to 0664 (rw-rw-r--)

1.3 -R 递归地更改文件和目录的权限(这个还是少用吧,尤其是嵌套子目录太多的时候,容易引起异常)

drwxrwxr-x 3 loong loong 4096 Apr 19 20:34 test
[loong@localhost Desktop]$ ll test
total 4
drwxrwxr-x 2 loong loong 4096 Apr 19 20:45 a
[loong@localhost Desktop]$ ll -R test
test:
total 4
drwxrwxr-x 2 loong loong 4096 Apr 19 20:45 a

test/a:
total 4
-rw-rw-r-- 1 loong loong 16 Apr 19 20:45 a.txt
[loong@localhost Desktop]$ chmod -cR 777 test
mode of `test' changed to 0777 (rwxrwxrwx)
mode of `test/a' changed to 0777 (rwxrwxrwx)
mode of `test/a/a.txt' changed to 0777 (rwxrwxrwx)
[loong@localhost Desktop]$ ll -R test
test:
total 4
drwxrwxrwx 2 loong loong 4096 Apr 19 20:45 a

test/a:
total 4
-rwxrwxrwx 1 loong loong 16 Apr 19 20:45 a.txt
[loong@localhost Desktop]$ chmod -cR 666 test
mode of `test' changed to 0666 (rw-rw-rw-)
chmod: `test': Permission denied
[loong@localhost Desktop]$ ll -R test
test:
total 0
?--------- ? ? ? ?            ? a
ls: test/a: Permission denied

呃,test文件夹下的a文件夹,出问题了~~权限不允许?换成root试试~

[root@localhost Desktop]# ll -R test
test:
total 4
drwxrwxrwx 2 loong loong 4096 Apr 19 20:45 a

test/a:
total 4
-rwxrwxrwx 1 loong loong 16 Apr 19 20:45 a.txt

哦,原来是将test的权限改为666(rw-rw-rw-)后,owner(也就是loong)就没有执行的权限了~~不能cd test……但是还可以chmod……改回来就是了。。。

[loong@localhost Desktop]$ chmod -v ug+x test
mode of `test' changed to 0776 (rwxrwxrw-)
[loong@localhost Desktop]$ ll -R test test: total 4 drwxrwxrwx 2 loong loong 4096 Apr 19 20:45 a test/a: total 4 -rwxrwxrwx 1 loong loong 16 Apr 19 20:45 a.txt

1.4 关于linux中 s、t权限的说明

以下转自: http://hi.baidu.com/reyleon/blog/item/0a0a6c115dab83e7c2ce7933.html

特殊权限(SUID/SGID/SBIT)

 

[loong@localhost Desktop]$ ll /usr/bin/passwd 
-rwsr-xr-x 1 root root 23420 Aug 11  2010 /usr/bin/passwd
[loong@localhost Desktop]$ ll /usr/bin/wall 
-r-xr-sr-x 1 root tty 10580 Jul 22  2011 /usr/bin/wall
[loong@localhost Desktop]$ ll -d /tmp
drwxrwxrwt 20 root root 4096 Apr 20 15:12 /tmp

第一个passwd文件在owner的rwx的执行x的地方为 s,

第二个wall文件在group的rwx的执行x的地方为 s,

第三个tmp文件夹的other的rwx的执行x的地方为 t。

1.4.1 Set UID

 当s这个标志出现在文件所有者的x权限上时,如/usr/bin/passwd这个文件的权限状态:“-rwsr-xr-x.”,此时就被称为Set UID,简称为SUID。那么这个特殊权限的特殊性的作用是什么呢?

1、SUID权限仅对二进制程序(binary program)有效;

2、执行者对于该程序需要具有x的可执行权限;

3、本权限仅在执行该程序的过程中有效(run-time);

4、执行者将具有该程序拥有者(owner)的权限。

SUID的目的就是:让本来没有相应权限的用户运行这个程序时,可以访问他没有权限访问的资源。

passwd就是一个很鲜明的例子,下面我们就来了解一下这相passwd执行的过程。

我们知道,系统中的用户密码是保存在/etc/shadow中的,而这个文件的权限是———-. (这个权限和以前版本的RHEL也有差别,以前的是-r——–)。其实有没有r权限不重要,因为我们的root用户是拥有最高的权限,什么都能干了。关键是要把密码写入到/etc/shadow中。我们知道,除了root用户能修改密码外,用户自己同样也能修改密码,为什么没有写入权限,还能修改密码,就是因为这个SUID功能。

-----------------------分割线---------------------------

下面就是passwd这个命令的执行过程

1、因为/usr/bin/passwd的权限对任何的用户都是可以执行的,所以系统中每个用户都可以执行此命令。

2、而/usr/bin/passwd这个文件的权限是属于root的。

3、当某个用户执行/usr/bin/passwd命令的时候,就拥有了root的权限了。

4、于是某个用户就可以借助root用户的权力,来修改了/etc/shadow文件了。

5、最后,把密码修改成功。

注:这个SUID只能运行在二进制的程序上(系统中的一些命令),不能用在脚本上(script),因为脚本还是把很多的程序集合到一起来执行,而不是脚本自身在执行。同样,这个SUID也不能放到目录上,放上也是无效的。

1.4.2 Set GID

我们前面讲过,当s这个标志出现在文件所有者的x权限上时,则就被称为Set UID。那么把这个s放到文件的所属用户组x位置上的话,就是SGID。如开头的/usr/bin/wall命令。

那么SGID的功能是什么呢?和SUID一样,只是SGID是获得该程序所属用户组的权限。

这相SGID有几点需要我们注意:

1、SGID对二进制程序有用;

2、程序执行者对于该程序来说,需具备x的权限;

3、SGID主要用在目录上;

理解了SUID,我想SGID也很容易理解。如果用户在此目录下具有w权限的话,若使用者在此目录下建立新文件,则新文件的群组与此目录的群组相同。

1.4.3 Sticky Bit

这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。

SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。

最具有代表的就是/tmp目录,任何人都可以在/tmp内增加、修改文件(因为权限全是rwx),但仅有该文件/目录建立者与 root能够删除自己的目录或文件。

注:这个SBIT对文件不起作用。

  

1.4.4 关于 SUID/SGID/SBIT权限设置的例子

注,博主懒了,直接copy了原作者的例子,特注明出处:http://hi.baidu.com/reyleon/blog/item/0a0a6c115dab83e7c2ce7933.html

View Code
SUID/SGID/SBIT权限设置

和我们前面说的rwx差不多,也有两种方式,一种是以字符,一种是以数字。
4 为 SUID = u+s
2 为 SGID = g+s
1 为 SBIT = o+t
下面我们就来看看如何设置,并看看达到的效果。

先看SUID的作用及设置

[root@yufei ~]# cd /tmp/
[root@yufei tmp]# cp /usr/bin/passwd ./
[root@yufei tmp]# mkdir testdir
上面两步是在/tmp目录下创建passwd文件和testdir目录
下面看看这两个的权限
[root@yufei tmp]# ls -l passwd ; ls -ld testdir/
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
drwxr-xr-x. 2 root root 4096 Jan 20 19:25 testdir/
我们切换到yufei用户,然后修改自己的密码
[root@yufei tmp]# su yufei
[yufei@yufei tmp]$ ./passwd
Changing password for user yufei.
Changing password for yufei.
(current) UNIX password:
New password:           
Retype new password:
passwd: Authentication token manipulation error
发现上面的yufei改不了自己的密码,为什么呢?就是因为没有权限把密码写入到/etc/shadow中。想让普通用户能修改/etc/shadow的话,那就需要用到SUID了。
[yufei@yufei tmp]$ su root
Password:
[root@yufei tmp]# chmod u+s passwd
[root@yufei tmp]# ls -l passwd
-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
看到有SUID权限了,下面再来修改yufei自己的密码
[yufei@yufei tmp]$ ./passwd
Changing password for user yufei.
Changing password for yufei.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
我们发现已经成功了。
我想这一下,你对SUID的作用已经了解了吧。
如果想把这个改回来(就是把SUID的权限去掉),我们用数字方式来设置
[root@yufei tmp]# chmod 0755 passwd
[root@yufei tmp]# ls -l passwd
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
OK这样就改过来了,这个数字的原理和我们前面讲的rwx是一样的,只是在最前面设置相应的数字而已。

注:在普通用户修改自己的密码是,密码要设置的复杂点,否则的话,通过不了认证,普通用户和root用户的权限是不同的。

再看SGID的作用及设置

我们以前面建立的/tmp/testdir为例子
[root@yufei tmp]# ls -ld testdir/
[root@yufei tmp]# chmod 757 testdir/
[root@yufei tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/
这时候,任何用户对此目录都有写入权限,那么我们就在这个目录里面创建文件与目录,并看看他们的权限如何
[root@yufei tmp]# su yufei
[yufei@yufei tmp]$ touch testdir/file1
[yufei@yufei tmp]$ mkdir testdir/dir1
[yufei@yufei tmp]$ ls -l testdir
total 0
drw-rw-r–. 1 yufei yufei 0 Jan 21 10:33 dir1
-rw-rw-r–. 1 yufei yufei 0 Jan 21 10:33 file1
这时候的文件与目录权限都是创建者的本身
下面我们就来看看,把这个目录加上SGID权限后,再创建文件与目录,会是什么样的效果
[yufei@yufei tmp]$ su root
Password:
[root@yufei tmp]# chmod g+s testdir/
[root@yufei tmp]# ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/
[root@yufei tmp]# su yufei
[yufei@yufei tmp]$ touch testdir/file2
[yufei@yufei tmp]$ mkdir testdir/dir2
[yufei@yufei tmp]$ ls -l testdir/
total 0
drw-rw-r–. 1 yufei yufei 0 Jan 21 10:33 dir1
drw-rw-r–. 1 yufei root  0 Jan 21 10:36 dir2
-rw-rw-r–. 1 yufei yufei 0 Jan 21 10:33 file1
-rw-rw-r–. 1 yufei root  0 Jan 21 10:35 file2
[yufei@yufei tmp]$ ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:36 testdir/
这时候我们就发现,file2和dir2的用户组变成了root了,也就是他们上层目录testdir这个目录的所属用户组。
这个应用,应用在一个项目的共同开发上,是很方便的。
[yufei@yufei tmp]$ su root
Password:
[root@yufei tmp]# chmod g-s testdir/
[yufei@yufei tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 10:36 testdir/
这样就还原了

最后我们来看SBIT的作用及设置

[root@yufei tmp]# rm -fr testdir/*
[root@yufei tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:42 testdir/
清空/tmp/testdir/目录里面的全部内容。
我们切换成普通用户,然后再里面创建文件,至少需要两个普通用户来测试这个,如果没有的话,就自己建立。
[root@yufei tmp]# su yufei
[yufei@yufei tmp]$ touch testdir/yufei_file
[yufei@yufei tmp]$ ls -l testdir/
total 0
-rw-rw-r– 1 yufei yufei 0 Jan 21 11:45 yufei_file
这时候我们建立了一个文件,我们换成另外一个用户
[yufei@yufei tmp]$ suopsers
Password:
[opsers@yufei tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:45 testdir/
我们看到,虽然其他用户对yufei_file只有只读权限,但由于yufei_file所在的目录,对其他人是全部的权限,所以,我们换其他用户还是可以删除这个文件的,看操作
[opsers@yufei tmp]$ rm -f testdir/yufei_file
[opsers@yufei tmp]$ ls testdir/
发现我们已经删除了这个不属于我们的权限。
下面我们就给这个目录加上SBIT权限,再来看看效果
[opsers@yufei tmp]$ su root
Password:
[root@yufei tmp]# chmod o+t testdir
[root@yufei tmp]# ls -ld testdir/
drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/
再一次切换普通用户,创建文件
[root@yufei tmp]# su yufei
[yufei@yufei tmp]$ touch testdir/yufei_file
[yufei@yufei tmp]$ ls -l testdir/yufei_file
-rw-rw-r– 1 yufei yufei 0 Jan 21 11:51 testdir/yufei_file
这个文件的权限还是和第一次创建的时候是一样的,我们再换成其他的用户,看看能不能再次删除这个文件
[yufei@yufei tmp]$ su opsers
Password:
[opsers@yufei tmp]$ rm -f testdir/yufei_file
rm: cannot remove `testdir/yufei_file’: Operation not permitted
看到提示,说权限不够了,只能由这个文件的创建者或root用户才能删除。这个我们就不演示了。
如果要还原权限的话,
[opsers@yufei tmp]$ su root
Password:
[root@yufei tmp]# chmod o-t testdir
[root@yufei tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:51 testdir/

两个需要注意的问题

OK,关于SUID/SGID/SBIT这些特殊权限的应用和作用我们已经讲完了。但如果你仔细一点的话,会发现,我并没有用数字方式来更改这个特殊的权限,为什么呢?且看下面的分析。

问题1:用数字改变目录的特殊权限,不起作用。

我们把/tmp/下面,我们自己建立的实验文件删除
[root@yufei tmp]# rm -fr testdir/
[root@yufei tmp]# rm -fr passwd
然后再重新创建一个文件和目录,
[root@yufei tmp]# cp /usr/bin/passwd ./
[root@yufei tmp]# mkdir testdir
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-xr-x 2 root root 4096 Jan 21 12:00 testdir/
下面我们就来用数字方式来更改这三个特殊的权限,看看会有什么样的结果
[root@yufei tmp]# chmod 4755 passwd
[root@yufei tmp]# chmod 3755 testdir/
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwsr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
发现用这种方式增加这三个特殊权限没有问题,那么我们再把权限改回去看看
[root@yufei tmp]# chmod 0755 passwd
[root@yufei tmp]# chmod 0755 testdir/
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
我们发现,对文件,权限是改回去了,而对于目录,只改回去了SBIT的权限,对SUID和SGID改不回去。这是RHEL6上的实验结果,可能是出于安全性的考虑吗?这个我就不清楚了,也找不到相关的资料。如果各位网友,有知道什么原因的,欢迎与我联系。在此先谢过了。
所以说,建议大家还是用最明了的方式,直接用+-来更改,无论方法如何,最终能得到结果就OK了。哈哈……

问题2:为什么会有大写的S和T。

还是用上面的文件和目录
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
我们把passwd和testdir的x权限去掉
[root@yufei tmp]# chmod u-x passwd
[root@yufei tmp]# chmod o-x testdir/
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rw-r-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr– 2 root root 4096 Jan 21 12:00 testdir/
再给他们加上SUID和SBIT权限
[root@yufei tmp]# chmod u+s passwd
[root@yufei tmp]# chmod o+t testdir/
[root@yufei tmp]# ls -l passwd ;ls -ld testdir/
-rwSr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-T 2 root root 4096 Jan 21 12:00 testdir/
我们看到,这时候的小s和小t已经变成了大S和大T了,为什么呢?因为他们这个位置没有了x权限,如果没有了x权限,根据我们上面讲的内容,其实,这个特殊的权限就相当于一个空的权限,没有意义。也就是说,如果你看到特殊权限位置上变成了大写的了,那么,就说明,这里有问题,需要排除。

另外参考: http://hi.baidu.com/d8t8/blog/item/908e9251e1a846100df3e341.html

原文地址:https://www.cnblogs.com/xlmeng1988/p/chmod.html