文件的属性查看及修改、文件的特殊权限、acl列表

######################文件的属性查看及修改、文件的特殊权限、acl列表##########

 

1、文件属性查看

   ls     -l         file
  -     rw-r--r--      1     root       root      0     Dec 31 01:27     file
 [1]    [2]            [3]      [4]          [5]      [6]               [7]              [8]

[1]   文件属性的查看   
        -        #普通文件
       d        #目录
       s        #socket套接子
        l        #软链接
       p        #管道
       c        #字符设备
      b        #块设备

[2]    文件读写权限
rw-     r--       r-- 

u       g          o

u      #文件拥有者对文件能做的动作
g       #文件所在组的组成员能对文件做的动作
o        #其他人对与文件能做的动作

r=4:对文件:可以查看文件的字符(内容)
      对目录:可以查看目录中的文件/目录的信息(这个目录里都有啥)

w=2:对文件:可以更改文件内的字符(增删改)
    对目录:在目录中进行增删改

x=1:对文件:可以运行文件中记录的程序动作
    对目录:可以进入目录

[3]  对文件:文件内容被系统记录的次数
      对目录:目录内里二级子目录的个数(ls -a)(不包括文件的个数)

[4]   文件拥有者
[5]   文件所属组
[6]   文件内容大小
[7]   文件最后一次被更改的时间
[8]    文件名称

 

2.文件用户组的更改

chown       用户名称    文件|目录           ##更改文件所有人
chgrp         组名称        文件|目录          ##更改文件所有组
chown     -R     用户名       目录|文件     ##更改目录本身以及目录中的子文件的所有人
chgrp      -R      组名        目录|文件       ##更改目录本身以及目录中的子文件的所有组

chown    用户名.组名   目录|文件          ## 更改文件或目录的拥有组和所属组

1、更改文件的所有人

2、更改文件的所有组

3、更改文件的所有人和所有组

4、更改目录及目录子文件的所有人

3.权限的识别

rwx           r-x                r-x
用户权限(u)     组成员权限(g)      其他用户权限(o)

权限种类
r
r权限针对文件,表示可以查看文件内容
r权限针对目录,表示可以ls 查看目录中存在的文件名称

w
w权限针对文件,表示可以更改文件的内容
w权限针对目录,表示是否可以删除目录中的子文件或者子目录

x
x权限对于文件,表示是否可以开启文件当中记录的程序
x权限对于目录,表示是否可以进入目录中

4、权限的更改:

chmod     XXX      文件/目录

7=rwx,6=rw-,5=r-x,4=r--,3=-wx,2=-w-,1=--x,0=---

5、系统默认权限的设定:

     系统设定新建文件或目录会去掉一些权限
设定方式:
umask(普通用户和超级用户系统预留权限不一致) #查看系统预留权限
umask xxx    #修改系统的预留权限(此设定是临时设定只对当前用户及当前shell生效)
系统默认目录满权限为777,文件满权限为666
永久设定:
[root@localhost Desktop]# vim /etc/bashrc
 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002           普通用户系统预留权限
    else
       umask 022           超级用户系统预留权限
    fi

[root@localhost Desktop]# vim /etc/profile
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
以上两个文件的更改后值必须一致
改完后再source  /etc/bashrc
                source  /etc/profile

让系统重新加载配置文件,让设定立即生效。

6、特殊权限

1、suid    ##冒险位   u+s  /  4xxx
只针对二进制可执行文件,文件内记录的程序产生的进程的拥有者为文件的拥有者和进程的发起人没关系。
[root@localhost mnt]# su - student
Last login: Mon Dec 31 03:03:07 EST 2018 on pts/1
[student@localhost ~]$ cd /mnt
[student@localhost mnt]$ touch file3
[student@localhost mnt]$ ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
[student@localhost mnt]$ exit
logout
[root@localhost mnt]#
[root@localhost mnt]#
[root@localhost mnt]# ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
[root@localhost mnt]# chmod u+s /usr/bin/touch
[root@localhost mnt]# ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
[root@localhost mnt]# ll /usr/bin/touch
-rwsr-xr-x. 1 root root 62432 Jan 24  2014 /usr/bin/touch
[root@localhost mnt]# su - student
Last login: Mon Dec 31 03:32:04 EST 2018 on pts/1
[student@localhost ~]$ cd /mnt
[student@localhost mnt]$ touch file4
[student@localhost mnt]$ ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
-rw-rw-r-- 1 root    student 0 Dec 31 03:33 file4
[student@localhost mnt]$ exit
logout
########文件内记录的程序产生的进程的拥有者为文件的拥有者和进
程的发起人没关系。此时我们用student用户新建的文件file4拥有者为root。
[root@localhost mnt]# chmod u-s /usr/bin/touch
[root@localhost mnt]# ll /usr/bin/touch
-rwxr-xr-x. 1 root root 62432 Jan 24  2014 /usr/bin/touch
[root@localhost mnt]# chmod 4755 /usr/bin/touch
[root@localhost mnt]# ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
-rw-rw-r-- 1 root    student 0 Dec 31 03:33 file4
[root@localhost mnt]# ll /usr/bin/touch
-rwsr-xr-x. 1 root root 62432 Jan 24  2014 /usr/bin/touch
[root@localhost mnt]# su - student
Last login: Mon Dec 31 03:33:30 EST 2018 on pts/1
[student@localhost ~]$ cd /mnt
[student@localhost mnt]$ touch file5
[student@localhost mnt]$ ll
total 0
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file
-rw-r--r-- 1 root    root    0 Dec 31 03:31 file2
-rw-rw-r-- 1 student student 0 Dec 31 03:32 file3
-rw-rw-r-- 1 root    student 0 Dec 31 03:33 file4
-rw-rw-r-- 1 root    student 0 Dec 31 03:35 file5

 

2、sgid        ###强制位
g+s               ##针对目录,在目录中创建的文件都自动归属到目录所在组,针对二进制文件,文件内记录的程序在执行时和执行者的组身份没有关系,而是以二进制文件的所有组的身份执行的

chmod    g+s     file|directory
chmod    2777    file|directory
eg:   mkdir  /xd
ls -ld    /xd   查看一下/xd目录下的组名称

chgrp  linux  /xd   更改/xd目录的组为linux
chmod   777 /xd   赋予其777的权限
切换用户   su - student
touch /xd/file    在student用户的xd目录中新建一个file文件

ls -l  /xd/file   查看建立的这的文件的组名称发现为student并非为linux
此时退出student用户在超级用户下 chmod  g+s  /xd
此时进入student用户中touch /xd/file1
ls -l /xd/file1    查看这个file1这个文件的信息发现此时文件组为linux

3、stickyid   ###粘制位
o+t          ###指针对目录,当一个目录上有t权限时,这个目录中的文件只能被文件拥有者删除

t=1
chmod o+t directroy
chmod 1777 directory

eg:mkdir    /westos  新建一个目录/westos
        chmod  777 /westos   权限为777(all人都有读写执行权力)
        chmod o+t   /westos(chmod    1777    /westos)   加入特殊权限这个目录的文件只能被文件拥有者删除(用户westos建立的文件只能被westos删除

7、acl列表:

acl=access cointrol  指定特殊的用户对特殊的文件有特殊的权限

(1)文件的acl列表
[root@localhost mnt]# ll
-rw-rwxr--+ 1 root root 0 Dec 31 01:54 file
                ^   有此加号表示acl列表开启
##注意:当文件/目录有权限列表时此时用getfacl file 来查看文件的所有权限,用ls -l查看的文件权限不太准确。
[root@localhost mnt]# setfacl -m g:student:rw  file1    设定student组对file1文件有rw权限
[root@localhost mnt]# setfacl -m u:student:rw  file1    设定student用户对file1文件有rw权限

-m 设定  u 用户   g 组
[root@localhost mnt]# getfacl file1
# file: file1   ##目录/文件的名称
# owner: root   ##d/f 的拥有者
# group: root   ##d/f 的所属组
user::rw-       ##拥有者的权限
group::r--      ##所属组的权限
group:student:rw-  ##acl列表中(特殊组)的权限
mask::rw-       ##权限掩码(acl)列表中最大权限
other::r--      ##其他人的权限
删除列表中的用户或者组
[root@localhost mnt]# setfacl -x g:student  file1

查看文件的属性
[root@localhost mnt]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
修改acl列表中的mask值
[root@localhost mnt]# setfacl -m m:rw file
[root@localhost mnt]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:student:rwx   #effective:rw-  ##尽管我们赋予student了rwx权限但是因为我们的mask值为rw因此我们的acl的有效值最大为rw
group::r--
mask::rw-
other::r--
关闭acl列表:
[root@localhost mnt]# setfacl -b file
[root@localhost mnt]# ls -l
-rw-r--r-- 1 root root 0 Dec 31 01:54 file

###可能:当你用chmod改变文件普通权限的时候可能会破坏acl mask
(2)对目录及新建的子目录子文件
acl的默认权限
当我们需要普通用户对属于root的某个目录拥有写的权限时,并且目录中新建的子目录对普通用户也生效,就要设定acl默认权限
注意:默认权限也只对目录中新建的目录或者文件生效,对已经建立的目录和文件无效,对目录本身也无效。

[root@localhost mnt]# su - student
Last login: Mon Dec 31 01:04:57 EST 2018 on pts/0
[student@localhost ~]$ cd /mnt
[student@localhost mnt]$ ls
westos
[student@localhost mnt]$ cd westos
[student@localhost westos]$ ls
[student@localhost westos]$ touch file
touch: cannot touch ‘file’: Permission denied
[student@localhost westos]$ exit
logout
[root@localhost mnt]# setfacl -m d:u:student:rwx /mnt/westos
给此目录/mnt/westos对于student用户赋予rwx权限
[root@localhost mnt]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:student:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@localhost mnt]# su - student
Last login: Mon Dec 31 02:57:23 EST 2018 on pts/1
[student@localhost ~]$ cd /mnt/westos/
[student@localhost westos]$ touch file
touch: cannot touch ‘file’: Permission denied
此时还是不可以新建文件因为facl赋予的权力只针对在此目录下新建目录有效westos目录时已经建立的目录对其无效,只有在其目录之下新建的目录及目录文件有效
[student@localhost westos]$ exit
logout
[root@localhost mnt]# cd westos/
[root@localhost westos]# mkdir linux
[root@localhost westos]# su - student
Last login: Mon Dec 31 03:02:05 EST 2018 on pts/1
[student@localhost ~]$ cd /mnt/westos/
[student@localhost westos]$ ls
linux
[student@localhost westos]$ cd linux/
[student@localhost linux]$ touch file  此时可以建立文件了
[student@localhost linux]$ ll
total 0
-rw-rw-r--+ 1 student student 0 Dec 31 03:03 file
[student@localhost linux]$ cd ..
[student@localhost westos]$ ll
total 4
drwxrwxr-x+ 2 root root 17 Dec 31 03:03 linux
[student@localhost westos]$ cd ..
[student@localhost mnt]$ ll
total 0
drwxr-xr-x+ 3 root root 18 Dec 31 03:03 westos

#练习
1.新建用户组,shengchan,caiwu,jishu

2.新建用户要求如下:
    1)tom 是shengchan组的附加用户
    2)harry 是caiwu组的附加用户
    3)leo 是jishu组的附加用户
    4)新建admin用户,此用户不属于以上提到的三个部门
3.新建目录要求如下:
    1)/pub目录为公共存储目录对所有用户可以读,写,执行,但用户只能删除自己的文件
    2)/sc 目录为生产部存储目录只能对生产部人员可以写入,并且生产部门人员所建立的文件都各自动归属到shengchan里
    3)/cw 目录为财务部存储目录只能对财务部人员可以写入中,并且财务部门人员所建立的文件都各自动归属到caiwu里

    4)admin用户能用touch工具在/sc目录和/cw目录中任意建立文件,但不能删除文件。

4、设定普通用户新建文件权限为r--r-----

5、设定admin用户可以通过sudo自由建立新用户

1、groupadd      shengchan

    groupadd      caiwu

    groupadd   jishu

2、useradd  -G  shengchan     tom

useradd  -G  caiwu    harry

useradd  -G  jishu     leo

useradd   admin

3、(1)

mkdir  /pub  /sc  /cw

chmod     1777  /pub

(2)

chgrp     shengchan      /sc

chmod     2575 /sc

(3)

chgrp   caiwu      /cw

chmod   2575     /cw

(4)

[root@localhost Desktop]# rm -fr /sc
[root@localhost Desktop]# rm -fr /cw
[root@localhost Desktop]# setfacl -m d:u:admin:rwx  /
[root@localhost Desktop]# mkdir /sc
[root@localhost Desktop]# mkdir  /cw
[root@localhost Desktop]# which touch
/usr/bin/touch
[root@localhost Desktop]# chmod  u+s   /usr/bin/touch
[root@localhost Desktop]# chmod  2575 /sc
[root@localhost Desktop]# chmod  2575 /cw
[root@localhost Desktop]# chgrp  shengchan /sc
[root@localhost Desktop]# chgrp  caiwu  /sc
[root@localhost Desktop]# su - admin
Last login: Sat Jan  5 21:28:33 EST 2019 on pts/1
-bash: /tmp/.colorlsw7z: Permission denied
[admin@localhost ~]$ cd /sc
[admin@localhost sc]$ touch file
[admin@localhost sc]$ rm -fr file
[root@localhost Desktop]# chmod  o+t  /sc
[root@localhost Desktop]# chmod  o+t  /cw
[root@localhost Desktop]# su - admin
Last login: Sat Jan  5 21:47:18 EST 2019 on pts/1
[admin@localhost sc]$ ls -l
total 0
-rw-rw-r--+ 1 root caiwu 0 Jan  5 21:47 file
[admin@localhost sc]$ rm -fr file
rm: cannot remove ‘file’: Operation not permitted

 

4、[root@localhost Desktop]# vim /etc/bashrc
 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 226           普通用户系统预留权限
    else
       umask 022           超级用户系统预留权限
    fi

[root@localhost Desktop]# vim /etc/profile
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 226
else
    umask 022
source  /etc/bashrc
source  /etc/profile

5、passwd   admin

超级用户执行visudo进入编辑/etc/sudoers模式

在文件第100行左右写入  admin(你刚建的用户名)  主机名(hostname出来的名字)=(root) NOPASSWD: /usr/sbin/useradd

切换到普通用户 su - admin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值