权限简介
文件的权限主要针对三类对象进行定义:
- owner:属主,u
- group:属组,g
- other:其它,o
每个文件针对每个访问者都定义了三种权限:
权限 | 对应的操作对象 | 权限说明 |
---|---|---|
r | 文件 | 可读,可以使用类似cat等命令查看文件内容 |
w | 文件 | 可写,可以编辑或删除此文件 |
x | 文件 | 可执行,eXacutable,可以在命令提示符下 当作命令提交给内核运行 |
r | 目录 | 可以对此目录执行ls以列出内部的所有文件 |
w | 目录 | 可以在此目录中创建文件,也可删除此目录中的文件 |
x | 目录 | 可以使用cd切换进此目录,也可以 使用ls -l查看内部文件的详细信息 |
权限的二进制与十进制转换:
权限 | 二进制 | 十进制 |
---|---|---|
— | 000 | 0 |
–x | 001 | 1 |
-w- | 010 | 2 |
-wx | 011 | 3 |
r– | 100 | 4 |
r-x | 101 | 5 |
rw- | 110 | 6 |
rwx | 111 | 7 |
权限管理命令
- 修改权限的命令 chmod
//修改三类用户的权限:
//语法:chmod MODE file,…
-R //递归修改权限
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 root root 42 9月 22 13:58 abc
[root@localhost ~]# ll abc/
总用量 0
-rw-r--r--. 1 root root 0 9月 22 13:58 a
-rw-r--r--. 1 root root 0 9月 22 13:58 b
-rw-r--r--. 1 root root 0 9月 22 13:58 c
-rw-r--r--. 1 root root 0 9月 22 13:58 d
[root@localhost ~]# chmod 777 abc
[root@localhost ~]# ll
总用量 8
drwxrwxrwx. 2 root root 42 9月 22 13:58 abc
[root@localhost ~]# chmod -R 777 abc
[root@localhost ~]# ll abc/
总用量 0
-rwxrwxrwx. 1 root root 0 9月 22 13:58 a
-rwxrwxrwx. 1 root root 0 9月 22 13:58 b
-rwxrwxrwx. 1 root root 0 9月 22 13:58 c
-rwxrwxrwx. 1 root root 0 9月 22 13:58 d
//修改某类用户或某些类用户权限:
//u,g,o,a(用户类别)
[root@localhost ~]# ll
总用量 8
drwxrwxrwx. 2 root root 42 9月 22 13:58 abc
[root@localhost ~]# chmod a-w abc
[root@localhost ~]# ll
总用量 8
dr-xr-xr-x. 2 root root 42 9月 22 13:58 abc
[root@localhost ~]# chmod u+w abc
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 root root 42 9月 22 13:58 abc
修改文件属主和属组的命令chown
chown命令只有管理员可以使用
//chown USERNAME file,…
-R //修改目录及其内部文件的属主
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 root root 6 9月 22 13:47 abc
[root@localhost ~]# chown tom:tom abc
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 tom tom 6 9月 22 13:47 abc
[root@localhost ~]# chown root.root abc
[root@localhost ~]# cd abc
[root@localhost abc]# touch a b c d
[root@localhost abc]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 22 13:58 a
-rw-r--r--. 1 root root 0 9月 22 13:58 b
-rw-r--r--. 1 root root 0 9月 22 13:58 c
-rw-r--r--. 1 root root 0 9月 22 13:58 d
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 root root 42 9月 22 13:58 abc
-rw-------. 1 root root 1729 9月 7 07:10 anaconda-ks.cfg
-rw-r--r--. 1 root root 1822 9月 7 07:14 initial-setup-ks.cfg
[root@localhost ~]# chown -R tom.tom abc
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 tom tom 42 9月 22 13:58 abc
[root@localhost ~]# ll abc/
总用量 0
-rw-r--r--. 1 tom tom 0 9月 22 13:58 a
-rw-r--r--. 1 tom tom 0 9月 22 13:58 b
-rw-r--r--. 1 tom tom 0 9月 22 13:58 c
-rw-r--r--. 1 tom tom 0 9月 22 13:58 d
//chown USERNAME:GROUPNAME file,…
//chown USERNAME.GROUPNAME file,…
修改文件的属组命令chgrp
//语法:chgrp [OPTION]… GROUP FILE…
-R //递归修改
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 tom tom 42 9月 22 13:58 abc
[root@localhost ~]# chgrp root abc
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 tom root 42 9月 22 13:58 abc
[root@localhost ~]# ll abc/
总用量 0
-rw-r--r--. 1 tom tom 0 9月 22 13:58 a
-rw-r--r--. 1 tom tom 0 9月 22 13:58 b
-rw-r--r--. 1 tom tom 0 9月 22 13:58 c
-rw-r--r--. 1 tom tom 0 9月 22 13:58 d
[root@localhost ~]# chgrp -R root abc/
[root@localhost ~]# ll abc/
总用量 0
-rw-r--r--. 1 tom root 0 9月 22 13:58 a
-rw-r--r--. 1 tom root 0 9月 22 13:58 b
-rw-r--r--. 1 tom root 0 9月 22 13:58 c
-rw-r--r--. 1 tom root 0 9月 22 13:58 d
遮罩码
为什么文件创建以后默认权限是644?
为什么目录创建以后默认权限是755?
这是由遮罩码umask来控制的。
从名字就能看出来,遮罩码umask是用来隐藏一些权限的。举例:如果你不想让人家认出你,你会怎么办?
文件最终的权限为:
666-umask
目录最终的权限为:
777-umask
文件默认是不能具有执行权限的,如果算得的结果有执行权限则将其权限整体加1。
[root@localhost ~]# umask
0022
[root@localhost ~]# touch abc
[root@localhost ~]# mkdir def
[root@localhost ~]# ll
总用量 8
-rw-r--r--. 1 root root 0 9月 22 14:32 abc
drwxr-xr-x. 2 root root 6 9月 22 14:32 def
[root@localhost ~]# umask 0011
[root@localhost ~]# umask
0011
[root@localhost ~]# touch bcd
[root@localhost ~]# mkdir opt
[root@localhost ~]# ll
总用量 8
-rw-r--r--. 1 root root 0 9月 22 14:32 abc // 权限为655
-rw-rw-rw-. 1 root root 0 9月 22 14:35 bcd //权限为666 权限整体+1
drwxr-xr-x. 2 root root 6 9月 22 14:32 def //权限为755
drwxrw-rw-. 2 root root 6 9月 22 14:36 opt //权限为766
linux安全上下文
前提:进程有属主和属组;文件有属主和属组
- 任何一个可执行程序文件能不能启动为进程,取决于发起者对程序文件是否拥有可执行权限;
- 启动为进程后,其进程的属主为发起者,属组为发起者所属的组
- 进程访问文件时的权限取决于进程的发起者:
- 进程的发起者是文件的属主时,则应用文件属主权限
- 进程的发起者是文件的属组时,则应用文件属组权限
- 应用文件“其它”权限
特殊权限
linux默认权限是根据linux安全上下文的方式来控制的,而特殊权限的存在打破了linux安全上下文的规则。
- SUID(4) //运行程序时,这个程序启动的进程的属主是程序文件自身的属主,而不是启动者为属主
chmod u+s file
chmod u-s file
//如果file本身原来就有执行权限,则SUID显示为s,否则显示为S
[root@localhost ~]# ll
总用量 8
-rw-r--r--. 1 root root 0 9月 22 14:32 abc
-rw-rw-rw-. 1 root root 0 9月 22 14:35 bcd
drwxr-xr-x. 2 root root 6 9月 22 14:32 def
drwxrw-rw-. 2 root root 6 9月 22 14:36 opt
[root@localhost ~]# /home/tom/vim abc
[root@localhost ~]# ps -ef| grep vim
root 8722 5923 0 16:20 pts/0 00:00:00 /home/tom/vim abc
root 8724 8228 0 16:20 pts/1 00:00:00 grep --color=auto vim
[root@localhost ~]# ll /home/tom/vim
-rwxr--r--. 1 tom tom 2289648 9月 22 15:39 /home/tom/vim
[root@localhost ~]# ll /home/tom/vim
-rwxr--r--. 1 tom tom 2289648 9月 22 15:39 /home/tom/vim
[root@localhost ~]# chmod 4744 /home/tom/vim
[root@localhost ~]# ll /home/tom/vim
-rwsr--r--. 1 tom tom 2289648 9月 22 15:39 /home/tom/vim
[root@localhost ~]# ps -ef | grep vim
tom 8742 5923 0 16:22 pts/0 00:00:00 /home/tom/vim abc
root 8744 8228 0 16:22 pts/1 00:00:00 grep --color=auto vim
- SGID(2) //运行程序时,这个程序启动的进程的属组是程序文件自身的属组,而不是启动者所属的基本组
//默认情况下,用户创建文件时,其属组为此用户所属的基本组;
//一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件或目录,其所属的组
//为此设定了SGID的目录的属组
chmod g+s DIR
chmod g-s DIR
//如果file本身原来就有执行权限,则SGID显示为s,否则显示为S
[root@localhost ~]# chgrp tom test/
[root@localhost ~]# ll test/
总用量 0
-rw-r--r--. 1 root root 0 9月 22 16:24 a
drwxr-xr-x. 2 root root 6 9月 22 16:24 b
[root@localhost ~]# ll -d test/
drwxr-xr-x. 3 root tom 24 9月 22 16:24 test/
[root@localhost ~]# ll test/
总用量 0
-rw-r--r--. 1 root root 0 9月 22 16:24 a
drwxr-xr-x. 2 root root 6 9月 22 16:24 b
[root@localhost ~]# chmod 2755 test/
[root@localhost ~]# cd test/
[root@localhost test]# touch c
[root@localhost test]# mkdir d
[root@localhost test]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 22 16:24 a
drwxr-xr-x. 2 root root 6 9月 22 16:24 b
-rw-r--r--. 1 root tom 0 9月 22 16:28 c
drwxr-sr-x. 2 root tom 6 9月 22 16:28 d
- Sticky(1) //在一个公共目录,每个人都能创建文件,删除自己的文件,但是不能删除别人创建的文件
chmod o+t DIR
chmod o-t DIR
//如果DIR本身原来就有执行权限,则Sticky显示为t,否则显示为T
[root@localhost test]# chmod 1777 /opt/test/
[root@localhost test]# ll -d /opt/test/
drwxrwxrwt. 3 root root 24 9月 22 16:42 /opt/test/
[root@localhost test]# ll
总用量 0
drwxrw-rw-. 2 root root 6 9月 22 16:42 a
-rw-rw-rw-. 1 root root 0 9月 22 16:42 b
[tom@localhost ~]$ cd /opt/test/
[tom@localhost test]$ ll
总用量 0
drwxrw-rw-. 2 root root 6 9月 22 16:42 a
-rw-rw-rw-. 1 root root 0 9月 22 16:42 b
[tom@localhost test]$ mkdir c
[tom@localhost test]$ touch d
[tom@localhost test]$ ll
总用量 0
drwxrw-rw-. 2 root root 6 9月 22 16:42 a
-rw-rw-rw-. 1 root root 0 9月 22 16:42 b
drwxrwxr-x. 2 tom tom 6 9月 22 16:45 c
-rw-rw-r--. 1 tom tom 0 9月 22 16:45 d
[tom@localhost test]$ rm -rf a
rm: 无法删除"a": 不允许的操作
[tom@localhost test]$ rm -rf c
[tom@localhost test]$ ll
总用量 0
drwxrw-rw-. 2 root root 6 9月 22 16:42 a
-rw-rw-rw-. 1 root root 0 9月 22 16:42 b
-rw-rw-r--. 1 tom tom 0 9月 22 16:45 d
[root@localhost ~]# su - wangqing
[wangqing@localhost ~]$ cd /opt/test/
[wangqing@localhost test]$ ll
总用量 0
drwxrw-rw-. 2 root root 6 9月 22 16:42 a
-rw-rw-rw-. 1 root root 0 9月 22 16:42 b
-rw-rw-r--. 1 tom tom 0 9月 22 16:45 d
[wangqing@localhost test]$ rm -rf d
rm: 无法删除"d": 不允许的操作
4755 //有SUID,文件权限为755
2755 //有SGID,文件权限为755
1755 //有Sticky,文件权限为755
7755 //有SUID、SGID、Sticky,文件权限为755
//这里前面的4、2、1分别表示SUID、SGID、Sticky
文件系统访问控制列表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
root@localhost ~]# cd /opt
[root@localhost opt]# ll
总用量 0
drwxrwxrwt. 3 root root 33 9月 22 16:48 test
[root@localhost opt]# touch abc
[root@localhost opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 22 16:59 abc
drwxrwxrwt. 3 root root 33 9月 22 16:48 test
[root@localhost opt]# setfacl -m u:jerry:rw abc //setfacl -m u:test:rw file
[root@localhost opt]# ll
总用量 0
-rw-rw-r--+ 1 root root 0 9月 22 16:59 abc
drwxrwxrwt. 3 root root 33 9月 22 16:48 test
[root@localhost opt]# getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
user:jerry:rw-
group::r--
mask::rw-
other::r--
[root@localhost opt]# id tom
uid=1003(tom) gid=1005(tom) 组=1005(tom)
[root@localhost opt]# setfacl -m g:tom:rw abc //setfacl -m g:test:rw file
[root@localhost opt]# getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
user:jerry:rw-
group::r--
group:tom:rw-
mask::rw-
other::r--
`
//如果要为某个目录设定默认的访问控制列表,只需要设定时在u或g前面加上d即可。 如:
//setfacl -m u:test:rw file,此时在此目录中创建的文件均继承此访问控制列表所设置的权限
-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 a
# file: a
# owner: root
# group: root
user::rwx
user:tom:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:tom:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@localhost opt]# setfacl -b a // -b
[root@localhost opt]# setfacl -m u:tom:rwx a //-m
[root@localhost opt]# getfacl a
# file: a
# owner: root
# group: root
user::rwx
user:tom:rwx
group::r-x
mask::rwx
other::r-x
[root@localhost opt]# setfacl -x u:tom a //-x
+ setfacl -x u:tom a
++ printf '\033]0;%s@%s:%s\007' root localhost /opt
[root@localhost opt]# getfacl a
+ getfacl a
# file: a
# owner: root
# group: root
user::rwx
group::r-x
mask::r-x
other::r-x
//文件权限应用次序:
//默认情况下:
Owner --> Group --> Other
//有设置facl的情况下:
Owner --> facl,user --> Group --> facl,group --> Other
sudo
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 =
- 用户的用户名
- 组名,使用%引导
- 还可以其它已经定义的用户别名
- User_Alias NETWORKADMIN =
- 主机别名:
- Host_Alias =
- 主机名
- IP地址
- 网络地址
- 其它主机别名
- Host_Alias =
- Runas别名:
- Runas_Alias =
- 用户名
- %组名
- 其它的Runas别名
- Runas_Alias =
- 命令别名:
- Cmnd_Alias =
- 命令路径
- 目录(此目录内的所有命令)
- 其它已定义的命令别名
- Cmnd_Alias =
[tom@localhost ~]$ useradd abc
-bash: /usr/sbin/useradd: 权限不够
[root@localhost ~]# visudo
root ALL=(root) /usr/sbin/useradd,/usr/sbin/userdel, /usr/sbin/usermod
[tom@localhost ~]$ sudo useradd runtime
我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:
#1) 尊重别人的隐私。
#2) 输入前要先考虑(后果和风险)。
#3) 权力越大,责任越大。
[sudo] tom 的密码:
[tom@localhost ~]$
---------------------------------------------------------------
[tom@localhost ~]$ sudo groupadd bbb
[sudo] tom 的密码:
对不起,用户 tom 无权以 root 的身份在 localhost.localdomain 上执行 /sbin/groupadd bbb。
text
**//sudo命令语法:sudo [options] COMMAND
-V //显示版本编号
-h //会显示版本编号及指令的使用方式说明
-l //列出当前用户可以使用的所有sudo类命令 **
-v //因为sudo在第一次执行时或是在N分钟内没有执行(N默认为5)会问密码,这个参数
//是重新做一次确认,如果超过N分钟,也会问密码
-k //让认证信息失效,如果不指定-k,默认认证信息在5分钟后失效
-b //将要执行的指令放在后台执行
-u USERNAME //以指定的用户名执行命令,默认为root
管理命令
w //显示当前登录到系统的用户有哪些,以及其正在做什么
[tom@localhost ~]$ w
18:06:42 up 9:43, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.116.1 13:27 2.00s 1.57s 0.01s w
root pts/1 192.168.116.1 15:41 33:22 1.71s 0.11s -bash
sleep //睡眠,写脚本为防止上一个命令没执行完下一命令就开始执行时可以加上sleep #
//表示停顿#秒后再执行后面的命令
sleep NUMBER[SUFFIX]…
SUFFIX:
s:秒,默认
m:分
h:小时
d:天
eg: echo 'hello world' ;sleep 5;echo 'runtime'
[tom@localhost ~]$ echo 'hello world' ;sleep 5;echo 'runtime'
hello world
runtime
last //显示/var/log/wtmp文件,显示用户登录历史及系统重启历史
-n # //显示最近#次的相关信息
[root@localhost ~]# last -n3
root pts/1 192.168.116.1 Sun Sep 22 15:41 still logged in
root pts/1 192.168.116.1 Sun Sep 22 15:09 - 15:41 (00:31)
root pts/1 192.168.116.1 Sun Sep 22 14:56 - 15:09 (00:12)
wtmp begins Sat Sep 7 07:10:44 2019
lastb //显示/var/log/btmp文件,显示用户错误的登录尝试
-n # //显示最近#次的相关信息
[root@localhost ~]# lastb
root pts/0 Wed Sep 18 16:41 - 16:41 (00:00)
root pts/1 Wed Sep 18 11:46 - 11:46 (00:00)
btmp begins Wed Sep 18 11:46:50 2019
lastlog //显示每个用户最近一次成功登录信息
-u username //显示特定用户最近的登录信息
[root@localhost ~]# lastlog -u tom
用户名 端口 来自 最后登陆时间
tom pts/0 日 9月 22 17:49:34 +0800 2019
basename //显示路径基名