前面文章分享了Linux下经常使用命令以及Shell编程相关知识,本节继续学习Linux用户管理及文件权限控制。linux
Linux是多用户多任务操做系统,具备很好的稳定性和安全性。既然是多用户,那就意味着多个用户能够同时使用同一个Linux操做系统,所以就会涉及用户的添加、修改、删除等管理工做以及权限分配问题;平时使用Linux系统通常是用于信息处理,而文件是信息载体,所以也须要掌握文件相关的操做和权限。算法
相信你们平时在使用windows操做系统时,为了避免让别人轻易看到某些敏感文件,而把文件设置为隐藏文件,Linux下是否也能实现一样的操做?是否能经过隐藏权限让黑客最多只能查看某些日志文件但不能进行修改和删除操做?是否可以对某个用户或某个用户组进行特殊的权限设置,让其只有能知足工做需求的最小权限,从而下降安全风险?本篇文章将逐一解决这些疑问。shell
针对初学者,在平常工做中,通常都是领导分配一个拥有必定权限的帐号,而后开展各项工做,不会开放很高的权限。但初学阶段,正如前面系列文章所演示,都是直接用root帐户进行操做,这样的目的是减小权限带来的干扰,让咱们更专一于相应知识点的学习。可是在生产环境中建议慎用root,由于权限太大,控制不当会有安全隐患。编程
who命令用于查看登陆用户信息,包括:who、whoami、who am i。windows
功能描述:查看当前登陆用户的用户名安全
案例:bash
[root@heimatengyun test]# whoami
root
[root@heimatengyun test]# su - test
Last login: Sat Nov 30 22:55:38 CST 2019 on pts/0
[test@heimatengyun ~]$ whoami
test
[test@heimatengyun ~]$ exit
logout
复制代码
能够看到,切换用户后,相应的结果发生变化,只显示当前登陆的用户。服务器
功能描述:显示最初登陆时用的用户名(不管切换几回)编辑器
案例:ide
[root@heimatengyun test]# who am i
root pts/0 2019-12-17 22:23 (192.168.78.1)
[root@heimatengyun test]# su - test
Last login: Tue Dec 17 22:31:09 CST 2019 on pts/0
[test@heimatengyun ~]$ who am i
root pts/0 2019-12-17 22:23 (192.168.78.1)
[test@heimatengyun ~]$ exit
logout
复制代码
能够看到,切换后用户名仍是显示最开始登陆时的用户名称。
功能描述:显示当前有哪些用户真正登陆到了本台机器(不会显示那些用su命令切换的用户)
案例:
[root@heimatengyun test]# who
(unknown) :0 2019-12-17 22:22 (:0)
root pts/0 2019-12-17 22:23 (192.168.78.1)
[root@heimatengyun test]# su - test
Last login: Tue Dec 17 22:34:44 CST 2019 on pts/0
[test@heimatengyun ~]$ who
(unknown) :0 2019-12-17 22:22 (:0)
root pts/0 2019-12-17 22:23 (192.168.78.1)
[test@heimatengyun ~]$ exit
logout
复制代码
能够看到用su命令切换用户后,显示结果中并咩有test用户,所以显示的知识真正登陆到本机的全部用户。
语法:id 用户名
功能描述:判断用户是否存在
案例:
[root@heimatengyun test]# id test
uid=1000(test) gid=1000(test) groups=1000(test)
[root@heimatengyun test]# id lover
id: lover: no such user
复制代码
若是用户存在返回用户信息,若是用户不存在则提示no such user
语法:
useradd [选项] 用户名
功能描述:
添加新用户,默认的用户家目录存放在/home目录中,默认的Shell解释器为 /bin/bash,同时会默认建立一个与该用户同名的基本用户组。在建立用户时,经过如下参数能够修改默认设置。
选项:
参数 | 做用 |
---|---|
-d | home-dir,指定用户的家目录,默认为/home/username |
-e | expiredate,帐户到期时间,格式:YYYY-MM-DD |
-u | uid,指定用户默认的UID |
-g | gid,指定初始用户基本组,组必须已存在 |
-G | groups,指定一个或多个扩展用户组 |
-N | no-user-group,不建立与用户同名的基本用户组 |
-s | shell,指定用户默认的Shell解释器 |
案例:
(1)采用默认参数建立用户
[root@heimatengyun test]# id lover
id: lover: no such user
[root@heimatengyun test]# useradd lover
[root@heimatengyun test]# id lover
uid=1001(lover) gid=1001(lover) groups=1001(lover)
[root@heimatengyun ~]# cat /etc/passwd
...省略部份内容
lover:x:1001:1001::/home/lover:/bin/bash
复制代码
建立的用户保存在/etc/passwd文件中,能够经过此文件查看用户信息。
一行为一条用户记录,分为7个字段,每一个字段用冒号分隔。每一个字段分别对应:
用户名:密码:UID:GID:注释:家目录:伪用户
(2)建立用户指定家目录、UID以及Shell解释器
[root@heimatengyun ~]# useradd -d /home/heima -u 9988 -s /sbin/nologin heimage
[root@heimatengyun ~]# id heimage
uid=9988(heimage) gid=9988(heimage) groups=9988(heimage)
[root@heimatengyun ~]# ls /home/
heima test
复制代码
/sbin/nologin是终端解释器中的一员,可是与Bash解释器不一样,被设置为nologin后,用户将不能登陆到系统中。
RHEL7(Centos7)系统中,用户身份有3种:管理员、系统用户、普通用户。系统的管理员用户UID为0;系统用户UID 为 1~999, Linux 系统为了不因某个服务程序出现漏洞而被黑客提 权至整台服务器,默认服务程序会有独立的系统用户负责运行,进而有效控制被破坏 范围;普通用户 UID 从 1000 开始,是由管理员建立的用于平常工做的用户。
须要注意的是,UID 是不能冲突的,并且管理员建立的普通用户的 UID 默认是从 1000 开始的(即便前面有闲置的号码)。
另外,在 Linux 系统中建立每一个用户时,将自动建立一个与其同名的基本用户组,并且 这个基本用户组只有该用户一我的。若是该用户之后被概括入其余用户组,则这个其余用户 组称之为扩展用户组。一个用户只有一个基本用户组,可是能够有多个扩展用户组,从而满 足平常的工做须要。
语法:
passwd [选项] 用户名
功能描述:
设置或修改用户密码、过时时间、认证信息等。
选项:
参数 | 做用 |
---|---|
-l | lock,锁定用户,禁止登陆 |
-u | unlock,解除锁定,容许用户登陆 |
-e | expire,强制用户在下次登陆时修改密码 |
-S | status,显示yoghurt的密码是否被锁定,以及密码采用的加密算法名称 |
案例:
(1)修改其余帐户密码
[root@heimatengyun test]# passwd lover
Changing password for user lover.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
复制代码
修改的密码不能太简单,不然修改不成功。修改为功后,便可使用帐户进行登陆。在界面中登陆后,便可查看当前登陆用户
[root@heimatengyun test]# who
lover :0 2019-12-17 23:05 (:0)
root pts/0 2019-12-17 22:23 (192.168.78.1)
复制代码
新建一个用户,若是没有设置密码,则用户没法直接登陆,只能经过root用户使用su命令切换。所以,通常新建用户就会同时设置密码。也就是说useradd和passed命令通常是一块儿使用。
不管是普通用户仍是超级权限用户均可以运行passwd命令,可是若是是普通用户则只能修改本身的密码。配合选项参数能够实现更丰富的功能,具体用法能够经过man命令进行查看。
(2)锁定及解锁帐户
假设你部门有一位同事要休假半年,那么能够经过-l参数锁定用户,禁止其登陆,等休假完毕回来上班后再使用-u参数将其解锁。这样避免了删除用户、添加用户带来的麻烦同时也保证了这段时间内系统的安全。
[root@heimatengyun ~]# passwd -S test
test PS 2019-11-27 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@heimatengyun ~]# passwd -l test
Locking password for user test.
passwd: Success
[root@heimatengyun ~]# passwd -S test
test LK 2019-11-27 0 99999 7 -1 (Password locked.)
[root@heimatengyun ~]# passwd -u test
Unlocking password for user test.
passwd: Success
[root@heimatengyun ~]# passwd -S test
test PS 2019-11-27 0 99999 7 -1 (Password set, SHA512 crypt.)
复制代码
语法:
usermod [选项] 用户名
功能描述:
修改用户信息
Linux系统一切皆文件,修改用户也就是修改配置文件。用户信息保存在/etc/passwd文件中,能够直接采用文本编辑器修改也能够经过usermod命令进行修改。
选项:
参数 | 做用 |
---|---|
-e | expiredate,帐号到期时间,格式为YYYY-MM-DD |
-g | gid,变动所属用户组 |
-G | groups,变动扩展用户组 |
-L | lock,锁定用户禁止其登陆 |
-U | unlock,解锁用户,容许其登陆 |
-u | uid,修改用户的UID |
案例:
经过-G参数将上边建立的lover用户加入到root用户组
[root@heimatengyun test]# id lover
uid=1001(lover) gid=1001(lover) groups=1001(lover)
[root@heimatengyun test]# usermod -G root lover
[root@heimatengyun test]# id lover
uid=1001(lover) gid=1001(lover) groups=1001(lover),0(root)
复制代码
语法:
userdel [选项] 用户名
功能描述:
当用户不会再登陆系统,则使用此命令删除用户
选项:
参数 | 做用 |
---|---|
-f | force,强制删除 |
-r | remove,删除用户及用户家目录 |
案例:
删除以前建立的lover用户,并删除目录
[root@heimatengyun home]# pwd
/home
[root@heimatengyun home]# ls
lover test
[root@heimatengyun home]# userdel -r lover
userdel: user lover is currently used by process 4419
[root@heimatengyun home]# userdel -rf lover
[root@heimatengyun home]# ls
test
复制代码
删除用户时默认会保留用户主目录,添加-r参数则会删除home目录下用户主目录。-f表示强制删除,因为前文经过界面上登陆了lover用户,因此提示有进程在使用,经过-f强制删除用户。
语法:
groupadd [选项] 组名
功能描述:
添加用户组,有时候为了高效的管理系统中各个用户的权限,常常会将多个用户添加到一个指定组中。
案例:
添加heima用户组并查看组信息
[root@heimatengyun ~]# groupadd heima
[root@heimatengyun ~]# cat /etc/group
...省略部份内容
test:x:1000:test
heima:x:1001:
复制代码
/etc/group文件包含全部组信息,能够查看到刚才添加的heima用户组。
/etc/group文件每行表示一条记录,标识一个用户组。每条记录分为四个字段,用冒号分割。第一字段:用户组名称;第二字段:用户组密码;第三字段:GID;第四字段:用户列表,每一个用户之间用逗号分隔,本字段能够为空
语法:
groupmod [选项] 新组名 老组名
功能描述:
选项:
参数 | 做用 |
---|---|
-n | 修改组名称 |
案例:
(1)修改heima组名称heimage
[root@heimatengyun ~]# groupmod -n heimage heima
[root@heimatengyun ~]# cat /etc/group
...省略部份内容
test:x:1000:test
heimage:x:1001:
复制代码
(2)新建并添加用户到heimage组
[root@heimatengyun ~]# cat /etc/group
...省略部份内容
test:x:1000:test
heimage:x:1001:
[root@heimatengyun ~]# useradd -g 1001 heimagege
[root@heimatengyun ~]# id heimagege
uid=1001(heimagege) gid=1001(heimage) groups=1001(heimage)
[root@heimatengyun ~]# cat /etc/group
...省略部份内容
test:x:1000:test
heimage:x:1001:
[root@heimatengyun ~]# cat /etc/passwd
...省略部份内容
heimagege:x:1001:1001::/home/heimagege:/bin/bash
复制代码
语法:
groupdel 组名
功能描述:
删除组,前提是组内没有用户才能删除
案例:
(1)删除用户组
[root@heimatengyun ~]# groupdel heimage
groupdel: cannot remove the primary group of user 'heimagege'
[root@heimatengyun ~]# userdel heimagege
[root@heimatengyun ~]# groupdel heimage
复制代码
(2)查看组内用户
如案例1所示,若是组内有用户则没法直接删除组。能够经过/etc/group文件匹配对应的组名查看对应组内有哪些用户
[root@heimatengyun ~]# grep 'test' /etc/group
test:x:1000:test
复制代码
查找以前建的test组内有哪些用户,第四个字段即为该组内全部用户列表。
Linux中一切皆文件,可是每一个文件类型可能不一样,如何区分文件类型呢?每一个文件都有全部者和全部组以及其余人对文件拥有的读、写、执行权限,如何查看文件的这些权限呢?
固然是经过ls或ll命令就能够查看
[root@heimatengyun test]# ll
-rw-r--r--. 1 root root 9 Nov 30 20:43 test1.txt
drwxr-xr-x. 2 root root 6 Dec 20 11:32 test1
复制代码
ll命令显示结果详解
以test1.txt文件为例,各部分表明的含义依次为以下
符号 | 文件类型 |
---|---|
- | 普通文件 |
d | 目录文件 |
l | 链接文件 |
b | 块设备文件 |
c | 字符设备文件 |
p | 管理文件 |
s | 套接字文件 |
文件权限:
“rw-r--r--.”,能够分为四段,前三段每三位为一段,最后一个点单独为一段。第一段rw-表示文件建立者/全部者对该文件所具备的权限,第二段r--表示建立者/全部者所在的组的其余用户所具备的权限,第三段r--表示其余组的其余用户所具备的权限。第四段.
字符 | 权限类型 |
---|---|
r | read,读取权限,数字表示为4。对文件而言,具备读取文件内容的权限;对目录来讲,具备浏览目录的权限 |
w | write,写入权限,数字表示为2。对文件而言,具备新增、修改文件内容的权限;对目录来讲,具备删除、移动目录内文件的权限 |
x | execute,执行权限,数字表示为1。对文件而言,具备执行文件的权限;对目录来讲,该用户具备进入目录的权限。 |
s或S | SUID,Set UID,可执行的文件搭配这个权限,便能获得特权,任意存取该文件的全部者能使用的所有系统资源。请注意具有SUID权限的文件,黑客常常利用这种权限,以SUID配上root账号拥有者,无声无息地在系统中开扇后门,供往后进出使用。 |
t或T | sticky,/tmp和 /var/tmp目录供全部用户暂时存取文件,亦即每位用户皆拥有完整的权限进入该目录,去浏览、删除和移动文件。 |
.或+ | 若是为+表示设置了ACL |
此处test1.txt文件,其建立者/全部者具备可读可写的权限,其建立者/全部者所在的组的其余用户具备可读权限,其余组的其余用户则具备可读权限。
文件权限除了可使用rwx表示,也能够用数字表示。如777表明:rwxrwxrwx(r=4,w=2,x=1,4+2+1=7=rwx),由此能够看出数字表示会简洁一些。
链接个数
对于文件,表示指向它的连接文件的个数;对于目录文件,表示它的第一级子目录的个数。注意此处看到的值要减2才等于该目录下的子目录的实际个数,好比test1目录下其实并无任何文件和目录,但此处显示为2,这是由于要加上.目录和..目录。在linux下,.目录表示当前目录,..目录表示上一级目录。
全部者和所属组
表示该文件的全部者/建立者(owner)及其所在的组(group)。
文件大小
若是是文件,则表示该文件的大小,单位为字节。若是是目录,则表示该目录符所占的大小,并不表示该目录下全部文件的大小。
修改日期
该文件最后修改的日期时间。
文件名称
文件或目录的名称。
不一样的shell窗口,还能用颜色区分文件的属性,能一目了然经过颜色区分文件类型,普通文件、可执行文件、压缩文件、目录、链接文件都有不一样的颜色区分。不一样工具能够根据本身须要进行颜色方案的修改和配置,这里就不说了。
语法:
chmod [选项] [ugoa] [+-=] [rwx或数字] 文件或目录
选项参数:
选项 | 含义 |
---|---|
-R | recursive,递归执行 |
属性符号 | 做用 |
---|---|
u | 文件拥有者 |
g | 文件所属组 |
o | 文件所属组外的其余用户 |
a | 全部用户,至关因而ugo同时使用 |
操做符号 | 做用 |
---|---|
+ | 添加权限 |
- | 删除权限 |
= | 设置权限,未指定的部分将被清除 |
权限符号 | 做用 |
---|---|
r | 对于文件有查看权限,对于目录能够列出目录内容 |
w | 对于文件有修改权限,对于目录能够在目录中建立和删除 |
x | 对于文件有执行权限,对于目录能够进入目录 |
功能描述:
改变文件或目录权限
案例:
(1)分别经过ugoa添加文件的可执行权限
[root@heimatengyun test1]# ll
total 8
-rw-r--r--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod u+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr--r--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod g+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr-xr--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod a+x test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr-x. 1 root root 6 Dec 20 14:51 test
复制代码
从示例能够看到能够经过u、g、o分别对不一样部分赋予权限,也能够直接用o一次性赋值。根据实际须要灵活选择便可。
(2)移除全部者或所属组以外其余用户的执行权限
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr-x. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o-x test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr--. 1 root root 6 Dec 20 14:51 test
复制代码
(3)用数字设置权限
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod 777 test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
复制代码
(4)经过=号设置权限
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o=w test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwx-w-. 1 root root 6 Dec 20 14:51 test
复制代码
(5)改变目录下全部文件
[root@heimatengyun test]# chmod -R 777 test1
[root@heimatengyun test]# ll
drwxrwxrwx. 2 root root 29 Dec 20 14:52 test1
[root@heimatengyun test]# cd test1/
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
复制代码
能够看到内部的全部文件权限一次性被修改。
语法:
chown [选项] 最终用户[:最终所属组] 文件或目录
参数:
参数 | 做用 |
---|---|
-R | 递归修改 |
功能描述:
改变文件或目录的全部者
案例:
(1)修改文件全部者
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chown test test
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test root 6 Dec 20 14:51 test
复制代码
(2)递归修改目录及其下全部文件全部者
[root@heimatengyun test]# chown -R test:test /root/test/test1
[root@heimatengyun test]# ll
drwxrwxrwx. 2 test test 29 Dec 20 14:52 test1
[root@heimatengyun test]# cd test1/
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test test 6 Dec 20 14:51 test
复制代码
这种用法在修改全部者时同时修改了所属组。注意,修改的当前目录及其下的全部文件都会修改,上级目录不会影响。如此处的/root/test目录不会变化,只会影响test1目录。
语法:
chgrp 最终用户组 文件或目录
功能描述:
改变文件或目录的所属组
案例:
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test test 6 Dec 20 14:51 test
[root@heimatengyun test1]# chgrp root test
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test root 6 Dec 20 14:51 test
复制代码
单纯设置文件的 rwx 通常权限没法知足咱们对安全和灵活性的需求,所以便有了 SUID、SGID 与 SBIT 的特殊权限位。
这是一种对文件权限进行设置的特殊功 能,能够与通常权限同时使用,以弥补通常权限不能实现的功能。
针对二进制程序设置的特殊权限,可让二进制程序的执行者临时拥有属主的权限(仅对拥有执行权限的二进制程序有效)。SUID是一种有条件的、临时的特殊权限受权方法,下文以passwd命令进行介绍。
还记得1.1.4讲的passwd命令吗?该命令用于修改用户密码,全部用户均可以执行 passwd 命 令来修改本身的用户密码,而用户密码保存在/etc/shadow 文件中,执行passwd命令本质就是修改shadow文件。
但仔细查看这个文件就会发 现它的默认权限是 000,也就是说除了 root 管理员之外,全部用户都没有查看或编辑该文件的权限。
[root@heimatengyun test1]# ll /etc/shadow
----------. 1 root root 1157 Dec 20 00:06 /etc/shadow
复制代码
既然没有此密码文件读写权限,那为什么全部用户均可以执行命令修改密码呢?先不急,咱们来看看passed命令的权限。
[root@heimatengyun test1]# ll /bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /bin/passwd
复制代码
能够看到全部者权限为rws,之前讲过可执行权限为x,此处为s。对,就是由于全部者的权限由 rwx变成了rws,其中x改变成s就意味着该文件被赋予了SUID 权限。在使用 passwd 命令时若是加上 SUID 特殊权限位,就可以让普通用户临时得到程序全部者的身份,把变动的密码信息写入到 shadow 文件中。
说明:设置SUID后,若是本来没有执行权限,则为S,有执行权限则为s。如rwx将变为rws,rw-变为rwS。
设置SUID权限,就是使用以前介绍的chmod命令便可,针对命令或可执行二进制文件进行设置。
[root@heimatengyun test]# ll
-rwxrwxrwx. 1 root root 145 Dec 1 16:06 mypid.sh
[root@heimatengyun test]# chmod u+s mypid.sh
[root@heimatengyun test]# ll
-rwsrwxrwx. 1 root root 145 Dec 1 16:06 mypid.sh
复制代码
主要应用场景和功能有:
(1)让执行者临时拥有所属组权限,对拥有执行权限的二进制程序进行设置。
(2)在某个目录中建立的文件自动继承该目录的用户组,只能够对目录进行设置。
SGID 的第一种功能是参考 SUID 而设计的,不一样点在于执行程序的用户获取的再也不是文 件全部者的临时权限,而是获取到文件所属组的权限。
针对第二种功能,咱们知道,每一个文件都有其归属的全部者和所属组,当建立或传送一个文件后,这个文件就会自动归属于执行这个操做的用户,即该用户就是文件的全部者。
假设有这样一种状况,须要在部门内建立一个共享目录,部门内全部人员都能读取目录中的内容。若是每一个人都去创建各自的文件,全部者和所属组都是建立者本身,别人没法使用。SGID的出现就是为了解决这个问题,建立部门共享目录后,在该目录上设置 SGID 特殊权限位。这样,部门内的任何人员在里面建立的任何文件都会归属于该目录的所属组,而再也不是本身的基本用户组。
SGID功能就是在某个目录中建立的文件自动继承该目录的用户组,只能够对目录进行设置。
下面演示在目录上建立SGID
[root@heimatengyun test]# mkdir sgid
[root@heimatengyun test]# ll
drwxr-xr-x. 2 root root 6 Dec 20 18:11 sgid
[root@heimatengyun test]# chmod 777 sgid/
[root@heimatengyun test]# ll
drwxrwxrwx. 2 root root 6 Dec 20 18:11 sgid
[root@heimatengyun test]# chmod g+s sgid/
[root@heimatengyun test]# ll
drwxrwsrwx. 2 root root 6 Dec 20 18:11 sgid
复制代码
建立SGID后,就能够切换到普通用户,建立文件,观察文件的所属组
[root@heimatengyun test]# su - test
Last login: Fri Dec 20 17:26:36 CST 2019 on pts/0
[test@heimatengyun ~]$ cd sgid/
[test@heimatengyun sgid]$ echo 'hello'>hello
[test@heimatengyun sgid]$ ll
total 4
-rw-rw-r--. 1 test root 6 Dec 20 18:14 hello
复制代码
能够看到test普通用户建立的文件所属组变为给上层文件夹一致属于root,再也不属于test本身。这样针对所属组设置权限,其余用户就能够实现操做文件。
SBIT 特殊权限位可确保用户只能删除本身的文件,而不能删除其余用户的文件。当对某个目录设置了 SBIT 粘滞位权限后,那么该目录中的文件就只能被其全部者执行删除操做了。
回到上文的例子,当一个部门共享一个目录后,如何避免用户删除其余用户的文件呢?显然用SBIT就能够保证用户不能删除别人的文件。
设置目录SBIT,一样用chmod命令
[root@heimatengyun test]# mkdir sbit
[root@heimatengyun test]# ll
drwxr-xr-x. 2 root root 6 Dec 20 18:26 sbit
[root@heimatengyun test]# chmod -R o+t sbit/
[root@heimatengyun test]# ll
drwxr-xr-t. 2 root root 6 Dec 20 18:26 sbit
复制代码
与前面所讲的 SUID 和 SGID 权限显示方法不一样,当目录被设置 SBIT 特殊权限位后,文件的其余人权限部分的 x 执行权限就会被替换成 t 或者 T,本来有 x 执行权限则会写成 t,本来没有 x 执行权限则会被写成 T。
咱们仔细观察,就会发现/tmp目录其实就是默认设置了SBIT,它是一个共享目录,保证用户只能删除本身的文件。
[root@heimatengyun /]# ll -d /tmp/
drwxrwxrwt. 15 root root 4096 Dec 20 18:30 /tmp/
复制代码
建立新用户并在tmp下建立文件,验证用其余用户去删除看可否删除,答案确定是不能删除的。
[root@heimatengyun /]# useradd heimagege
[root@heimatengyun /]# su - heimagege
[heimagege@heimatengyun ~]$ cd /tmp/
[heimagege@heimatengyun tmp]$ echo 'heimagege'>heimagege
[heimagege@heimatengyun tmp]$ ll
-rw-rw-r--. 1 heimagege heimagege 10 Dec 20 18:34 heimagege
[root@heimatengyun /]# su - test
Last login: Fri Dec 20 18:29:10 CST 2019 on pts/0
[test@heimatengyun ~]$ cd /tmp/
[test@heimatengyun tmp]$ rm -f heimagege
rm: cannot remove ‘heimagege’: Operation not permitted
复制代码
Linux 系统中的文件除了具有通常权限和特殊权限以外,还有一种隐藏权限,即被隐藏起 来的权限,默认状况下不能直接被用户发觉。当你新接手一台服务器,碰到明明权限充足但却没法删除某个文件的状况,或者仅能在日志文件中追加内容而不能修改或删除内容,这肯you可能就是设置了文件隐藏属性。这种属性在必定程度上阻止了黑客篡改系统日志的图谋,所以这种“奇怪”的文件也保障了Linux 系统的安全性。
语法:
chattr [选项] [+-=参数] 文件
选项及参数:
选项 | 做用 |
---|---|
R | 递归 |
参数 | 做用 |
---|---|
i | 没法对文件进行修改;若对目录设置了该参数,则仅能修改其中的子文件内容 而不能新建或删除文件 |
a | 仅容许补充(追加)内容,没法覆盖/删除内容(Append Only) |
S | 文件内容在变动后当即同步到硬盘(sync) |
s | 完全从硬盘中删除,不可恢复(用 0 填充原文件所在硬盘区域) |
A | 再也不修改这个文件或目录的最后访问时间(atime) |
b | 再也不修改文件或目录的存取时间 |
d | 使用 dump 命令备份时忽略本文件/目录 |
u | 当删除该文件后依然保留其在硬盘中的数据,方便往后恢复 |
功能描述:
设置文件的隐藏权限
案例:
经过隐藏属性,设置文件不能删除
[root@heimatengyun test]# echo 'test chattr'>testattr
[root@heimatengyun test]# chattr +a testattr
[root@heimatengyun test]# rm testattr
rm: remove regular file ‘testattr’? y
rm: cannot remove ‘testattr’: Operation not permitted
复制代码
语法:
lsattr 文件
功能描述:
显示文件的隐藏权限
案例:
查看文件隐藏属性,删除隐藏属性
[root@heimatengyun test]# lsattr testattr
-----a---------- testattr
[root@heimatengyun test]# chattr -a testattr
[root@heimatengyun test]# lsattr testattr
---------------- testattr
[root@heimatengyun test]# rm testattr
rm: remove regular file ‘testattr’? y
[root@heimatengyun test]#
复制代码
能够看到,删除隐藏属性后文件删除成功。
前文讲解的通常权限、特殊权限、隐藏权限都是针对某一类用户设置的。若是但愿对某个指定的用户进行单独的权限控制,就须要用到文件 的访问控制列表(ACL)了。
基于普通文件或目录设置 ACL 其实就是针对指定的用户或用户组设置文件或目录的操做权限。另外,若是针对某个目录设置了 ACL,则目录中 的文件会继承其 ACL;若针对文件设置了 ACL,则文件再也不继承其所在目录的 ACL。
文件的 ACL 提供的是在全部者、所属组、其余人的读/写/执行权限以外的特殊权限控制,使用 setfacl 命令能够针对单一用户或用户组、单一文件或目录来进行读/写/执行权限的控制。
语法:
setfacl [参数] 文件名称
参数选项:
参数 | 做用 |
---|---|
-R | 递归参数 |
-m | modify修改目录或文件的ACL |
-b | 删除扩展ACL,保留原有的基础权限 |
功能描述:
管理文件的 ACL 规则
案例:
让普通用户能访问root目录
[root@heimatengyun ~]# ll -d /root/
dr-xr-x---. 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 18:34:47 CST 2019 on pts/0
[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$ exit
logout
复制代码
未设置ACL前其余用户是不能访问root目录的,设置root目录ACL容许test访问。
[root@heimatengyun ~]# setfacl -Rm u:test:rwx /root/
[root@heimatengyun ~]# ll -d /root/
dr-xrwx---+ 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:10:26 CST 2019 on pts/0
[test@heimatengyun ~]$ ls /root/
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos Desktop Downloads Music Public test
复制代码
同时经过ll命令能够看到权限后边的点变为了+。这个标识说明已经设置了ACL。
语法:
getfacl 文件名称
功能描述:
查看文件上设置的ACL信息
案例:
(1)查看文件上设置的ACL
[test@heimatengyun ~]$ getfacl /root/
getfacl: Removing leading '/' from absolute path names
# file: root/
# owner: root
# group: root
user::r-x
user:test:rwx
group::r-x
mask::rwx
other::---
复制代码
(2)删除文件ACL
[root@heimatengyun ~]# ll -d /root/
dr-xrwx---+ 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# setfacl -b /root/
[root@heimatengyun ~]# ll -d /root/
dr-xr-x---. 16 root root 4096 Dec 20 11:49 /root/
复制代码
平时学习通常直接用root能够避免各类配置服务或权限致使的干扰问题,使得学习中心放在相应的知识点上。可是正式工做中,每每不多用root操做,所以可能会涉及用户切换以及用户提权问题。
su 命令能够解决切换用户身份的需求,使得当前用户在不退出登陆的状况下,顺畅地切 换到其余用户。
因为前文已经演示了su命令的用法,所以再也不赘述。只是要注意su命令与用户名之间有一个减号(-),这意味着彻底切 换到新的用户,即把环境变量信息也变动为新用户的相应信息,而不是保留原始的信息。强 烈建议在切换用户身份时添加这个减号(-)。另外,当从 root 管理员切换到普通用户时是不须要密码验证的,而从普通用户切换成 root 管理员就须要进行密码验证了;这也是一个必要的安全检查。
尽管使用 su 命令后,普通用户能够彻底切换到 root 管理员身份来完成相应工做,但这将暴露 root 管理员的密码,从而增大了系统密码被黑客获取的概率;这并非最安全的方案。
sudo 命令能够把特定命令的执行权限赋予给指定用户而无需给出root密码, 这样既可保证普通用户可以完成特定的工做,也能够避免泄露 root 管理员密码。
语法格式:
sudo [参数] 命令
参数:
参数 | 做用 |
---|---|
-l | 列出当前用户可执行的命令 |
功能描述:
sudo 命令用于给普通用户提供额外的权限来完成本来 root 管理员才能完成的任务。
使用sudo以前须要先配置sudo服务,配置文件为/etc/sudoers,能够直接编辑此文件,也可使用visudo命令进行配置。
案例:
(1)为普通用户test添加sudo权限
未为普通用户配置sudo服务时,经过sodo命令查看能执行的命令,将提高没有配置。
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:14:02 CST 2019 on pts/0
[test@heimatengyun ~]$ 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 test:
Sorry, user test may not run sudo on heimatengyun.
复制代码
经过visudo命令,在“root ALL=(ALL) ALL”后仿照添加“test ALL=(ALL) ALL”,而后保存退出。操做方式跟vi编辑器一致。
配置内容解释:test ALL=(ALL) ALL 其中test为用户名,表示谁可使用sudo,第一个ALL表示是运行使用的主机,等号以后括号内的ALL表示以谁的身份执行,最后的ALL表示可执行命令的列表。“谁可使用 容许使用的主机=(以谁的身份) 可执行命令的列表”
配置后,再次使用test用户来查看,将得出以下结果:
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:41:52 CST 2019 on pts/0
[test@heimatengyun ~]$ sudo -l
[sudo] password for test:
Matching Defaults entries for test 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 test may run the following commands on this host:
(ALL) ALL
复制代码
代表针对test用户的sudo服务配置成功。配置成功以后,就能够采用sudo提高test用户的权限了。
[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$ sudo ls /root/
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos
Desktop Downloads Music Public test
[test@heimatengyun ~]$
复制代码
是否是很神奇,test用户经过sudo提权就能够看到root目录内容了。
(2)按需为用户配置sudo权限
咱们前边直接给了ALL最大的权限,实际状况应该按需分配最小权限,好比咱们只给test用户cat命令权限。
经过whereis命令查看cat所在路径,必定要给命令的绝对路径,否则系统没法识别命令。
[test@heimatengyun ~]$ whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz
复制代码
经过visudo命令修改以前添加的内容为:test ALL=(ALL) /usr/bin/cat。保存后切换到test普通用户查看效果
[root@heimatengyun ~]# visudo
...省略部份内容
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
#test ALL=(ALL) ALL
test ALL=(ALL) /usr/bin/cat
...省略部份内容
[test@heimatengyun ~]$ sudo -l
[sudo] password for test:
Matching Defaults entries for test 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 test may run the following commands on this host:
(ALL) /usr/bin/cat
[test@heimatengyun ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[test@heimatengyun ~]$ sudo cat /etc/shadow
...省略部份内容
heimagege:!!:18250:0:99999:7:::
[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$
复制代码
从实验结果能够看出,配置cat命令后,只能执行cat命令,再次使用ls命令就不能看到root目录内容。这样权限就获得了很好的控制。
学习完用户及文件相关权限知识后,下一篇文章咱们将讲解防火墙相关知识。