4-设置目录的权限及归属


2016.6.9

设置目录的权限chmod

  • 格式1

    • chmod [ugoa] [+-=] [rwx] 目录...
  • 格式2

    • chmod nnn 目录...
  • 常用选项

    • -R:递归修改制定目录下所有子项的权限
  • 示例

    • 针对目录/var/ftp/pub设置权限并测试

更改pub的权限

[root@test /]# mkdir -p /var/ftp/pub
[root@test /]# ls -ld /var/ftp/pub
drwxr-xr-x. 2 root root 4096 Jul  1 21:16 /var/ftp/pub
[root@test /]# chmod 750 /var/ftp/pub/
[root@test /]# ls -ld /var/ftp/pub/
drwxr-x---. 2 root root 4096 Jul  1 21:16 /var/ftp/pub/
[root@test /]# su test
[test@test /]$ ls /var/ftp/pub/
ls: cannot open directory /var/ftp/pub/: Permission denied  //test对pub没有读权限
[test@test /]$ su 
Password: 
[root@test /]# touch /var/ftp/var/test
touch: cannot touch `/var/ftp/var/test': No such file or directory
[root@test /]# cd /var/ftp/pub
[root@test pub]# touch test
[root@test pub]# vi test
[root@test pub]# 
[root@test pub]# 
[root@test pub]# cat test
This is test
[root@test pub]# su test
[test@test pub]$ cd /var     
[test@test var]$ cd /ftp
bash: cd: /ftp: No such file or directory
[test@test var]$ ls
account  crash  db     ftp    gdm  local  log   nis  preserve  spool  www
cache    cvs    empty  games  lib  lock   mail  opt  run       tmp    yp
[test@test var]$ cd ftp
[test@test ftp]$ ls
pub
[test@test ftp]$ cd pub
bash: cd: pub: Permission denied     //无法进入pub,test用户针对pub没有执行权限
[test@test ftp]$ ls pub
ls: cannot open directory pub: Permission denied
[test@test ftp]$ touch /var/ftp/pub/test2  //test针对pub没有写入权限
touch: cannot touch `/var/ftp/pub/test2': Permission denied
[test@test ftp]$ exit
exit
[root@test pub]# cd
[root@test ~]# chmod 750 -R /var/ftp/pub/
[root@test ~]# su test
[test@test root]$ cd /var
[test@test var]$ cd ftp
[test@test ftp]$ exit
exit
[root@test ~]# ls -ld /var
drwxr-xr-x. 23 root root 4096 Jul  1 21:16 /var

chmod 750 -R /var/ftp/pub/修改的是pub以下的文件和目录的权限,并不会更改var和ftp的权限。

只改变var的权限

[root@test ~]# chmod 750 /var
[root@test ~]# ls -ld /var
drwxr-x---. 23 root root 4096 Jul  1 21:16 /var
[root@test ~]# su test
[test@test root]$ cd /var
bash: cd: /var: Permission denied

使用-R来改变var的权限

[test@test root]$ chmod 750 -R /var
chmod: changing permissions of `/var': Operation not permitted
chmod: cannot read directory `/var': Permission denied
[test@test root]$ exit
exit
[root@test ~]# chmod 750 -R /var
chmod: changing permissions of `/var/run/vmblock-fuse': Function not implemented
chmod: changing permissions of `/var/run/vmblock-fuse/dev': Function not implemented
chmod: changing permissions of `/var/run/vmblock-fuse/blockdir': Function not implemented
[root@test ~]# ls -ld /var
drwxr-x---. 23 root root 4096 Jul  1 21:16 /var
[root@test ~]# cd /var
[root@test var]# ls -l
total 84
drwxr-x---.  2 root root 4096 May 24 00:08 account
drwxr-x---. 15 root root 4096 May 24 00:12 cache
drwxr-x---.  2 root root 4096 May 24 00:14 crash
drwxr-x---.  2 root root 4096 Nov 22  2013 cvs
drwxr-x---.  3 root root 4096 May 24 00:08 db
drwxr-x---.  3 root root 4096 May 24 00:08 empty
drwxr-x---.  3 root root 4096 Jul  1 21:16 ftp
drwxr-x---.  2 root root 4096 Sep 23  2011 games
drwxr-x---.  2 root gdm  4096 Nov 22  2013 gdm
drwxr-x---. 40 root root 4096 May 24 00:21 lib
drwxr-x---.  2 root root 4096 Sep 23  2011 local
drwxr-x---.  5 root lock 4096 Jul  1 20:49 lock
drwxr-x---. 14 root root 4096 Jul  1 19:47 log
lrwxrwxrwx.  1 root root   10 May 24 00:02 mail -> spool/mail
drwxr-x---.  2 root root 4096 Sep 23  2011 nis
drwxr-x---.  2 root root 4096 Sep 23  2011 opt
drwxr-x---.  2 root root 4096 Sep 23  2011 preserve
drwxr-x---. 31 root root 4096 Jul  1 19:52 run
drwxr-x---. 13 root root 4096 May 24 00:06 spool
drwxr-x---.  2 root root 4096 May 24 00:10 tmp
drwxr-x---.  6 root root 4096 May 24 00:06 www
drwxr-x---.  3 root root 4096 May 24 00:06 yp
[root@test var]# cd ftp
[root@test ftp]# ls -lh
total 4.0K
drwxr-x---. 2 root root 4.0K Jul  1 21:21 pub
[root@test ftp]#

可以看到使用-R之后,var下的文件和目录的权限都是750,除了连接文件。

设置目录的归属

  • 格式

    • chown 属主 目录
    • chown :属组 目录
    • chown 属主:属组 目录
  • 常用选项

    • -R:递归修改制定目录下所有文件、子目录的归属
  • 示例

    • 将/var/ftp/pub目录的属主有root改为ftp(调整后用户ftp将拥有“rwx”的权限)
[root@test var]# chown ftp /var/ftp/pub/
[root@test var]# ls -ld /var/ftp/pub
drwxr-x---. 2 ftp root 4096 Jul  1 21:21 /var/ftp/pub
[root@test var]#
[root@test /]# chown development:development /quotatest/*
chown: invalid user: `development:development'
[root@test /]# useradd development
useradd: group development exists - if you want to add this user to that group, use -g.
[root@test /]# passwd development
passwd: Unknown user name 'development'.
[root@test /]# useradd -g development development
[root@test /]# passwd development
Changing password for user development.
New password: 
BAD PASSWORD: it does not contain enough DIFFERENT characters
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@test /]# tail -1 /etc/passwd
development:x:504:501::/home/development:/bin/bash
[root@test /]# 
[root@test /]# chown development:development /quotatest/*
[root@test /]# ls -l /quotatest/
total 4
drwxrwxr-x. 3 development development 4096 Jul  1 19:50 ext4
[root@test /]# chmod g+s /quotatest/ext4/
[root@test /]# ls -l /quotatest/
total 4
drwxrwsr-x. 3 development development 4096 Jul  1 19:50 ext4

results matching ""

    No results matching ""