天天看点

Linux笔记4-用户身份和文件权限用户身份和能力文件权限与归属文件特殊权限文件隐藏属性su命令与sudo服务文件访问控制列表

文章目录

  • 用户身份和能力
  • 文件权限与归属
  • 文件特殊权限
  • 文件隐藏属性
  • su命令与sudo服务
    • su命令
    • sudo命令
  • 文件访问控制列表

用户身份和能力

root用户拥有极高的权限,能够管理各项功能,如:添加/删除用户、启动/关闭进程等。

其实root 只是个名字,真正让他成为超级用户的是他的UID值。

UID(USER ID):每个用户都有对应的DUI值,就像身份证号码。

超级用户UID0的用户,默认是root 用户

系统用户UID1-999:系统中系统服务由不同的用户运行,更加安全,默认被限制登录西戎

普通用户UID1000~ :由管理员创建的用于日常工作而不能管理系统的普通用户

账号名称和UID保存在/etc/passwd 文件中,而账户密码则保存在 /etc/shadow 文件中

GID(GROUP ID):可以将多个用户归到一个组中,方便指派热恩物或工作。用户组名称与GID保存在 /etc/group中

文件权限与归属

linux 中一些都是文件,文件和目录的归属和权限 分别规定文件的所有者、所有人、其余人的读、写、执行权限。

Linux笔记4-用户身份和文件权限用户身份和能力文件权限与归属文件特殊权限文件隐藏属性su命令与sudo服务文件访问控制列表

如下示例中 表示 index.html 是一个文件 所有者权限:读写 所属组权限:只读 其他用户权限:只读

第一位意义:-: 普通文件 d:目录文件 l:链接文件 b:块设备文件 c:字符设备文件 p:管道文件

普通文件的执行权限是说文件可以被执行,目录文件的执行权限表示用户可以进入到目录中

-rw-r--r--  1 root root 2381 Oct 19 10:29 index.html
           

文件特殊权限

SUID:让执行者临时拥有属主的权限(仅对拥有执行权限的二进制程序有效)

SGID: 1)让执行者临时拥有属组的权限(只对可执行文件生效)

2)在该目录中创建的文件自动集成此目录的用户组(只对目录文件生效)

chmod 命令用于修改文件或目录的权限 格式: chmod [参数] 权限 文件或目录名

chown 命令用于修改文件或目录所属的用户和分组 格式:chown [参数] 所属用户:所属组 文件或目录名

示例

用root用户创建目录 ttdir, 增加执行权限(只有有执行权限的文件或目录才可以 添加 ssui 和 sgid权限)

然后再添加 sgid 权限 后 切换到hadoop 用户可以正常访问编辑该目录。即用户hadoop临时拥有了 所属组的权限

[[email protected] tmpdir]# mkdir ttdir
[[email protected] tmpdir]# ls -ald ttdir
drwxr-xr-x 2 root root 6 Oct 27 04:40 ttdir
[[email protected] tmpdir]# ls -ald ttdir
drwxrwxrwx 2 root root 6 Oct 27 04:40 ttdir
[[email protected] tmpdir]# chmod -rf g+s ttdir
chmod: invalid mode: ‘-rf’
Try 'chmod --help' for more information.
[[email protected] tmpdir]# chmod -R g+s ttdir
[[email protected] tmpdir]# ls -ald ttdir
drwxrwsrwx 2 root root 6 Oct 27 04:40 ttdir
[[email protected] tmpdir]# su hadoop
[[email protected] tmpdir]$ cd ttdir
[[email protected] ttdir]$ ll
total 0
[[email protected] ttdir]$ echo “hello ” > test
[[email protected] ttdir]$ ll
total 4
-rw-r--r-- 1 hadoop root 13 Oct 27 04:54 test
           

SBIT(Stick bit) :只可管理自己数据而不能删除他人文件(仅对目录有效)

示例,root用户进入/tmp 目录 ,权限中最后一位 t 表示 该目录设置了SBIT权限(粘滞位),创建 文件test.txt 并对所有用户开发读写权限,然后切换 普通用户hadoop ,去删除该文件,提示删除失败。(虽然有权限单不允许删除别人的文件)

[[email protected] tmp]# ls -adl /tmp
drwxrwxrwt. 12 root root 4096 Oct 27 03:41 /tmp
[[email protected] tmp]# echo 123>test.txt
[[email protected] tmp]# ll
-rw-r--r-- 1 root root  0 Oct 27 05:06 test.txt
[[email protected] tmp]# chmod 777 test.txt
[[email protected] tmp]# su hadoop
[[email protected] tmp]$ ll
-rwxrwxrwx 1 root root  0 Oct 27 05:06 test.txt
[[email protected] tmp]$ rm test.txt
rm: cannot remove ‘test.txt’: Operation not permitted
           

文件隐藏属性

文件权限除了读写执行和SUID、SGID、SBIT 之外还有隐藏权限。例如只能为某个文件追加内容不能减少内容。

chattr命令

用于设置文件的隐藏权限 格式:chattr [参数] 文件

参数:

i    无法对文件进行修改,若对目录设置后则只能修改子文件不能新建或删除文件
a    仅允许补充内容,无法删除 覆盖
...
           

lsattr命令

用于显示文件的隐藏权限,格式:lsattr[参数] 文件

参数:

a     显示所有文件和目录
l       显示隐藏属性的全称
R     递归处理
d     如果目标文件为目录 则需要加此参数
           

示例

创建 文件555.txt 没有增加特殊权限之前可以成功删除。

重新创建 文件 555.txt 增加 特殊权限a 后,只能追加文件内容,无法覆盖和删除文件

[[email protected] ~]# echo '555'>555.txt
[[email protected] ~]# rm 555.txt
rm: remove regular file ‘555.txt’? y
[[email protected] ~]# echo '555' >555.txt
[[email protected] ~]# chattr +a 555.txt
[[email protected] ~]# echo '5555'>>555.txt
[[email protected] ~]# echo '5555'>555.txt
-bash: 555.txt: Operation not permitted
[[email protected] ~]# rm 555.txt
rm: remove regular file ‘555.txt’? y
rm: cannot remove ‘555.txt’: Operation not permitted
           

su命令与sudo服务

su命令

用于变更使用者的身份(切换登录用户) 格式:su [-] 用户名 加参数 - 可以同时切换环境变量,不加仅切换用户

root用户切换到其他用户时无需输入密码

通过示例发现 如果加了 参数- 那么会自动切换到 用户的 根目录 下,环境变量也会同时切换,否则 目录和环境变量不会切换。同时 加了参数 执行exit退出时 提示 logout 否则 只提示 exit.

[[email protected] ~]# su hadoop
[[email protected] root]$ su root
Password: 
[[email protected] ~]# su - hadoop
Last login: Tue Oct 27 07:51:53 EDT 2020 on pts/1
[[email protected] ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/apps/jdk/bin:/home/hadoop/.local/bin:/home/hadoop/bin
[[email protected] ~]$ su root
Password: 
[[email protected] hadoop]# su hadoop
[[email protected] ~]$ su -  root
Password: 
Last login: Tue Oct 27 07:53:30 EDT 2020 on pts/1
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/apps/jdk/bin:/root/bin
[[email protected] ~]# su hadoop
[[email protected] root]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/apps/jdk/bin:/root/bin
[[email protected] root]$ 
[[email protected] ~]# su hadoop
[[email protected] root]$ exit
exit
[[email protected] ~]# su - hadoop
Last login: Tue Oct 27 07:59:17 EDT 2020 on pts/1
[[email protected] ~]$ exit
logout
           

sudo命令

用于给普通用户提供额外权限完成原本超级用户才能外城的任务,格式为:sudo [参数] 命令名称

su命令可以将登陆用户切换成root,这样会增加隐患。使用sudo 可以只将文件执行的权限给普通用户,增加安全性。在保证普通用户完成工作的前提下尽可能的减少权限。

sudo 具体功能:

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

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

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

4、验证过密码后5分钟(默认)内无需再次验证密码,更加方便

参数

-h  列出帮助信息
-l  列出前用户可执行命令
-u  用户性或uid      以指定的用户身份执行命令
-k   清空安全时间,下次执行sudo时 必须要再次验证密码
-b   在后台执行指定的命令
-p   更改询问密码提示语
           

visudo命令

只有超级用户才可以使用visudo命令编辑 /etc/sudoers文件,该命令可以避免多人同时修改,同时会对文件内容进行语法检查,如果检查不通过无法保存。

示例

允许hadoop用户通过sudo执行所有命令

通过visudo 命令编辑编辑文件,99行后增加 hadoop权限配置如下

root ALL=(ALL) ALL

hadoop ALL=(ALL) ALL

切换到hadoop用户可以查看 通过sudo 可以执行的命令。直接 访问 /root 目录无法访问,通过sudo可以正常访问。

[[email protected] ~]# su - hadoop
Last login: Tue Oct 27 08:20:08 EDT 2020 on pts/1
[[email protected] ~]$ sudo -l

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for hadoop: 
Sorry, try again.
[sudo] password for hadoop: 
Matching Defaults entries for hadoop on bogon:
    !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS
    DISPLAY HOSTNAME HISTSIZE 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 hadoop may run the following commands on bogon:
    (ALL) ALL
[[email protected] ~]$ ls /root
ls: cannot open directory /root: Permission denied
[[email protected] ~]$ sudo ls /root
555.txt          date.txt    res.txt      test       testdd.txt  tmpdir         tmpfile  ttt.txt
anaconda-ks.cfg  index.html  root.tar.gz  test2.txt  test.txt    tmpdir.tar.gz  tstd
           

同理可以配置

hadoop ALL=(ALL) ALL //表示 hadoop 用户可以以所有用户的身份执行所有命令

hadoop ALL=(root) /bin/cat //表示 hadoop 用户可以以root用户的身份执行/bin/cat命令

hadoop ALL=NOPASSWD:ALL //表示hadoop 用户可以以任意身份执行命令 且不需要密码验证

文件访问控制列表

上述rwx权限控制都是对某一类人的权限控制,如果需要对指定用户进行权限控制,需要使用文件的访问控制列表来实现。

可以基于文件或目录设置ACL(指定用户或用户组的访问权限),如果对某个目录设置了控制策略,那么他的子文件,默认继承该访问策略(除非进行特殊设置)

setfacl 命令

用于增加或者修改acl规则,格式:setfacl [参数] 文件

-r       递归(对目录使用)
-m     设置文件的acl规则
-b      删除acl规则
           

getfacl 命令

用于显示文件的acl规则,格式:getfacl 文件

示例

root 目录正常 hadoop 用户无法访问,这里为hadoop 增加 root 目录访问权限,然后切换到hadoop用户,可以访问该目录

[[email protected] ~]# setfacl -rm u:hadoop:rwx /root
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
Try `setfacl --help' for more information.
[[email protected] ~]# setfacl -Rm u:hadoop:rwx /root
setfacl: /root/ttt.txt: Operation not permitted
setfacl: /root/555.txt: Operation not permitted
[[email protected] ~]# getfacl /root
getfacl: Removing leading '/' from absolute path names
# file: root
# owner: root
# group: root
user::r-x
user:hadoop:rwx
group::r-x
mask::rwx
other::---
[[email protected] ~]# su - hadoop
Last login: Tue Oct 27 08:23:45 EDT 2020 on pts/1
[[email protected] ~]$ cd /root
[[email protected] root]$ ll
total 52
-rw-r--r--  1 root root    9 Oct 27 07:43 555.txt
-rw-rwx---+ 1 root root 1260 Jun 10 05:26 anaconda-ks.cfg
-rw-rwxr--+ 1 root root 1595 Oct 26 04:59 date.txt
-rw-rwxr--+ 1 root root 2381 Oct 19 10:29 index.html
-rw-rwxr--+ 1 root root  767 Oct 19 09:51 res.txt
           

继续阅读