保存用戶信息的文件:
- 賬號(hào):
/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
第一個(gè)字符代表文件類型:
-
-
: 普通文件 -
d
:目錄 -
l
:鏈接文件盟蚣,鏈接文件的屬性總是lrwxrwxrwx.
,真正的權(quán)限要看被鏈接文件的屬性 -
c
:character 代表以字符(數(shù)據(jù)流)形式處理數(shù)據(jù)的設(shè)備赶盔;比如:終端布蔗, Modem -
b
:block, 代表以塊(block)形式處理數(shù)據(jù)的設(shè)備友驮;比如:硬盤疼鸟,CD-ROM
權(quán)限
權(quán)限:model
/ permission
修改權(quán)限
用命令 chmod
修改權(quán)限殴蓬,有2中方式:
使用八進(jìn)制
# 4 2 1
# r w x
# 如果需要讀寫權(quán)限,只要設(shè)置為 6 (4+2) 即可
# 創(chuàng)建文件
$ echo hello > hello.txt
$ ls -l
-rw-rw-r--. 1 admin admin 6 Apr 23 20:48 hello.txt
# 去掉寫入權(quán)限
$ 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
#-------------------------------------------------------#
# 添加寫入權(quán)限
$ 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
使用標(biāo)識(shí)修改權(quán)限
使用方法
chmod [who]<operator><permission>[,[who]<operator><permission>]
who
u: user 默認(rèn)狂窑,可省略
g: group
o: other
a: all
operator
+: 添加權(quán)限
-: 去掉權(quán)限
=:設(shè)置權(quán)限
perssion
r
w
x
例如
$ ls -l
-rw-------. 1 admin admin 12 Apr 23 20:50 hello.txt
# a (all) 添加讀寫權(quán)限
$ chmod a+rw hello.txt
$ ls -l
-rw-rw-rw-. 1 admin admin 12 Apr 23 20:50 hello.txt
# g (group) 去掉組的讀寫權(quán)限
$ chmod g-rw hello.txt
$ ls -l
-rw----rw-. 1 admin admin 12 Apr 23 20:50 hello.txt
# o (other) 去掉其他人的讀寫權(quán)限
$ chmod o-rw hello.txt
$ ls -l
-rw-------. 1 admin admin 12 Apr 23 20:50 hello.txt
權(quán)限和文件備份
備份配置文件媳板,將備份文件設(shè)為只讀,避免被修改
$ ls -l
total 4
-rw-r--r--. 1 admin admin 970 Apr 23 20:27 yum.conf
# 備份源文件泉哈,并設(shè)置為只讀
$ 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~
備份配置文件蛉幸,將備份文件放入無寫入權(quán)限的目錄,避免被誤刪
$ ls
yum.conf
# 由于文件的刪除(移動(dòng)/重命名)操作權(quán)限在目錄設(shè)置丛晦,可以新建一個(gè)目錄
# 并將目錄的寫入權(quán)限去掉奕纫,避免誤刪
$ 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
特殊權(quán)限
-
setuid
: 4000 【賦執(zhí)行者予命令創(chuàng)建者權(quán)限】(設(shè)置在可執(zhí)行文件上) -
setgid
: 2000 【設(shè)置在目錄上,在目錄中創(chuàng)建的文件的組的值繼承目錄的組的值】 -
sticky bit
: 1000 【設(shè)置在目錄上烫沙,只有文件擁有者若锁,組或創(chuàng)建用戶可以刪除/修改文件,即使目錄權(quán)限為 1777】 斧吐;比如:/tmp
real user
: 執(zhí)行命令的人
effective user
:命令執(zhí)行時(shí)用的身份
比如:setuid
會(huì)改變 effective user
為文件創(chuàng)建者
或者,sudo 的 real user
為執(zhí)行者仲器,而 effective user
為 root
setuid
比如煤率,普通用戶可以執(zhí)行 passwd
,但資源 /etc/shadow
只有 root 才能訪問乏冀;為 passwd
設(shè)置 setuid
后:普通用于將以 root 身份執(zhí)行 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
實(shí)驗(yàn)失敗蝶糯,可能是因?yàn)?https://unix.stackexchange.com/questions/166817/using-the-setuid-bit-properly
setgid
當(dāng) setgid
設(shè)置在目錄時(shí),在目錄中新創(chuàng)建文件的組的值會(huì)繼承目錄的組的值(默認(rèn)新創(chuàng)建的文件的擁有者和組都是創(chuàng)建者)-- 此特性可以用于共享目錄
需求:創(chuàng)建一個(gè)共享目錄辆沦,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)
# 創(chuàng)建共享目錄
[root@localhost ~]# mkdir /share
# 設(shè)置 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
# 老板委派任務(wù)
[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
# 查看任務(wù)
[worker@localhost share]$ cat task.txt
john: New Project
tom: New Project
# 無話語權(quán)
[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
# 設(shè)置 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 文件默認(rèn)屬性
umark
用八進(jìn)制要移除新創(chuàng)建文件的哪些權(quán)限(不包括執(zhí)行權(quán)限妒茬,新創(chuàng)建文件永遠(yuǎn)沒有執(zhí)行權(quán)限);退出 Shell 后蔚晨,umask
恢復(fù)默認(rèn)
# 查看 umask 的值
$ umask
0002
# 0002 表示新創(chuàng)建文件乍钻,其他人沒有寫入權(quán)限
[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: 新創(chuàng)建文件肛循,組和其他人沒有寫入權(quán)限
[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 ? 到底發(fā)生了什么? 不要這樣對我,我還是個(gè)孩子 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
一般不會(huì)修改 umask银择,默認(rèn)值是經(jīng)過發(fā)行組織考慮過的多糠;除非我們想要更加嚴(yán)格的控制權(quán)限
進(jìn)制
- 二進(jìn)制:計(jì)算機(jī)用二進(jìn)制表示數(shù)據(jù)
- 八進(jìn)制:
2^3 = 8
所以,1個(gè)八進(jìn)制可以表示3個(gè)二進(jìn)制 - 十六進(jìn)制:
2^4 = 16
所以浩考,1個(gè)十六進(jìn)制可以表示4個(gè)二進(jìn)制
用二進(jìn)制表示的數(shù)據(jù)多于冗長夹孔,為了方便閱讀和書寫,又出現(xiàn)了八進(jìn)制和十六進(jìn)制
切換用戶身份
su
# 兩條命令作用相同
# -l/- : login shell析孽,載入環(huán)境變量并切換到用戶 home 目錄
$ su -l john
$ su - john
# 以 john 用戶身份執(zhí)行命令 whoami
[admin@localhost ~]$ su -c ‘whoami’ john
john
sudo
-
sudo
可以限制以其他身份執(zhí)行的命令搭伤,su
執(zhí)行所有命令 -
sudo
用用戶自己的密碼作為驗(yàn)證,su
用其他用戶的密碼作為驗(yàn)證 -
sudo
的配置文件為:/etrc/sudoers
(man sudoers)
也就是說绿淋,Linux(支持多用戶同時(shí)登錄的系統(tǒng)的)普通用戶不知道 root 的密碼闷畸,并且普通用戶的超級(jí)權(quán)限將被限制在合理的范圍內(nèi)
# 列出 admin 可以執(zhí)行的命令
[admin@localhost ~]$ sudo -l
User admin may run the following commands on localhost:
(ALL) ALL
chown 修改文件的擁有者
執(zhí)行
chown
需要 root 權(quán)限
chown
可以修改 文件/ 目錄 的擁有者和組的信息
chown [owner][:[group]] file...
其他命令
-
chgrp
在以前,使用chgrp
修改文件的組的信息 -
passwd
修改用戶密碼吞滞,賬號(hào)管理
useradd
userdel
groupadd
groupdel
-
usermod
管理賬戶信息
占個(gè)坑:參考 CentOS 如何添加用戶 (2018年4月25日16點(diǎn)04分)
賬號(hào)管理
# useradd 默認(rèn)選項(xiàng)
$ sudo useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
# 不建議使用 useradd 設(shè)置密碼佑菩,因?yàn)檫@樣密碼會(huì)被看見
# 創(chuàng)建用戶之后,默認(rèn)賬號(hào)是鎖上的
$ 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
# 創(chuàng)建用戶 tony 并加入組 music
sudo useradd -G music tony
# 刪除用戶及其 home 目錄
sudo userdel -r tony
總結(jié)
- 將用戶加入組后殿漠,可能要重新登錄才有效
- 對于目錄來說,
x
表示能進(jìn)入目錄佩捞,rx
表示能列出目錄的內(nèi)容绞幌,w
表示能對目錄里面的文件進(jìn)行新建,重命名一忱,刪除文件 -
umask
設(shè)置 要在新建文件中移除的讀寫權(quán)限莲蜘,新建文件永遠(yuǎn)沒有執(zhí)行權(quán)限 -
setgid
作用于目錄,在目錄內(nèi)創(chuàng)建的 文件/ 目錄 的組都將繼承被設(shè)置 setgid 的目錄的組 -
sticky bit
設(shè)置在目錄之后帘营,只有擁有者/ 組票渠,root 可以刪除里面的文件 / 目錄