文件权限

三种权限

一般权限(rwx)

特殊权限(suid、sgid、sbit)

隐藏权限

rwx 421 一般权限

1 2
ls -l install.log -rw-r--r-- 1 root root 34298 04-02 00:23 install.log

文件类型、访问权限、所有者、所属组、占用磁盘大小、修改时间、文件名称

suid 所有者特殊权限

执行者临时拥有所有者权限,仅对拥有执行权限的二进制程序有效,尚方宝剑

rwx rws

rw- rwS

1 2 3 4 5
ls -l /etc/shadow ----------. 1 root root 1004 Jan 3 06:23 /etc/shadow ls -l /bin/passwd -rwsr-xr-x. 1 root root 27832 Jan 29 2017 /bin/passwd

sgid 属组特殊权限

1、执行者临时拥有属组权限(对拥有执行权限的二进制程序进行设置)

2、在某个目录中创建的文件,自动继承该目录的用户组(只可以对目录进行设置)

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
-r-xr-sr-x 1 bin system 59346 Feb 11 2017 ps cd /tmp mkdir testdir ls -ald testdir/ drwxr-xr-x. 2 root root 6 Feb 11 11:50 testdir/ chmod -Rf 777 testdir/ chmod -Rf g+s testdir/ ls -ald testdir/ drwxrwsrwx. 2 root root 6 Feb 11 11:50 testdir/ su - linuxprobe Last login: Wed Feb 11 11:49:16 CST 2017 on pts/0 cd /tmp/testdir/ echo "linuxprobe.com" > test ls -al test -rw-rw-r--. 1 linuxprobe root 15 Feb 11 11:50 test

sbit 粘滞特殊权限

Sticky Bit,粘滞位,保护位

目录中文件,只能被所有者删除,/tmp默认设置

rwx rwt

rw- rwT

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
su - linuxprobe Last login: Wed Feb 11 12:41:20 CST 2017 on pts/0 ls -ald /tmp drwxrwxrwt. 17 root root 4096 Feb 11 13:03 /tmp cd /tmp ls -ald drwxrwxrwt. 17 root root 4096 Feb 11 13:03 . echo "Welcome to linuxprobe.com" > test chmod 777 test ls -al test -rwxrwxrwx. 1 linuxprobe linuxprobe 10 Feb 11 12:59 test su - blackshield Last login: Wed Feb 11 12:41:29 CST 2017 on pts/1 cd /tmp rm -f test rm: cannot remove ‘test’: Operation not permitted exit Logout cd ~ mkdir linux chmod -R o+t linux/ ls -ld linux/ drwxr-xr-t. 2 root root 6 Feb 11 19:34 linux/

chattr 隐藏权限

chattr命令参数

参数作用
i无法对文件进行修改;若对目录设置了该参数,则仅能修改其中的子文件内容而不能新建或删除文件
a仅允许补充(追加)内容,无法覆盖/删除内容(Append_Only)
S文件内容在变更后立即同步到硬盘(sync)
s彻底从硬盘中删除,不可恢复(用0填充原文件所在硬盘区域)
A不再修改这个文件或目录的最后访问时间(atime)
b不再修改文件或目录的存取时间
D检查压缩文件中的错误
d使用dump命令备份时忽略本文件/目录
c默认将文件或目录进行压缩
u当删除该文件后依然保留其在硬盘中的数据,方便日后恢复
t让文件系统支持尾部合并(tail-merging)
X可以直接访问压缩文件中的内容

 1  2  3  4  5  6  7  8  9 10 11 12
echo "for Test" > linuxprobe rm linuxprobe rm: remove regular file ‘linuxprobe’? y echo "for Test" > linuxprobe #+a 不允许删除与覆盖 chattr +a linuxprobe rm linuxprobe rm: remove regular file ‘linuxprobe’? y rm: cannot remove ‘linuxprobe’: Operation not permitted

lsattr 显示隐藏权限

 1  2  3  4  5  6  7  8  9 10 11 12
ls -al linuxprobe -rw-r--r--. 1 root root 9 Feb 12 11:42 linuxprobe lsattr linuxprobe -----a---------- linuxprobe chattr -a linuxprobe lsattr linuxprobe ---------------- linuxprobe rm linuxprobe rm: remove regular file ‘linuxprobe’? y

ACL 访问控制列表

针对指定的用户、用户组,设置文件、目录的操作权限

1 2 3 4 5 6
su - linuxprobe Last login: Sat Mar 21 16:31:19 CST 2017 on pts/0 cd /root -bash: cd: /root: Permission denied exit

setfacl 设置ACL

-R 针对目录文件

-m 针对普通文件

-b 删除某个文件的ACL

 1  2  3  4  5  6  7  8  9 10 11 12 13 14
setfacl -Rm u:linuxprobe:rwx /root su - linuxprobe Last login: Sat Mar 21 15:45:03 CST 2017 on pts/1 cd /root ls anaconda-ks.cfg Downloads Pictures Public cat anaconda-ks.cfg exit #.变成+,已设置ACL ls -ld /root dr-xrwx---+ 14 root root 4096 May 4 2017 /root

getfacl 显示ACL

 1  2  3  4  5  6  7  8  9 10
getfacl /root getfacl: Removing leading '/' from absolute path names # file: root # owner: root # group: root user::r-x user:linuxprobe:rwx group::r-x mask::rwx other::---

su 切换用户

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
id uid=0(root) gid=0(root) groups=0(root) su - linuxprobe Last login: Wed Jan 4 01:17:25 EST 2017 on pts/0 id uid=1000(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 su root Password: su - linuxprobe Last login: Mon Aug 24 19:27:09 CST 2017 on pts/0 exit logout

sudo

把特定命令的执行权限赋予给指定用户

1、限制用户执行指定的命令

2、记录用户执行的每一条命令

3、配置文件(/etc/sudoers)提供集中的用户管理、权限与主机等参数

4、验证密码的后5分钟内(默认值)无须再让用户再次验证密码。

sudo服务参数

参数作用
-h列出帮助信息
-l列出当前用户可执行的命令
-u用户名或UID值以指定的用户身份执行命令
-k清空密码的有效时间,下次执行sudo时需要再次进行密码验证
-b在后台执行指定的命令
-p更改询问密码的提示语

visudo

root权限才可以使用visudo编辑sudo服务的配置文件

1 2 3 4 5 6
visudo: >>> /etc/sudoers: syntax error near line 111 <<< What now? Options are: (e)dit sudoers file again (x)it without saving changes to sudoers file (Q)uit and save changes to sudoers file (DANGER!)

谁可以使用 允许使用的主机=(以谁的身份) 可执行命令的列表

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
visudo 96 ## 97 ## Allow root to run any commands anywhere 98 root ALL=(ALL) ALL 99 linuxprobe ALL=(ALL) ALL su - linuxprobe Last login: Thu Sep 3 15:12:57 CST 2017 on pts/1 sudo -l [sudo] password for linuxprobe:此处输入linuxprobe用户的密码 Matching Defaults entries for linuxprobe on this host: requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin User linuxprobe may run the following commands on this host: (ALL) ALL ls /root ls: cannot open directory /root: Permission denied sudo ls /root anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Desktop Downloads Music Public Videos exit logout whereis cat cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz visudo 96 ## 97 ## Allow root to run any commands anywhere 98 root ALL=(ALL) ALL 99 linuxprobe ALL=(ALL) /usr/bin/cat su - linuxprobe Last login: Thu Sep 3 15:51:01 CST 2017 on pts/1 cat /etc/shadow cat: /etc/shadow: Permission denied sudo cat /etc/shadow root:$6$GV3UVtX4ZGg6ygA6$J9pBuPGUSgZslj83jyoI7ThJla9ZAULku3BcncAYF00Uwk6Sqc4E36 MnD1hLtlG9QadCpQCNVJs/5awHd0/pi1:16626:0:99999:7::: bin:*:16141:0:99999:7::: daemon:*:16141:0:99999:7::: adm:*:16141:0:99999:7::: lp:*:16141:0:99999:7::: sync:*:16141:0:99999:7::: shutdown:*:16141:0:99999:7::: halt:*:16141:0:99999:7::: mail:*:16141:0:99999:7::: operator:*:16141:0:99999:7::: games:*:16141:0:99999:7::: ftp:*:16141:0:99999:7::: nobody:*:16141:0:99999:7::: ………………省略部分文件内容……………… exit logout whereis poweroff poweroff: /usr/sbin/poweroff /usr/share/man/man8/poweroff.8.gz visudo ………………省略部分文件内容……………… 96 ## 97 ## Allow root to run any commands anywhere 98 root ALL=(ALL) ALL 99 linuxprobe ALL=NOPASSWD: /usr/sbin/poweroff ………………省略部分文件内容……………… su - linuxprobe Last login: Thu Sep 3 15:58:31 CST 2017 on pts/1 poweroff User root is logged in on seat0. Please retry operation after closing inhibitors and logging out other users. Alternatively, ignore inhibitors and users with 'systemctl poweroff -i'. sudo poweroff

浙ICP备11005866号-12