第二課 Linux用戶和組

本節(jié)主要內(nèi)容

  1. 理解用戶和組的概念
  2. 用戶管理
  3. 組管理
  4. 權(quán)限分配

1. 理解用戶和組的概念

在第一講中我們提到,linux是一種多任務(wù)、多用戶的操作系統(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

這其中便涉及到用戶和組的權(quán)限問(wèn)題,在講這部分內(nèi)容之前呢,先來(lái)理解一下用戶和組的概念

在Linux中,用戶是能夠獲取系統(tǒng)資源的權(quán)限的集合,組是權(quán)限的容器。

Linux用戶類型

用戶類型 描述
管理員 root 具有使用系統(tǒng)所有權(quán)限的用戶,其UID 為0
普通用戶 即一般用戶,其使用系統(tǒng)的權(quán)限受限,其UID為500-60000之間.
系統(tǒng)用戶 保障系統(tǒng)運(yùn)行的用戶,一般不提供密碼登錄系統(tǒng),其UID為1-499之間

與Linux用戶信息相關(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ào)
password :用戶密碼占位符
UID:用戶的ID號(hào)
GID:用戶所在組的ID號(hào)
GECOS:用戶的詳細(xì)信息(如姓名,年齡,電話等)
diretory:用戶所的主目錄
shell:用戶所在的編程環(huán)境

/etc/shadow文件內(nèi)容格式為:

root:$1$.TZS2yur$uQ3.5XLbdEhLkak9HKqZx/:16042:0:99999:7:空白:空白:空白
 1           2                            3   4   5   6  7    8   9
 |           |                            |   |   |   |  |    |   |--保留字段,目前為空
 |           |                            |   |   |   |  |    |--用戶過(guò)期日期(單位/天),此字段指定了用戶作廢的天數(shù)(從1970年的1月1日至今天數(shù))
 |           |                            |   |   |   |  |--在口令過(guò)期之后多少天禁用此用戶
 |           |                            |   |   |   |--提前多少天警告用戶口令將過(guò)期
 |           |                            |   |   |--兩次修改口令間隔最多的天數(shù)
 |           |                            |   |--兩次修改口令間隔最少的天數(shù)
 |           |                            |--上次修改密碼的時(shí)間(單位/天),自1970年01月01日至今天數(shù)
 |           |--用戶密碼(對(duì)應(yīng)/etc/passwd文件內(nèi)的密碼占位符),如果密碼添加“!!”表示禁用該用戶
 |--用戶名(對(duì)應(yīng)/etc/passwd文件內(nèi)的用戶名)
 注:其中字段4、5、6、7、8的值為空時(shí),賬號(hào)可永久使用

Linux用戶組類型

用戶組類型 描述
普通用戶組 可以加入多個(gè)用戶
系統(tǒng)組 一般加入一些系統(tǒng)用戶
私有組(也稱基本組) 當(dāng)創(chuàng)建用戶時(shí),如果沒(méi)有為其指明所屬組,則就為其定義一個(gè)私有的用戶組,起名稱與用戶名同名,當(dāng)把其他用戶加入到該組中,則其就變成了普通組

前面我提到,組是權(quán)限的容器,如普通用戶 user1,user2, user3所屬組group,則它們會(huì)繼承組group的權(quán)限,與group相關(guān)的文件包括/etc/group /etc/gshadow

下面給出的是/etc/group文件內(nèi)容及格式含義

root:x:0:root
 |   | |  |--額外組(可以多個(gè)用“,”隔開(kāi))
 |   | |--組ID(GID)
 |   |--組密碼占位符
 |--組名

下面給出的是/etc/gshadow文件內(nèi)容及格式含義

root:空白:空白:root
 |    |    |    |--以逗號(hào)分隔的小組成員
 |    |    |--以逗號(hào)分隔的組管理員
 |    |--加密的密碼
 |--組名

2. 用戶管理

Linux中的用戶管理主要涉及到用戶賬號(hào)的添加、刪除和修改。所有操作都影響/etc/passwd中的文件內(nèi)容

(1)添加用戶 useradd

useradd [options] username
options:
    1.-u :UID
    2.-g :GID
    3.-d :指定用戶家目錄,默認(rèn)是/home/username
    4.-s :指定用戶所在的shell環(huán)境
    5.-G:指定用戶的附加組

例子:
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)行用戶john密碼的修改
xtwy@ubuntu:~$ sudo passwd john
[sudo] password for xtwy: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

注銷用戶,重新到登錄界面時(shí),便會(huì)出現(xiàn)john用戶的登錄選項(xiàng)


image.png

(2)修改用戶 usermod

usermod  [options] username
options:
    1.-u :UID
    2.-g :GID
    3.-d :指定用戶家目錄,默認(rèn)是/home/username
            -m 與-b 一起用表示把用戶家目錄的內(nèi)容也移走
    4.-s :指定用戶所在的shell環(huán)境
    5.-G:指定用戶的附加組
 使用示例:
 //將前面的/bin/sh,改/bin/bash
 root@ubuntu:/home/john# usermod -s /bin/bash john

(3)刪除用戶 userdel

刪除用戶時(shí),用戶不能處于登錄狀態(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è)用戶都有一個(gè)用戶組,沒(méi)有指定時(shí)都默認(rèn)為私有組,私有組名同用戶名一致,建立用戶組的好處是系統(tǒng)能對(duì)一個(gè)用戶組中的所有用戶的操作權(quán)限進(jìn)行集中管理。組管理涉及組的添加、刪除和修改。組的增加、刪除和修改實(shí)際上就對(duì)/etc/group文件的更新。

(1)新增用戶組

groupadd [options] 用戶組名
options:
 1 -g GID:指定新用戶組的組標(biāo)識(shí)號(hào)(GID)。

使用示例:
//創(chuàng)建用戶組前的/etc/group文件內(nèi)容
oot@ubuntu:/home# tail /etc/group
.....................
xtwy:x:1000:
sambashare:x:122:xtwy
//使用默認(rèn)增加用戶組
root@ubuntu:/home# groupadd john
//添加用戶組后得到的/etc/group文件內(nèi)容
//可以看到默認(rèn)用戶組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)修改用戶組

groupmod [options] 用戶組名
options:
 1 -g GID: 為用戶組指定新的組標(biāo)識(shí)號(hào)。
 2 -n : 將用戶組的名字改為新名字

使用示例:
root@ubuntu:/home# groupmod -g 1002 -n john2 john1
root@ubuntu:/home# tail /etc/group
.....................
john:x:1001:
john2:x:1002:

(3)刪除用戶組

groupdel 用戶組名

使用示例:
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)看前半部分


image.png

首先是文件類型,-表示文本文件,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)

后面緊跟著的是用戶權(quán)限、組權(quán)限及其它權(quán)限,其中r表示讀權(quán)限,w表示寫權(quán)限,x表示可執(zhí)行權(quán)限,

再后面的數(shù)字表示的是鏈接數(shù),這個(gè)放到下一節(jié)中介紹
緊接著是文件或目錄的所屬者,所屬用戶組,文件大?。ㄗ止?jié)數(shù)),文件最后訪問(wèn)時(shí)間,文件名。

下面具體說(shuō)明:

//hello1.txt是一個(gè)普通文件
//root用戶具有讀寫權(quán)限,但不能執(zhí)行
//用戶組root具有讀權(quán)限,無(wú)寫和執(zhí)行權(quán)限
//其它用戶只有讀權(quán)限,無(wú)寫和執(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è)目錄
//用戶xtwy具有讀寫和執(zhí)行權(quán)限,這里面的執(zhí)行權(quán)限x表示可以訪問(wèn)目錄
//用用組xtwy具有讀和執(zhí)行權(quán)限,無(wú)寫權(quán)限
//其它用戶具有讀和執(zhí)行權(quán)限,無(wú)寫權(quán)限
drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop

(2)修改文件或目錄權(quán)限

1 增加權(quán)限

//chmod命令,a表示所有,包括用戶、組及其它用戶都有增加寫權(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)前用戶增加權(quán)限

//不加all表示作用于當(dāng)前用戶
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ù)字方式

讀、寫、運(yùn)行三項(xiàng)權(quán)限可以用數(shù)字表示,就是r=4,w=2,x=1

//用戶具有讀寫權(quán)限
//用戶組和其它用戶具有執(zhí)行權(quán)限,無(wú)讀寫權(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 改變用戶 chown

將root擁有改xtwy用戶擁有

-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 改變用戶組 chgrp

-rw---x--x 1 xtwy root   30 2015-08-22 17:28 hello1.txt
xtwy@ubuntu:~$ chgrp xtwy hello1.txt
-rw---x--x 1 xtwy xtwy   47 2015-08-22 23:43 hello1.txt

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容