Linux下的使用者許可權

2021-08-01 20:04:59 字數 4437 閱讀 3395

linux是乙個多使用者、多使用者的作業系統。乙個系統同時可能有幾個使用者訪問系統,如果我們給這些使用者同乙個使用者名稱去登入系統,可能會帶來不便,因為不同的使用者有不同的需求,例如你自己總有一些資訊不想讓他人看到,或者有些個性化的系統設定,還有作為系統的管理員想限制一些使用者的訪問等,為不同的使用者建立不同的賬號,賦予不同的許可權,這是為例計算機安全。

那麼linux是通過是通過什麼來區分使用者的?

linux是通過uid來標示使用者的,root(超級管理員)的uid是0,普通使用者的uid是1-65535,系統使用者uid:1-499(centos6),1-999(centos7),這個是對守護程序獲取資源進行許可權分配;登入使用者uid:500+(centos6),1000+(centos7),用於互動式登入用。

使用者組是具有相同特徵的使用者集合體;例如我們有時需要讓多個使用者具有相同的許可權,比如檢視、修改某個檔案或者目錄,或者有些檔案只能看不能修改等,我們可以建立個滿足這個要求的組,然後把這些使用者加到組裡去,這樣批量管理使用者許可權,而不用針對每個使用者賦予相應的許可權。

linux是通過gid來標示組的,root組(組管理員)的gid是0,普通使用者的gid是1-65535,系統使用者gid:1-499(centos6),1-999(centos7);登入使用者gid:500+(centos6),1000+(centos7).

首先,我們先看下下面這個例子。

-rw-r--r--. 1 miouqi miouqi 173 may 25

上面例子解析:

從左到右每項:

【許可權屬性列表】【引用計數】【所有者】【所屬組】【大小】 【建立時間】【檔名】

檔案是存放實際資料的所在,目錄主要的內容是記錄檔名列表,檔名與目錄有強烈的關聯。

r(讀):可以檢視此目錄中檔案列表,不能訪問檔案(即不能檢視目錄下的檔案,不能刪除目錄下的檔案(此目錄屬於使用者的家目錄除外),不能進入(cd)目錄);

例1:用root賬號為普通使用者xh建立如下的實驗環境

[root@centos6 tmp]# ls -al test/

total 8

dr--------. 2 xh xh 4096 may 30 14:13 .

drwxrwxrwt. 32 root root 4096 may 30 14:10 ..

-rw-r--r--. 1 xh xh 0 may 30 14:13 a

-rw-r--r--. 1 xh xh 0 may 30 14:13 b

-rw-r--r--. 1 xh xh 0 may 30 14:13 c

用xh的賬號登入linux系統,對資料夾test及其目錄下檔案進行如下操作:

[xh@centos6 ~]$ ls -al /tmp/test

ls: cannot access /tmp/test/c: permission denied

ls: cannot access /tmp/test/..: permission denied

ls: cannot access /tmp/test/.: permission denied

ls: cannot access /tmp/test/a: permission denied

ls: cannot access /tmp/test/b: permission denied

total 0

d????????? ? ? ? ? ? .

d????????? ? ? ? ? ? ..

-????????? ? ? ? ? ? a

-????????? ? ? ? ? ? b

-????????? ? ? ? ? ? c

[xh@centos6 ~]$ cd /tmp/test

-bash: cd: /tmp/test: permission denied

[xh@centos6 ~]$ rm /tmp/test/a

rm: cannot remove `/tmp/test/a': permission denied

w(寫):可以讓使用者刪除、更新、新建檔案或目錄(這些實現的前置條件是必須有x許可權);

例2:對例1的實驗環境修改,用root賬號將test目錄的許可權通過chmod u=w /tmp/test命令修改,其他不變

[xh@centos6 ~]$ ls -al /tmp/test

ls: cannot open directory /tmp/test: permission denied

[xh@centos6 ~]$ cd /tmp/test

-bash: cd: /tmp/test: permission denied

[xh@centos6 ~]$ rm -r /tmp/test/a

rm: cannot remove `/tmp/test/a': permission denied

[xh@centos6 ~]$ rename /tmp/test/a /tmp/test/f

[xh@centos6 ~]$ mv /tmp/test/a /tmp/a

mv: cannot stat `/tmp/test/a': permission denied

x:可以使用ls -l檢視此目錄中檔案列表,也可以cd進入此目錄(執行許可權是基本許可權);

例3:對例1的實驗環境修改,用root賬號將test目錄的許可權通過chmod u=wx /tmp/test命令修改,其他不變,

[xh@centos6 ~]$ cd /tmp/test

[xh@centos6 test]$ ls

ls: cannot open directory .:

permission denied

[xh@centos6 test]$ cat a

abc[xh@centos6 test]$ echo 1234 | tee a

1234

[xh@centos6 test]$ rm a

[xh@centos6 test]$ cat a

cat:

a:no such file or directory

[xh@centos6 test]$ mv b ../

[xh@centos6 test]$ cd ..

[xh@centos6 tmp]$ ls -ld b

-rw-r--r--. 1 xh xh 0

may3014:

13 b

x(大寫x):只給目錄x許可權,不給檔案x許可權(與-r配合使用,針對目錄及其子目錄增加x許可權,對檔案無執行的檔案,不會增加x許可權,任意三種人有執行許可權,也會增加x許可權)

例4:用root賬號為普通使用者xh建立如下的實驗環境

[root@centos6 tmp]# ls -al test

total 12

drwx------. 3

xhxh

4096 may 30

15:09 .

drwxrwxrwt. 32 root root 4096 may 30

15:01 ..

-rwxr--r--. 1

xhxh

0 may 30

15:06 a

-rw-r--r--. 1

xhxh

0 may 30

15:06 b

-rw-r--r-x. 1

xhxh

0 may 30

14:13 c

drw-r--r--. 2

xhxh

4096 may 30

15:09 dir

用xh的賬號登入linux系統,對資料夾test及其目錄下檔案進行如下操作:

[root@centos6 ~]# chmod -r g+x /tmp/test/

[root@centos6 ~]# ls -al /tmp/test/

total 12

drwx--x---. 3

xhxh

4096 may 30

15:09 .

drwxrwxrwt. 32 root root 4096 may 30

15:01 ..

-rwxr-xr--. 1

xhxh

0 may 30

15:06 a

-rw-r--r--. 1

xhxh

0 may 30

15:06 b

-rw-r-xr-x. 1

xhxh

0 may 30

14:13 c

drw-r-xr--. 2

xhxh

4096 may 30

15:09 dir

Linux下修改新建使用者許可權

linux下使用useradd增加使用者,如果沒有特別的指定,該使用者指定組名也是使用者名稱,並且為一般使用者 沒有root使用者的許可權 不能ssh遠端登入。xuwangcheng14 root useradd test2 xuwangcheng14 root id test2 uid 1009 ...

Linux下賦予使用者su許可權

首先,將使用者加入wheel組。usermod g wheel liuqiang 然後,修改 etc pam.d su檔案。兩種su許可權設定方式 1.auth required pam wheel.so use uid 這行去掉注釋的話,那麼只有wheel 組的人su後,輸入正確的root密碼才能...

Linux下使用者及檔案許可權

使用sudo,需要當前使用者屬於sudo組,且需要輸入當前使用者的密碼,su 命令可以切換使用者組,但是同時使用者的環境變數和工作目錄也會跟著改變成目標使用者所對應的。新建乙個叫小明的使用者 sudo adduser xiaoming 這個命令不但可以新增使用者到系統,同時也會預設為新使用者在 ho...