保存用户信息的文件:
账号: /etc/passwd
组:/etc/group
密码:/etc/shadow
$ id
uid=1000(admin) gid=1000(admin) groups=1000(admin),10(wheel)
$ id root
uid=0(root) gid=0(root) groups=0(root)
文件类型
$ ls -l programs.txt
-rw-rw-r--. 1 admin admin 5654 Apr 23 15:13 programs.txt
第一个字符代表文件类型:
-: 普通文件
d:目录
l:链接文件,链接文件的属性总是 lrwxrwxrwx. ,真正的权限要看被链接文件的属性
c:character 代表以字符(数据流)形式处理数据的设备;比如:终端, Modem
b:block, 代表以块(block)形式处理数据的设备;比如:硬盘,CD-ROM
权限
权限:model / permission
Permission
修改权限
用命令 chmod 修改权限,有2中方式:
使用八进制
# 4 2 1
# r w x
# 如果需要读写权限,只要设置为 6 (4+2) 即可
# 创建文件
$ echo hello > hello.txt
$ ls -l
-rw-rw-r--. 1 admin admin 6 Apr 23 20:48 hello.txt
# 去掉写入权限
$ chmod 400 hello.txt
$ ls -l
-r--------. 1 admin admin 6 Apr 23 20:48 hello.txt
# 写入失败
$ echo world >> hello.txt
-bash: hello.txt: Permission denied
#-------------------------------------------------------#
# 添加写入权限
$ chmod 600 hello.txt
$ ls -l
-rw-------. 1 admin admin 6 Apr 23 20:48 hello.txt
# 写入成功
$ echo world >> hello.txt
$ cat hello.txt
hello
world
使用标识修改权限
使用方法
chmod [who][,[who]]
who
u: user 默认,可省略
g: group
o: other
a: all
operator
+: 添加权限
-: 去掉权限
=:设置权限
perssion
r
w
x
例如
$ ls -l
-rw-------. 1 admin admin 12 Apr 23 20:50 hello.txt
# a (all) 添加读写权限
$ chmod a+rw hello.txt
$ ls -l
-rw-rw-rw-. 1 admin admin 12 Apr 23 20:50 hello.txt
# g (group) 去掉组的读写权限
$ chmod g-rw hello.txt
$ ls -l
-rw----rw-. 1 admin admin 12 Apr 23 20:50 hello.txt
# o (other) 去掉其他人的读写权限
$ chmod o-rw hello.txt
$ ls -l
-rw-------. 1 admin admin 12 Apr 23 20:50 hello.txt
权限和文件备份
备份配置文件,将备份文件设为只读,避免被修改
$ ls -l
total 4
-rw-r--r--. 1 admin admin 970 Apr 23 20:27 yum.conf
# 备份源文件,并设置为只读
$ cp yum.conf yum.conf~; chmod 400 yum.conf~
$ ls -l
total 8
-rw-r--r--. 1 admin admin 970 Apr 23 20:27 yum.conf
-r--------. 1 admin admin 970 Apr 23 20:28 yum.conf~
备份配置文件,将备份文件放入无写入权限的目录,避免被误删
$ ls
yum.conf
# 由于文件的删除(移动/重命名)操作权限在目录设置,可以新建一个目录
# 并将目录的写入权限去掉,避免误删
$ mkdir source_files_backup
$ cp yum.conf source_files_backup/
$ chmod 500 source_files_backup/
## 删除文件
$ rm source_files_backup/yum.conf
rm: cannot remove ‘source_files_backup/yum.conf’: Permission denied
$ rm -rf source_files_backup/yum.conf
rm: cannot remove ‘source_files_backup/yum.conf’: Permission denied
$ rm -rf source_files_backup/
rm: cannot remove ‘source_files_backup/yum.conf’: Permission denied
## 删除目录
$ rm source_files_backup
rm: cannot remove ‘source_files_backup’: Is a directory
$ rm -rf source_files_backup
rm: cannot remove ‘source_files_backup/yum.conf’: Permission denied
特殊权限
setuid: 4000 【赋执行者予命令创建者权限】(设置在可执行文件上)
setgid: 2000 【设置在目录上,在目录中创建的文件的组的值继承目录的组的值】
sticky bit: 1000 【设置在目录上,只有文件拥有者,组或创建用户可以删除/修改文件,即使目录权限为 1777】 ;比如:/tmp
real user: 执行命令的人
effective user:命令执行时用的身份
比如:setuid 会改变 effective user 为文件创建者
或者,sudo 的 real user 为执行者,而 effective user 为 root
setuid
比如,普通用户可以执行 passwd,但资源 /etc/shadow 只有 root 才能访问;为 passwd 设置 setuid 后:普通用于将以 root 身份执行 passwd
$ ll $(which passwd)
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
$ ll /etc/shadow
----------. 1 root root 690 Apr 24 01:55 /etc/shadow
setgid
当 setgid 设置在目录时,在目录中新创建文件的组的值会继承目录的组的值(默认新创建的文件的拥有者和组都是创建者)-- 此特性可以用于共享目录
需求:创建一个共享目录,Boss 们能添加/ 删除/ 修改目录中的文件,其他人只能读取
# 招工啦,招工啦
[root@localhost ~]# 添加用户 john, tom; 并加入组 boss
[root@localhost ~]# groupadd boss
[root@localhost ~]# useradd -G boss john
[root@localhost ~]# passwd john
[root@localhost ~]# useradd -G boss tom
[root@localhost ~]# passwd tom
[root@localhost ~]# id john
uid=1001(john) gid=1002(john) groups=1002(john),1001(boss)
[root@localhost ~]# id tom
uid=1002(tom) gid=1003(tom) groups=1003(tom),1001(boss)
# 创建共享目录
[root@localhost ~]# mkdir /share
# 设置 setgid
[root@localhost ~]# chmod 2775 /share
[root@localhost ~]# chown :boss /share
[root@localhost ~]# ll -d /share
drwxrwsr-x. 2 root boss 6 Apr 24 01:31 /share
# 老板委派任务
[root@localhost ~]# exit
exit
[admin@localhost ~]$ su john
[john@localhost admin]$ cd /share/
[john@localhost share]$ echo "john: New Project" >> task.txt
[john@localhost share]$ su tom
[tom@localhost share]$ echo "tom: New Project" >> task.txt
# 招工啦,招工啦
# 苦逼程序员 worker
[tom@localhost share]$ su
[root@localhost share]# useradd worker
[root@localhost share]# passwd worker
[root@localhost share]# exit
exit
[tom@localhost share]$ su worker
# 查看任务
[worker@localhost share]$ cat task.txt
john: New Project
tom: New Project
# 无话语权
[worker@localhost share]$ echo 'I done!!!' >> task.txt
bash: task.txt: Permission denied
[worker@localhost share]$ exit
exit
[tom@localhost share]$ exit
exit
[john@localhost share]$ exit
exit
[admin@localhost ~]$ su
[root@localhost admin]# userdel -r worker
[root@localhost admin]# userdel -r john
[root@localhost admin]# userdel -r tom
[root@localhost admin]# groupdel boss
[root@localhost admin]# rm -rf /share
sticky bit
# 设置 sticky bit
[admin@localhost share]$ sudo chmod 1777 /share
[admin@localhost share]$ echo hola > hola.txt
[admin@localhost share]$ su john
[john@localhost share]$ rm hola.txt
rm: remove write-protected regular file ‘hola.txt’? y
rm: cannot remove ‘hola.txt’: Operation not permitted
# t 代表 sticky bit
[john@localhost share]$ ll -d
drwxrwxrwt. 2 admin admin 22 Apr 24 02:21 .
[john@localhost share]$ ll
total 4
-rw-rw-r--. 1 admin admin 5 Apr 24 02:21 hola.txt
umark 文件默认属性
umask
umark 用八进制要移除新创建文件的哪些权限(不包括执行权限,新创建文件永远没有执行权限);退出 Shell 后,umask 恢复默认
# 查看 umask 的值
$ umask
0002
# 0002 表示新创建文件,其他人没有写入权限
[admin@localhost ~]$ touch hello.txt
[admin@localhost ~]$ ls -l hello.txt
-rw-rw-r--. 1 admin admin 0 Apr 23 21:47 hello.txt
# 修改 umask 为 0022: 新创建文件,组和其他人没有写入权限
[admin@localhost ~]$ umask 0022
[admin@localhost ~]$ touch hello_2.txt
[admin@localhost ~]$ ls -l
total 0
-rw-r--r--. 1 admin admin 0 Apr 23 21:48 hello_2.txt
-rw-rw-r--. 1 admin admin 0 Apr 23 21:47 hello.txt
# 每次登录 Shell, umask 被重置
$ grep -n umask /etc/profile .bashrc
/etc/profile:55:# By default, we want umask to get set. This sets it for login shell
/etc/profile:60: umask 002
/etc/profile:62: umask 022
# 022 ? 到底发生了什么? 不要这样对我,我还是个孩子 TT
$ head -65 /etc/profile | cat -n
55 # By default, we want umask to get set. This sets it for login shell
56 # Current threshold for system reserved uid/gids is 200
57 # You could check uidgid reservation validity in
58 # /usr/share/doc/setup-*/uidgid file
59 if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
60 umask 002
61 else
62 umask 022
63 fi
一般不会修改 umask,默认值是经过发行组织考虑过的;除非我们想要更加严格的控制权限
进制
二进制:计算机用二进制表示数据
八进制:2^3 = 8 所以,1个八进制可以表示3个二进制
十六进制:2^4 = 16 所以,1个十六进制可以表示4个二进制
用二进制表示的数据多于冗长,为了方便阅读和书写,又出现了八进制和十六进制
切换用户身份
su
# 两条命令作用相同
# -l/- : login shell,载入环境变量并切换到用户 home 目录
$ su -l john
$ su - john
# 以 john 用户身份执行命令 whoami
[admin@localhost ~]$ su -c ‘whoami’ john
john
sudo
sudo 可以限制以其他身份执行的命令,su 执行所有命令
sudo 用用户自己的密码作为验证, su 用其他用户的密码作为验证
sudo 的配置文件为:/etrc/sudoers (man sudoers)
也就是说,Linux(支持多用户同时登录的系统的)普通用户不知道 root 的密码,并且普通用户的超级权限将被限制在合理的范围内
# 列出 admin 可以执行的命令
[admin@localhost ~]$ sudo -l
User admin may run the following commands on localhost:
(ALL) ALL
chown 修改文件的拥有者
执行 chown 需要 root 权限
chown 可以修改 文件/ 目录 的拥有者和组的信息
chown [owner][:[group]] file...
其他命令
chgrp 在以前,使用 chgrp 修改文件的组的信息
passwd 修改用户密码,账号管理
useradd
userdel
groupadd
groupdel
usermod 管理账户信息
占个坑:参考 CentOS 如何添加用户 (2018年4月25日16点04分)
账号管理
# useradd 默认选项
$ sudo useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
# 不建议使用 useradd 设置密码,因为这样密码会被看见
# 创建用户之后,默认账号是锁上的
$ sudo useradd tony
$ sudo passwd -S tony
tony LK 2018-04-23 0 99999 7 -1 (Password locked.)
# 删除密码,即不用密码也可以登录
sudo passwd -d tony
# 将 user 加入组 group_name 中
sudo usermod -aG group_name user
# 创建用户 tony 并加入组 music
sudo useradd -G music tony
# 删除用户及其 home 目录
sudo userdel -r tony
总结
将用户加入组后,可能要重新登录才有效
对于目录来说,x 表示能进入目录,rx 表示能列出目录的内容,w 表示能对目录里面的文件进行新建,重命名,删除文件
umask 设置 要在新建文件中移除的读写权限,新建文件永远没有执行权限
setgid 作用于目录,在目录内创建的 文件/ 目录 的组都将继承被设置 setgid 的目录的组
sticky bit 设置在目录之后,只有拥有者/ 组,root 可以删除里面的文件 / 目录