zoukankan      html  css  js  c++  java
  • Linux权限管理

    权限管理

    1.权限管理命令

    1.1.修改权限的命令chmod

    //修改三类用户的权限:
    //语法:chmod MODE file,...
    -R //递归修改权限

    [root@localhost ~]# ll
    总用量 8
    -r--rwxr--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod 777 a
    [root@localhost ~]# ll
    总用量 8
    -rwxrwxrwx. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    

    //修改某类用户或某些类用户权限:
    //u,g,o,a(用户类别)

    //chmod 用户类别=MODE file,.....
    //chmod 用户类别=MODE,用户类别=MODE file,.....

    [root@localhost ~]# ll
    总用量 8
    -rwxrwxrwx. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod u=r a
    [root@localhost ~]# ll
    总用量 8
    -r--rwxrwx. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod g=rw a
    [root@localhost ~]# ll
    总用量 8
    -r--rw-rwx. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod o=rw a
    [root@localhost ~]# ll
    总用量 8
    -r--rw-rw-. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod a=r a
    [root@localhost ~]# ll
    总用量 8
    -r--r--r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    

    //修改某类的用户某位或某些位权限:
    //u,g,o,a(用户类别)

    //chmod 用户类别+|-MODE file,.....
    //chmod 用户类别+|-MODE,用户类别+|-MODE file,.....
    //chmod +|-MODE file,.....

    [root@localhost ~]# ll
    总用量 8
    -r--r--r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod u+w a
    [root@localhost ~]# ll
    总用量 8
    -rw-r--r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod u-r a
    [root@localhost ~]# ll
    总用量 8
    --w-r--r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod g+wx a
    [root@localhost ~]# ll
    总用量 8
    --w-rwxr--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod +x a
    [root@localhost ~]# ll
    总用量 8
    --wxrwxr-x. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    [root@localhost ~]# chmod -x a
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    

    1.2.修改文件属主和属组的命令chown

    chown命令只有管理员可以用
    //chown USERNAME file,...
    -R //修改目录及其内部文件的属主

    //chown USERNAME:GROUPNAME file,...
    //chown USERNAME.GROUPNAME file,...

    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 29 9月 20 14:26 yingyu
    [root@localhost ~]# chown liping anaconda-ks.cfg 
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 29 9月 20 14:26 yingyu
    [root@localhost ~]# chown .root a
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping root 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 29 9月 20 14:26 yingyu
    [root@localhost ~]# chown liping.liping a
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 29 9月 20 14:26 yingyu
    [root@localhost ~]# ll yingyu/
    总用量 0
    -rw-r--r--. 1 root root 0 9月 20 14:26 hello
    -rw-r--r--. 1 root root 0 9月 20 14:26 hi
    [root@localhost ~]# chown -R liping.liping yingyu/
    [root@localhost ~]# ll yingyu/
    总用量 0
    -rw-r--r--. 1 liping liping 0 9月 20 14:26 hello
    -rw-r--r--. 1 liping liping 0 9月 20 14:26 hi
    

    1.3.修改文件的属组命令chgrp

    //语法:chgrp [OPTION]... GROUP FILE...
    -R //递归修改

    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping root 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 liping root 29 9月 20 14:26 yingyu
    [root@localhost ~]# chgrp liping a
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 liping root 29 9月 20 14:26 yingyu
    [root@localhost ~]# ll yingyu/
    总用量 0
    -rw-r--r--. 1 liping liping 0 9月 20 14:26 hello
    -rw-r--r--. 1 liping liping 0 9月 20 14:26 hi
    [root@localhost ~]# chgrp -R root yingyu/
    [root@localhost ~]# ll yingyu/
    总用量 0
    -rw-r--r--. 1 liping root 0 9月 20 14:26 hello
    -rw-r--r--. 1 liping root 0 9月 20 14:26 hi
    

    2.遮罩码

    为什么文件创建以后默认权限是644?
    为什么目录创建以后默认权限是755?

    这是由遮罩码umask来控制的。

    从名字就能看出来,遮罩码umask是用来隐藏一些权限的。举例:如果你不想让人家认出你,你会怎么办?

    文件最终的权限为:

    666-umask
    目录最终的权限为:

    777-umask

    [root@localhost ~]# umask 
    0022
    [root@localhost ~]# umask 011
    [root@localhost ~]# umask 
    0011
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    drwxr-xr-x. 2 liping root 29 9月 20 14:26 yingyu
    [root@localhost ~]# touch kk
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    -rw-rw-rw-. 1 root root 0 9月 20 14:51 kk
    drwxr-xr-x. 2 liping root 29 9月 20 14:26 yingyu
    [root@localhost ~]# mkdir yy
    [root@localhost ~]# ll
    总用量 8
    --w-rw-r--. 1 liping liping 78 9月 16 16:36 a
    -rw-------. 1 liping root 1451 9月 6 19:48 anaconda-ks.cfg
    -rw-rw-rw-. 1 root root 0 9月 20 14:51 kk
    drwxr-xr-x. 2 liping root 29 9月 20 14:26 yingyu
    drwxrw-rw-. 2 root root 6 9月 20 14:52 yy
    

    3.Linux安全上下文与特殊权限

    3.1.Linux安全上下文

    前提:进程有属主和属组;文件有属主和属组

    任何一个可执行程序文件能不能启动为进程,取决于发起者对程序文件是否拥有可执行权限;
    启动为进程后,其进程的属主为发起者,属组为发起者所属的组
    进程访问文件时的权限取决于进程的发起者:
    进程的发起者是文件的属主时,则应用文件属主权限
    进程的发起者是文件的属组时,则应用文件属组权限
    应用文件“其它”权限

    3.2.特殊权限

    linux默认权限是根据linux安全上下文的方式来控制的,而特殊权限的存在打破了linux安全上下文的规则。

    SUID(4) //运行程序时,这个程序启动的进程的属主是程序文件自身的属主,而不是启动者为属主
    chmod u+s file
    chmod u-s file
    //如果file本身原来就有执行权限,则SUID显示为s,否则显示为S

    [root@localhost ~]# ll /home/tom
    总用量 892
    -rw-rw-r--. 1 tom root 0 9月 20 15:52 shsjdf
    -rwxr-xr-x. 1 tom tom 910088 9月 20 16:34 vi
    [root@localhost ~]# chmod u+s /home/tom/vi
    [root@localhost ~]# ll /home/tom
    总用量 892
    -rw-rw-r--. 1 tom root 0 9月 20 15:52 shsjdf
    -rwsr-xr-x. 1 tom tom 910088 9月 20 16:34 vi
    
    

    SGID(2) //运行程序时,这个程序启动的进程的属组是程序文件自身的属组,而不是启动者所属的基本组
    //默认情况下,用户创建文件时,其属组为此用户所属的基本组;
    //一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件或目录,其所属的组
    //为此设定了SGID的目录的属组
    chmod g+s DIR
    chmod g-s DIR
    //如果file本身原来就有执行权限,则SGID显示为s,否则显示为S

    [root@localhost ~]# mkdir test
    [root@localhost ~]# ll
    总用量 4
    -rwxrwxrwx. 1 root root 18 9月 20 16:49 abc
    drwxr-xr-x. 2 root root 6 9月 20 16:59 test
    [root@localhost ~]# chown tom.tom test/
    [root@localhost ~]# ll
    总用量 4
    -rwxrwxrwx. 1 root root 18 9月 20 16:49 abc
    drwxr-xr-x. 2 tom tom 6 9月 20 16:59 test
    [root@localhost ~]# cd test/
    [root@localhost test]# touch jj
    [root@localhost test]# mkdir yy
    [root@localhost test]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 9月 20 17:01 jj
    drwxr-xr-x. 2 root root 6 9月 20 17:01 yy
    [root@localhost test]# cd
    [root@localhost ~]# ll
    总用量 4
    -rwxrwxrwx. 1 root root 18 9月 20 16:49 abc
    drwxr-xr-x. 3 tom tom 26 9月 20 17:01 test
    [root@localhost ~]# chmod 2755 test/
    [root@localhost ~]# ll
    总用量 4
    -rwxrwxrwx. 1 root root 18 9月 20 16:49 abc
    drwxr-sr-x. 3 tom tom 26 9月 20 17:01 test
    [root@localhost ~]# cd test/
    [root@localhost test]# touch hh
    [root@localhost test]# mkdir ll
    [root@localhost test]# ll
    总用量 0
    -rw-r--r--. 1 root tom 0 9月 20 17:03 hh
    -rw-r--r--. 1 root root 0 9月 20 17:01 jj
    drwxr-sr-x. 2 root tom 6 9月 20 17:03 ll
    drwxr-xr-x. 2 root root 6 9月 20 17:01 yy
    

    Sticky(1) //在一个公共目录,每个人都能创建文件,删除自己的文件,但是不能删除别人创建的文件
    chmod o+t DIR
    chmod o-t DIR
    //如果DIR本身原来就有执行权限,则Sticky显示为t,否则显示为T

    [root@localhost test]# mkdir /opt/test
    [root@localhost test]# ll /opt/
    总用量 0
    -rw-r--r--. 1 root root 0 9月 7 16:16 abc
    drwxr-xr-x. 2 root root 6 9月 12 22:02 sjnfjd
    drwxr-xr-x. 2 root root 6 9月 12 22:02 snj
    drwxr-xr-x. 2 root root 6 9月 20 17:09 test
    [root@localhost test]# chmod 1777 /opt/test/
    [root@localhost test]# ll /opt/
    总用量 0
    -rw-r--r--. 1 root root 0 9月 7 16:16 abc
    drwxr-xr-x. 2 root root 6 9月 12 22:02 sjnfjd
    drwxr-xr-x. 2 root root 6 9月 12 22:02 snj
    drwxrwxrwt. 2 root root 6 9月 20 17:09 test
    [root@localhost test]# su - tom
    上一次登录:五 9月 20 15:56:34 CST 2019pts/0 上
    [tom@localhost ~]$ cd /opt/test/
    [tom@localhost test]$ ll
    总用量 0
    [tom@localhost test]$ touch abc
    [tom@localhost test]$ ll
    总用量 0
    -rw-r--r--. 1 tom root 0 9月 20 17:13 abc
    [tom@localhost test]$ exit 
    登出
    [root@localhost test]# cd /opt/test/
    [root@localhost test]# ll
    总用量 0
    -rw-r--r--. 1 tom root 0 9月 20 17:13 abc
    [root@localhost test]# touch a
    [root@localhost test]# mkdir b
    [root@localhost test]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 9月 20 17:15 a
    -rw-r--r--. 1 tom root 0 9月 20 17:13 abc
    drwxr-xr-x. 2 root root 6 9月 20 17:15 b
    [root@localhost test]# su - tom
    上一次登录:五 9月 20 17:13:23 CST 2019pts/0 上
    [tom@localhost ~]$ cd /opt/test/
    [tom@localhost test]$ ll
    总用量 0
    -rw-r--r--. 1 root root 0 9月 20 17:15 a
    -rw-r--r--. 1 tom root 0 9月 20 17:13 abc
    drwxr-xr-x. 2 root root 6 9月 20 17:15 b
    [tom@localhost test]$ rm -rf a
    rm: 无法删除"a": 不允许的操作
    [tom@localhost test]$ rm -f abc
    [tom@localhost test]$ ll
    总用量 0
    -rw-r--r--. 1 root root 0 9月 20 17:15 a
    drwxr-xr-x. 2 root root 6 9月 20 17:15 b
    

    4.文件系统访问控制列表facl

    facl(Filesystem Access Control List),利用文件扩展保存额外的访问控制权限。

    //语法:setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
    -m //设定
    u:UID:perm
    g:GID:perm
    //setfacl -m u:test:rw file
    //setfacl -m g:test:rw file
    //如果要为某个目录设定默认的访问控制列表,只需要设定时在u或g前面加上d即可。 如:
    //setfacl -dm u:test:rw file,此时在此目录中创建的文件均继承此访问控制列表所设置的权限

    [root@localhost opt]# touch abc
    [root@localhost opt]# setfacl -m u:tom:rw abc
    [root@localhost opt]# ll
    总用量 0
    -rw-rw-r--+ 1 root root 0 9月 20 17:23 abc
    [root@localhost opt]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    user:tom:rw-
    group::r--
    mask::rw-
    other::r--
    
    [root@localhost opt]# cd
    [root@localhost ~]# su - tom
    上一次登录:五 9月 20 17:15:39 CST 2019pts/0 上
    [tom@localhost ~]$ cd /opt/
    [tom@localhost opt]$ ls
    abc
    [tom@localhost opt]$ ll
    总用量 0
    -rw-rw-r--+ 1 root root 0 9月 20 17:23 abc
    [tom@localhost opt]$ vi abc
    [tom@localhost opt]$ cat abc
    jkj
    
    [root@localhost ~]# id liping
    uid=1004(liping) gid=1004(liping) 组=1004(liping)
    [root@localhost ~]# 
    [root@localhost ~]# cd /opt/
    [root@localhost opt]# ls
    abc
    [root@localhost opt]# setfacl -m g:liping:rw abc
    [root@localhost opt]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    user:tom:rw-
    group::r--
    group:liping:rw-
    mask::rw-
    other::r--
    
    [root@localhost opt]# su - tom
    上一次登录:五 9月 20 17:25:46 CST 2019pts/0 上
    [tom@localhost ~]$ cd /opt/
    [tom@localhost opt]$ vi abc
    [tom@localhost opt]$ cat abc
    jkj
    dhfjd
    djhfj
    

    -x //取消
    u:UID
    g:GID
    //setfacl -x u:test file
    //setfacl -x g:test file
    -b //Remove all

    //语法:getfacl [-aceEsRLPtpndvh] file ...
    //getfacl file

    [root@localhost opt]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    user:tom:rw-
    group::r--
    group:liping:rw-
    mask::rw-
    other::r--
    
    [root@localhost opt]# setfacl -x u:tom abc
    [root@localhost opt]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    group::r--
    group:liping:rw-
    mask::rw-
    other::r--
    
    [root@localhost opt]# setfacl -b abc
    [root@localhost opt]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    group::r--
    other::r--
    

    5.sodo

    sudo可以实现某个用户能够以另外哪一个用户的身份通过哪些主机执行什么命令

    sudo的配置文件:/etc/sudoers

    使用visudo命令进行sudo的配置,每一行就是一个sudo条目,条目格式如下:

    who which_hosts=(runas) command
    who:User_Alias,表示运行命令者的身份
    which_hosts:Host_Alias,通过哪些主机
    runas:Runas_Alias,以哪个用户的身份
    command:Cmnd_Alias,运行哪些命令
    别名必须全部而且只能使用大写英文字母的组合,可以使用感叹号取反

    别名分类:

    用户别名:
    User_Alias NETWORKADMIN =
    用户的用户名
    组名,使用%引导
    还可以其它已经定义的用户别名
    主机别名:
    Host_Alias =
    主机名
    IP地址
    网络地址
    其它主机别名
    Runas别名:
    Runas_Alias =
    用户名
    %组名
    其它的Runas别名
    命令别名:
    Cmnd_Alias =
    命令路径
    目录(此目录内的所有命令)
    其它已定义的命令别名
    //sudo命令语法:sudo [options] COMMAND
    -V //显示版本编号
    -h //会显示版本编号及指令的使用方式说明
    -l //列出当前用户可以使用的所有sudo类命令
    -v //因为sudo在第一次执行时或是在N分钟内没有执行(N默认为5)会问密码,这个参数
    //是重新做一次确认,如果超过N分钟,也会问密码
    -k //让认证信息失效,如果不指定-k,默认认证信息在5分钟后失效
    -b //将要执行的指令放在后台执行
    -u USERNAME //以指定的用户名执行命令,默认为root

    6.管理命令

    w //显示当前登录到系统的用户有哪些,以及其正在做什么
    sleep //睡眠,写脚本为防止上一个命令没执行完下一命令就开始执行时可以加上sleep #
    //表示停顿#秒后再执行后面的命令
    sleep NUMBER[SUFFIX]...
    SUFFIX:
    s:秒,默认
    m:分
    h:小时
    d:天
    last //显示/var/log/wtmp文件,显示用户登录历史及系统重启历史
    -n # //显示最近#次的相关信息
    lastb //显示/var/log/btmp文件,显示用户错误的登录尝试
    -n # //显示最近#次的相关信息
    lastlog //显示每个用户最近一次成功登录信息
    -u username //显示特定用户最近的登录信息
    basename //显示路径基名

    [root@localhost opt]# w
     18:02:01 up 1:32, 2 users, load average: 0.00, 0.03, 0.05
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    root pts/0 192.168.124.1 16:29 1.00s 0.21s 0.00s w
    root pts/1 192.168.124.1 16:36 1:07m 0.05s 0.05s -bash
    [root@localhost opt]# last -3
    root pts/1 192.168.124.1 Fri Sep 20 16:36 still logged in   
    root pts/0 192.168.124.1 Fri Sep 20 16:29 still logged in   
    reboot system boot 3.10.0-693.el7.x Fri Sep 20 16:29 - 18:02 (01:33)    
    
    wtmp begins Fri Sep 6 19:49:56 2019
    [root@localhost opt]# lastb -2
    tom pts/1 Fri Sep 20 15:51 - 15:51 (00:00)    
    tom pts/1 Fri Sep 20 15:51 - 15:51 (00:00)    
    
    btmp begins Sat Sep 7 16:37:31 2019
    [root@localhost opt]# lastlog -u tom
    用户名 端口 来自 最后登陆时间
    tom pts/0 五 9月 20 17:58:28 +0800 2019
    
  • 相关阅读:
    《Mathematical Olympiad——组合数学》——染色问题
    《啊哈算法》——栈、队列、链表
    《Mathematical Olympiad——组合数学》——抽屉原理
    2749: [HAOI2012]外星人
    bzoj4241: 历史研究
    bzoj3210: 花神的浇花集会
    bzoj4998: 星球联盟
    bzoj2728: [HNOI2012]与非
    bzoj2669: [cqoi2012]局部极小值
    bzoj5441: [Ceoi2018]Cloud computing
  • 原文地址:https://www.cnblogs.com/liping0826/p/11558958.html
Copyright © 2011-2022 走看看