本節(jié)主要內(nèi)容
- 理解用戶(hù)和組的概念
- 用戶(hù)管理
- 組管理
- 權(quán)限分配
1. 理解用戶(hù)和組的概念
在第一講中我們提到等脂,linux是一種多任務(wù)主守、多用戶(hù)的操作系統(tǒng)胯陋,在講ls -l命令行我們看到如下文件詳細(xì)信息:
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos
這其中便涉及到用戶(hù)和組的權(quán)限問(wèn)題老赤,在講這部分內(nèi)容之前呢蟀拷,先來(lái)理解一下用戶(hù)和組的概念
在Linux中,用戶(hù)是能夠獲取系統(tǒng)資源的權(quán)限的集合允粤,組是權(quán)限的容器崭倘。
Linux用戶(hù)類(lèi)型
用戶(hù)類(lèi)型 | 描述 |
---|---|
管理員 root | 具有使用系統(tǒng)所有權(quán)限的用戶(hù),其UID 為0 |
普通用戶(hù) | 即一般用戶(hù),其使用系統(tǒng)的權(quán)限受限,其UID為500-60000之間. |
系統(tǒng)用戶(hù) | 保障系統(tǒng)運(yùn)行的用戶(hù),一般不提供密碼登錄系統(tǒng),其UID為1-499之間 |
與Linux用戶(hù)信息相關(guān)的文件有兩個(gè),分別是/etc/passwd类垫, /etc/shadow
/etc/passwd文件內(nèi)容如下:
root@ubuntu:/home/xtwy# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:2:2:bin:/bin:/bin/sh
........................
xtwy:x:1000:1000:Ubuntu-10.04,,,:/home/xtwy:/bin/bash
/etc/passwd文件內(nèi)容格式為:
account:password:UID:GID:GECOS:diretory:shell
account: 用戶(hù)名或帳號(hào)
password :用戶(hù)密碼占位符
UID:用戶(hù)的ID號(hào)
GID:用戶(hù)所在組的ID號(hào)
GECOS:用戶(hù)的詳細(xì)信息(如姓名司光,年齡,電話(huà)等)
diretory:用戶(hù)所的主目錄
shell:用戶(hù)所在的編程環(huán)境
/etc/shadow文件內(nèi)容格式為:
root:$1$.TZS2yur$uQ3.5XLbdEhLkak9HKqZx/:16042:0:99999:7:空白:空白:空白
1 2 3 4 5 6 7 8 9
| | | | | | | | |--保留字段悉患,目前為空
| | | | | | | |--用戶(hù)過(guò)期日期(單位/天)残家,此字段指定了用戶(hù)作廢的天數(shù)(從1970年的1月1日至今天數(shù))
| | | | | | |--在口令過(guò)期之后多少天禁用此用戶(hù)
| | | | | |--提前多少天警告用戶(hù)口令將過(guò)期
| | | | |--兩次修改口令間隔最多的天數(shù)
| | | |--兩次修改口令間隔最少的天數(shù)
| | |--上次修改密碼的時(shí)間(單位/天),自1970年01月01日至今天數(shù)
| |--用戶(hù)密碼(對(duì)應(yīng)/etc/passwd文件內(nèi)的密碼占位符)售躁,如果密碼添加“!!”表示禁用該用戶(hù)
|--用戶(hù)名(對(duì)應(yīng)/etc/passwd文件內(nèi)的用戶(hù)名)
注:其中字段4坞淮、5、6陪捷、7回窘、8的值為空時(shí),賬號(hào)可永久使用
Linux用戶(hù)組類(lèi)型
用戶(hù)組類(lèi)型 | 描述 |
---|---|
普通用戶(hù)組 | 可以加入多個(gè)用戶(hù) |
系統(tǒng)組 | 一般加入一些系統(tǒng)用戶(hù) |
私有組(也稱(chēng)基本組) | 當(dāng)創(chuàng)建用戶(hù)時(shí),如果沒(méi)有為其指明所屬組,則就為其定義一個(gè)私有的用戶(hù)組,起名稱(chēng)與用戶(hù)名同名市袖,當(dāng)把其他用戶(hù)加入到該組中,則其就變成了普通組 |
前面我提到啡直,組是權(quán)限的容器,如普通用戶(hù) user1,user2, user3所屬組group,則它們會(huì)繼承組group的權(quán)限苍碟,與group相關(guān)的文件包括/etc/group /etc/gshadow
下面給出的是/etc/group文件內(nèi)容及格式含義
root:x:0:root
| | | |--以逗號(hào)分隔的小組成員
| | |--組ID(GID)
| |--組密碼占位符
|--組名
下面給出的是/etc/gshadow文件內(nèi)容及格式含義
root:空白:空白:root
| | | |--以逗號(hào)分隔的小組成員
| | |--以逗號(hào)分隔的組管理員
| |--加密的密碼
|--組名
2. 用戶(hù)管理
Linux中的用戶(hù)管理主要涉及到用戶(hù)賬號(hào)的添加酒觅、刪除和修改。所有操作都影響/etc/passwd中的文件內(nèi)容
(1)添加用戶(hù) useradd
useradd [options] username
options:
1.-u :UID
2.-g :GID
3.-d :指定用戶(hù)家目錄微峰,默認(rèn)是/home/username
4.-s :指定用戶(hù)所在的shell環(huán)境
5.-G:指定用戶(hù)的附加組
例子:
root@ubuntu:/home/xtwy# useradd -u 1988 -g 1000 john
root@ubuntu:/home/xtwy# tail -1 /etc/passwd
john:x:1988:1000::/home/john:/bin/sh
添加完成后可以采用下列命令進(jìn)行用戶(hù)john密碼的修改
xtwy@ubuntu:~$ sudo passwd john
[sudo] password for xtwy:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
注銷(xiāo)用戶(hù)舷丹,重新到登錄界面時(shí),便會(huì)出現(xiàn)john用戶(hù)的登錄選項(xiàng)
(2)修改用戶(hù) usermod
usermod [options] username
options:
1.-u :UID
2.-g :GID
3.-d :指定用戶(hù)家目錄蜓肆,默認(rèn)是/home/username
-m 與-b 一起用表示把用戶(hù)家目錄的內(nèi)容也移走
4.-s :指定用戶(hù)所在的shell環(huán)境
5.-G:指定用戶(hù)的附加組
使用示例:
//將前面的/bin/sh颜凯,改/bin/bash
root@ubuntu:/home/john# usermod -s /bin/bash john
(3)刪除用戶(hù) userdel
刪除用戶(hù)時(shí),用戶(hù)不能處于登錄狀態(tài):
userdel [options]username
options
1.-r :連同主目錄一起刪除
使用示例:
root@ubuntu:/home/xtwy# userdel -r john
root@ubuntu:/home/xtwy# cd ..
root@ubuntu:/home# ls
3. 組管理
前面我們知道症杏,組是權(quán)限的集合装获。在linux系統(tǒng)中,每個(gè)用戶(hù)都有一個(gè)用戶(hù)組厉颤,沒(méi)有指定時(shí)都默認(rèn)為私有組穴豫,私有組名同用戶(hù)名一致,建立用戶(hù)組的好處是系統(tǒng)能對(duì)一個(gè)用戶(hù)組中的所有用戶(hù)的操作權(quán)限進(jìn)行集中管理。組管理涉及組的添加精肃、刪除和修改秤涩。組的增加、刪除和修改實(shí)際上就對(duì)/etc/group文件的更新司抱。
(1)新增用戶(hù)組
groupadd [options] 用戶(hù)組名
options:
1 -g GID:指定新用戶(hù)組的組標(biāo)識(shí)號(hào)(GID)筐眷。
使用示例:
//創(chuàng)建用戶(hù)組前的/etc/group文件內(nèi)容
oot@ubuntu:/home# tail /etc/group
.....................
xtwy:x:1000:
sambashare:x:122:xtwy
//使用默認(rèn)增加用戶(hù)組
root@ubuntu:/home# groupadd john
//添加用戶(hù)組后得到的/etc/group文件內(nèi)容
//可以看到默認(rèn)用戶(hù)組gid是自增長(zhǎng)的
root@ubuntu:/home# tail /etc/group
.................
xtwy:x:1000:
sambashare:x:122:xtwy
john:x:1001:
//指定gid
root@ubuntu:/home# groupadd -g 1011 john1
root@ubuntu:/home# tail /etc/group
................
john:x:1001:
john1:x:1011:
(2)修改用戶(hù)組
groupmod [options] 用戶(hù)組名
options:
1 -g GID: 為用戶(hù)組指定新的組標(biāo)識(shí)號(hào)。
2 -n : 將用戶(hù)組的名字改為新名字
使用示例:
root@ubuntu:/home# groupmod -g 1002 -n john2 john1
root@ubuntu:/home# tail /etc/group
.....................
john:x:1001:
john2:x:1002:
(3)刪除用戶(hù)組
groupdel 用戶(hù)組名
使用示例:
root@ubuntu:/home# groupdel john2
root@ubuntu:/home# tail /etc/group
....................
xtwy:x:1000:
sambashare:x:122:xtwy
john:x:1001:
4. 權(quán)限分配
(1)權(quán)限說(shuō)明
在本節(jié)第一節(jié)习柠,我們提到ls -l命令顯示的文件或目錄詳細(xì)信息具有如下格式
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
現(xiàn)在我們對(duì)ls -l顯示的內(nèi)容進(jìn)行分解匀谣,首先來(lái)看前半部分
首先是文件類(lèi)型,-表示文本文件资溃,d表示目錄武翎,除此之外還有下列幾種文件(不常見(jiàn)):
code | File Type |
---|---|
- | Standard file |
d | Standard directory |
l | Symbolic link (a shortcut to another file) |
s | Socket (a file designed to send and receive data over a network) |
c | Character device (a hardware device driver, usually found in /dev) |
b | Block device (a hardware device driver, usually found in /dev) |
后面緊跟著的是用戶(hù)權(quán)限、組權(quán)限及其它權(quán)限溶锭,其中r表示讀權(quán)限宝恶,w表示寫(xiě)權(quán)限,x表示可執(zhí)行權(quán)限趴捅,
再后面的數(shù)字表示的是鏈接數(shù)垫毙,這個(gè)放到下一節(jié)中介紹
緊接著是文件或目錄的所屬者,所屬用戶(hù)組拱绑,文件大凶劢妗(字節(jié)數(shù)),文件最后訪(fǎng)問(wèn)時(shí)間欺栗,文件名毫痕。
下面具體說(shuō)明:
//hello1.txt是一個(gè)普通文件
//root用戶(hù)具有讀寫(xiě)權(quán)限征峦,但不能執(zhí)行
//用戶(hù)組root具有讀權(quán)限迟几,無(wú)寫(xiě)和執(zhí)行權(quán)限
//其它用戶(hù)只有讀權(quán)限,無(wú)寫(xiě)和執(zhí)行權(quán)限
-rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
//下面的代碼演示了前面的內(nèi)容
xtwy@ubuntu:~$ ls
Desktop Downloads hello1.txt literature Pictures Templates
Documents examples.desktop hello.txt Music Public Videos
xtwy@ubuntu:~$ more hello1.txt
hello linux
hello linux linux
xtwy@ubuntu:~$ echo "test permission" >> hello1.txt
bash: hello1.txt: Permission denied
//Desktop是一個(gè)目錄
//用戶(hù)xtwy具有讀寫(xiě)和執(zhí)行權(quán)限栏笆,這里面的執(zhí)行權(quán)限x表示可以訪(fǎng)問(wèn)目錄
//用用組xtwy具有讀和執(zhí)行權(quán)限类腮,無(wú)寫(xiě)權(quán)限
//其它用戶(hù)具有讀和執(zhí)行權(quán)限,無(wú)寫(xiě)權(quán)限
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
(2)修改文件或目錄權(quán)限
1 增加權(quán)限
//chmod命令蛉加,a表示所有蚜枢,包括用戶(hù)、組及其它用戶(hù)都有增加寫(xiě)權(quán)限
root@ubuntu:/home/xtwy# chmod a+w hello1.txt
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-rw-rw-rw- 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos
2 減小權(quán)限
//減小權(quán)限针饥,用減號(hào)表示
root@ubuntu:/home/xtwy# chmod a-w hello1.txt
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-r--r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos
3 給當(dāng)前用戶(hù)增加權(quán)限
//不加all表示作用于當(dāng)前用戶(hù)
root@ubuntu:/home/xtwy# chmod +w hello1.txt
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos
4 靈活設(shè)置權(quán)限厂抽,采用數(shù)字方式
讀、寫(xiě)丁眼、運(yùn)行三項(xiàng)權(quán)限可以用數(shù)字表示筷凤,就是r=4,w=2,x=1
//用戶(hù)具有讀寫(xiě)權(quán)限
//用戶(hù)組和其它用戶(hù)具有執(zhí)行權(quán)限,無(wú)讀寫(xiě)權(quán)限
root@ubuntu:/home/xtwy# chmod 611 hello1.txt
root@ubuntu:/home/xtwy# ls -l
total 48
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
-rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
-rw---x--x 1 root root 30 2015-08-22 17:28 hello1.txt
-rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos
5 改變用戶(hù) chown
將root擁有改xtwy用戶(hù)擁有
-rw---x--x 1 root root 30 2015-08-22 17:28 hello1.txt
root@ubuntu:/home/xtwy# chown xtwy hello1.txt
-rw---x--x 1 xtwy root 30 2015-08-22 17:28 hello1.txt
//修改完成后可以對(duì)文件中的內(nèi)容進(jìn)行修改
root@ubuntu:/home/xtwy# su xtwy
xtwy@ubuntu:~$ echo "test permissions" >> hello1.txt
6 改變用戶(hù)組 chgrp
-rw---x--x 1 xtwy root 30 2015-08-22 17:28 hello1.txtxtwy@ubuntu:~$ chgrp xtwy hello1.txt
-rw---x--x 1 xtwy xtwy 47 2015-08-22 23:43 hello1.txt