linux下更改文件的权限

来源:互联网 发布:ipad安装下载软件 编辑:程序博客网 时间:2024/06/06 10:56
  1. 更改所属组 chgrp

语法:chgrp  [组名]  [文件名]

[root@localhost ~]# groupadd testgroup[root@localhost ~]# touch test1[root@localhost ~]# ls -l test1-rw-r--r-- 1 root root 0 5月  10 08:41 test1[root@localhost ~]# chgrp testgroup test1[root@localhost ~]# ls -l test1-rw-r--r-- 1 root testgroup 0 5月  10 08:41 test1

这里用到了 ‘groupadd’ 命令,其含义为增加一个用户组。该命令在以后章节中做详细介绍,您只要知道它是用来增加用户组的即可。除了更改文件的所属组,还可以更改目录的所属组。

[root@localhost ~]# ls -l dirb/总用量 8drwxr-xr-x. 2 root root 4096 5月  10 05:08 dirc-rw-r--r--. 1 root root   20 5月  10 05:37 filee[root@localhost ~]# ls -ld dirb/drwxr-xr-x. 3 root root 4096 5月  10 05:10 dirb/[root@localhost ~]# chgrp testgroup dirb[root@localhost ~]# ls -ld dirb/drwxr-xr-x. 3 root testgroup 4096 5月  10 05:10 dirb/[root@localhost ~]# ls -l dirb/总用量 8drwxr-xr-x. 2 root root 4096 5月  10 05:08 dirc-rw-r--r--. 1 root root   20 5月  10 05:37 filee

‘chgrp’命令也可以更改目录的所属组,但是只能更改目录本身,而目录下面的目录或者文件没有更改,要想级联更改子目录以及子文件,有个选项可以实现:

[root@localhost ~]# chgrp -R testgroup dirb[root@localhost ~]# ls -l dirb总用量 8drwxr-xr-x. 2 root testgroup 4096 5月  10 05:08 dirc-rw-r--r--. 1 root testgroup   20 5月  10 05:37 filee

‘chgroup’ 命令阿铭使用的不多,因为还有一个命令可以替代。

  1. 更改文件的所属主 chown

语法: chown [ -R ] 账户名 文件名 chown [ -R ] 账户名:组名 文件名

这里的-R选项只作用于目录,作用是级联更改,即不仅更改当前目录,连目录里的目录或者文件全部更改。

[root@localhost ~]# mkdir test // 创建 'test' 目录[root@localhost ~]# useradd user1 // 创建用户 'user1', 关于 'useradd' 命令会在后续章节介绍。[root@localhost ~]# touch test/test2 // 在test目录下创建test2文件[root@localhost ~]# chown user1 test[root@localhost ~]# ls -l test总用量 0-rw-r--r-- 1 root root 0 5月  10 09:00 test2[root@localhost ~]# ls -ld test   // test目录所属主已经由 'root' 改为 'user1'.drwxr-xr-x 2 user1 root 4096 5月  10 09:00 test[root@localhost ~]# ls -l test  // 但是test目录下的test2文件所属主依旧是 'root'.总用量 0-rw-r--r-- 1 root root 0 5月  10 09:00 test2[root@localhost ~]# chown -R user1:testgroup test[root@localhost ~]# ls -l test总用量 0-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test2

‘chown -R user1:testgroup’ 把test目录以及目录下的文件都修改成所属主为user1, 所属组为testgroup.

  1. 改变用户对文件的读写执行权限 chmod

在linux中为了方便更改这些权限,linux使用数字去代替rwx, 具体规则为 ‘r’ 等于4, ‘w’ 等于2, ‘x’ 等于1, ‘-‘ 等于0. 举个例子: ‘-rwxrwx—’ 用数字表示就是 ‘770’, 具体是这样来的: ‘rwx’ = 4+2+1=7; ‘rwx’ = 4+2+1=7; ‘- - -‘ = 0+0+0=0.

chmod 语法: chmod [-R] xyz 文件名 (这里的xyz,表示数字)

‘-R’ 选项作用同chown,级联更改。

值得提一下的是,在linux系统中,默认一个目录的权限为 755,而一个文件的默认权限为644.

[root@localhost ~]# ls -ld testdrwxr-xr-x 2 user1 testgroup 4096 5月  10 09:00 test[root@localhost ~]# ls -l test总用量 0-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test2[root@localhost ~]# chmod 750 test[root@localhost ~]# ls -ld testdrwxr-x--- 2 user1 testgroup 4096 5月  10 09:00 test[root@localhost ~]# ls -l test/test2-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2[root@localhost ~]# chmod 700 test/test2[root@localhost ~]# chmod -R 700 test[root@localhost ~]# ls -ld testdrwx------ 2 user1 testgroup 4096 5月  10 09:00 test[root@localhost ~]# ls -l test总用量 0-rwx------ 1 user1 testgroup 0 5月  10 09:00 test2

如果您创建了一个目录,而该目录不想让其他人看到内容,则只需设置成 ‘rwxr—–’ (740) 即可。’chmod’ 还支持使用rwx的方式来设置权限。从之前的介绍中我们可以发现,基本上就九个属性分别是(1)user (2)group (3)others, 我们可以使用u, g, o 来代表它们三个的属性,此外, a 则代表 all 亦即全部。阿铭举例来介绍他们的用法:

[root@localhost ~]# chmod u=rwx,og=rx test/test2[root@localhost ~]# ls -l test/test2-rwxr-xr-x 1 user1 testgroup 0 5月  10 09:00 test/test2

这样可以把 ‘test/test2’ 文件权限修改为 ‘rwxr-xr-x’. 另外还可以针对u, g, o, a增加或者减少某个权限(读,写,执行),例如:

[root@localhost ~]# chmod u-x test/test2[root@localhost ~]# ls -l test总用量 0-rw-r-xr-x 1 user1 testgroup 0 5月  10 09:00 test2[root@localhost ~]# chmod a-x test/test2[root@localhost ~]# ls -l test/test2-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2[root@localhost ~]# chmod u+x test/test2[root@localhost ~]# ls -l test/test2-rwxr--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2

命令: umask

上边也提到了默认情况下,目录权限值为755, 普通文件权限值为644, 那么这个值是由谁规定呢?追究其原因就涉及到了 ‘umask’.

umask语法: umask  xxx (这里的xxx代表三个数字)

查看umask值只要输入 ‘umask’ 然后回车。

[root@localhost ~]# umask0022

umask预设是0022,其代表什么含义?先看一下下面的规则:

1)若用户建立为普通文件,则预设 ‘没有可执行权限’, 只有’rw’两个权限。最大为666 (‘-rw-rw-rw-‘).

2)若用户建立为目录,则预设所有权限均开放,即777 (‘drwxrwxrwx’).

umask数值代表的含义为,上边两条规则中的默认值(文件为666,目录为777)需要减掉的权限。所以目录的权限为 'rwxrwxrwx' - '----w--w-' = 'rwxr-xr-x',普通文件的权限为 'rw-rw-rw-' - '----w--w-' = 'rw-r--r--'. umask的值是可以自定义的,比如设定umask 为 002,您再创建目录或者文件时,默认权限分别为'rwxrwxrwx' - '-------w-' = 'rwxrwxr-x' 和 'rw-rw-rw-' - '-------w-' = 'rw-rw-r--'.

[root@localhost ~]# umask 002[root@localhost ~]# mkdir test2[root@localhost ~]# ls -ld test2drwxrwxr-x 2 root root 4096 5月  10 09:44 test2[root@localhost ~]# touch test3[root@localhost ~]# ls -l test3-rw-rw-r-- 1 root root 0 5月  10 09:45 test3

可以看到创建的目录权限默认变为775, 而文件默认权限变为664. 然后再把umask改回来。

[root@localhost ~]# umask 022[root@localhost ~]# touch test4[root@localhost ~]# ls -l test4-rw-r--r-- 1 root root 0 5月  10 09:45 test4

umask 可以在 /etc/bashrc 里面更改,预设情况下,root的umask为022,而一般使用者则为002,因为可写的权限非常重要,因此预设会去掉写权限。

  1. 修改文件的特殊属性

命令 : chattr

语法: chattr  [+-=][ASaci [文件或者目录名]

‘+-=’ : 分别为增加、减少、设定

‘A’ : 增加该属性后,文件或目录的atime将不可被修改;

‘S’ : 增加该属性后,会将数据同步写入磁盘中;

‘a’ : 增加该属性后,只能追加不能删除,非root用户不能设定该属性;

‘c’ : 自动压缩该文件,读取时会自动解压;

‘i’ : 增加后,使文件不能被删除、重命名、设定链接接、写入、新增数据;

[root@localhost ~]# chattr +i test2[root@localhost ~]# touch test2/test1touch: 无法创建'test2/test1': 权限不够[root@localhost ~]# chattr -i test2[root@localhost ~]# touch test2/test1[root@localhost ~]# chattr +i test2[root@localhost ~]# rm -f test2/test1rm: 无法删除'test2/test1': 权限不够

对 ‘test2’ 目录增加 ‘i’ 权限后,即使是root账户也不能在 ‘test2’ 里创建或删除test1文件。

[root@localhost ~]# chattr -i test2[root@localhost ~]# touch test2/test3[root@localhost ~]# ls test2test1  test3[root@localhost ~]# chattr +a test2[root@localhost ~]# rm -f test2/test1rm: 无法删除 'test2/test1': 不允许的操作[root@localhost ~]# touch test2/test4[root@localhost ~]# ls test2test1  test3  test4

test2目录增加 ‘a’ 权限后,只可以在里面创建文件,而不能删除文件。文件同样可以适用这些权限。

[root@localhost ~]# chattr +a test2/test1[root@localhost ~]# echo '11111' > test2/test1-bash: test2/test1: 不允许的操作[root@localhost ~]# echo '11111' >> test2/test1[root@localhost ~]# cat test2/test111111[root@localhost ~]# chattr +i test2/test3[root@localhost ~]# echo '11111' >> test2/test3-bash: test2/test3: 权限不够[root@localhost ~]# echo '11111' > test2/test3-bash: test2/test3: 权限不够[root@localhost ~]# rm -f test2/test3rm: 无法删除'test2/test3': 权限不够

命令 : lsattr

该命令用来读取文件或者目录的特殊权限,语法为 lsattr  [-aR] [文件/目录名]

‘-a’ : 类似与ls 的-a 选项,即连同隐藏文件一同列出;

‘-R’ : 连同子目录的数据一同列出

[root@localhost ~]# lsattr test2-----a-------e- test2/test1----i--------e- test2/test3-------------e- test2/test4[root@localhost ~]# lsattr -aR test2----i--------e- test2/.-----a-------e- test2/test1-------------e- test2/..----i--------e- test2/test3-------------e- test2/test4
0 0
原创粉丝点击