當前位置:編程學習大全網 - 編程語言 - Linux操作系統文件訪問權限詳解

Linux操作系統文件訪問權限詳解

壹直以root登陸使用linux的人來說很少有權限被拒這種概念,但某些時候又深受權限拒絕困擾。

知道為什麽很多程序中需要使用getuid(),setuid()?為什麽以普通權限登陸的用戶不能進入/root,為什麽在/目錄下執行ls -l後可以顯示root的信息,但ls /root -al卻是權限不夠?為什麽有些文件夾可以繼續創建文件,但就是不能ls?等等,相信看了此文就能明白。

主要是學習筆記,不足之處請指正。

CentOS 5.4 [testc@xxx opt]$ uname -a Linux xxx 2.6.18-164.el5xen #1 SMP Thu Sep 3 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux

壹、口令文件1,格式存儲文件/etc/passwd,格式如下:root:x:0:0:root:/root:/bin/bash aaa:x:501:501:bj, bj, 8111111,136000111:/home/aaa:/bin/bash用戶名:加密密碼:用戶ID:組ID:註釋:工作目錄:shell:

默認情況是第壹行的格式;註釋字段可以自行修改,用逗號隔開,如第二行格式,這主要是給finger命令使用時可解析。

可以vi /etc/passwd修改,但為了保證其格式的正確性,請用vipw命令編譯此文件。

sh-3.2# finger aaa Login: aaa Name: bj Directory: /home/aaa Shell: /bin/bash Office: bj, 8111111 Home Phone: 136000111 Never logged in. No mail. No Plan.

2,編程實例

/*getpwnam_pwuid.c*/ #include #include #include

int main(void)

{ //struct passwd *pwd = getpwnam("aaa");struct passwd *pwd = getpwuid(501);if(pwd == NULL)

{ printf("err.\n");return 1;}

printf("name:%s\n", pwd->pw_name);printf("passwd:%s\n", pwd->pw_passwd);printf("description:%s\n", pwd->pw_gecos);printf("uid:%d\n", pwd->pw_uid);printf("gid:%d\n", pwd->pw_gid);printf("dir:%s\n", pwd->pw_dir);printf("shell:%s\n", pwd->pw_shell);

return 0;}

sh-3.2# gcc getpwnam_pwuid.c -o app sh-3.2# ./app name:aaa passwd:x description:bj, bj, 8111111,136000111 uid:501 gid:501 dir:/home/aaa shell:/bin/bash

二、組文件1,格式存儲文件/etc/group,格式如下root:x:0:root bin:x:1:root,bin,daemon aaa:x:501:組名:加密密碼:組ID:指向的各用戶名

2,改變文件uid和gid.

sh-3.2# pwd /root/study sh-3.2# ls -al -rw-r——r—— 1 root root 397 10-11 03:23 test.c

chgrp 改變所屬組ID,當然只有root權限才可以修改。

sh-3.2# chgrp aaa test.c sh-3.2# ls -al -rw-r——r—— 1 root aaa 397 10-11 03:23 test.c

這個aaa就是新組名,其在/etc/group中,可以通過adduser aaa自行添加sh-3.2# cat /etc/group root:x:0:root bin:x:1:root,bin,daemon daemon:x:2:root,bin,daemon.

gdm:x:42:sabayon:x:86:plmtest:x:500:aaa:x:501:

chown 改變用戶ID或組ID sh-3.2# chown aaa:aaa test.c sh-3.2# ls -al -rw-r——r—— 1 aaa aaa 397 10-11 03:23 test.c

3,編程實例

/*getgrnam.c*/ #include #include

int main(int argc, char *argv[])

{ if(argv[1] == NULL)

{ printf("input error.\n");return 1;}

struct group *gp = getgrnam(argv[1]);if(gp == NULL)

{ printf("err.\n");return 1;}

printf("name:%s\n", gp->gr_name);printf("psswd:%s\n", gp->gr_passwd);printf("gid:%d\n", gp->gr_gid);

int i;for(i = 0; gp->gr_mem[i] != NULL; i++)

{ printf("group name:%s\n", gp->gr_mem[i]);}

return 0;}

sh-3.2# gcc getgrnam.c -o app sh-3.2# ./app bin name:bin psswd:x gid:1 group name:root group name:bin group name:daemon 4,文件權限不細講了sh-3.2# ls -al總計 483984 drwxr-x—— 13 root root 4096 02-22 00:01 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……

-rw-r——r—— 1 root root 464023491 10-25 22:33 3.3.005-080425.tgz -rw—— 1 root root 9346 02-21 23:16 .bash_history -rw-r——r—— 1 root root 24 2007-01-06 .bash_logout -rw-r——r—— 1 root root 191 2007-01-06 .bash_profile -rw-r——r—— 1 root root 176 2007-01-06 .bashrc drwxrwxrwx 10 1000 users 4096 08-23 20:16 cflow-1.3 -rw-r——r—— 1 root root 759691 08-23 20:13 cflow.tar.gz -rw-r——r—— 1 root root 100 2007-01-06 .cshrc -rwxr-xr-x 1 root root 582 11-11 21:48 delete_M.sh -rw-r——r—— 1 root root 2518 11-11 20:25 .dir_colors

主要是最左邊壹列:drwxr-x——10個字符,最左邊是文件類型,-默認為普通文件;d:目錄文件;l符號鏈接……

後面9個,3個壹組***三組,分別表示所屬用戶uid的權限;所屬組或者附屬組gid的權限;其它權限。

三個字符分別是讀、寫、執行權限讀4,寫2, 執行1

所以chmod 777 test.c,提升到讀、寫、執行權限。

5,組權限操作實例此節演示相同組的成員之間***享資源,即不同uid但相同gid的用戶***享同壹組的資源。

為了方便起見,我同時開了兩個終端。

"sh-3.2#"以root權限登陸的shell /bin/sh "[testa@xxx root]"以testa用戶登陸的shell

註:下文提到的“用戶”是指/etc/passwd裏定義的通過終端登陸的用戶(此文即以下增加的三個賬號名)。

sh-3.2# useradd testa sh-3.2# useradd testb sh-3.2# useradd testc

sh-3.2# tail -f /etc/passwd -n 4 sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin testa:x:500:500::/home/testa:/bin/bash testb:x:501:501::/home/testb:/bin/bash testc:x:502:502::/home/testc:/bin/bash

再開壹個終端登陸testa,之前那個終端保持。

sh-3.2# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)

[testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc

[testa@xxx home]$ cd testb bash: cd: testb: 權限不夠

通過root修改testb目錄權限為770,即當前uid或者gid相同的用戶均有讀寫執行權限。

sh-3.2# cd /home/ sh-3.2# chmod 770 testb

[testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb (here modify)

drwx—— 3 testc testc 4096 02-21 22:52 testc

[testa@xxx home]$ cd testb bash: cd: testb: 權限不夠[testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)

此時雖然開放了testb的所屬組權限,但用戶testa的gid=500(testa) groups=500(testa),它還不屬於testb組。

下面修改testa的gid為testb(或者增加其附屬組groups值為testb)

sh-3.2# usermod -G testb testa (增加用戶testa的附屬組testb)

sh-3.2# id testa uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

此時testa終端需要重新登下,使剛才更改生效[testa@xxx root]$ exit exit [root@xxx ~]# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

[testa@xxx root]$ cd /home/ [testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……

drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc [testa@xxx home]$ cd testb [testa@xxx testb]$ pwd /home/testb

以上是增加了用戶testa的附屬組testb,使其對於屬於testb組的資源有了訪問權限。

下面再使用newgrp切換用戶testa的gid.

[testa@xxx testb]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)

[testa@xxx testb]$ newgrp testb [testa@xxx testb]$ id uid=500(testa) gid=501(testb) groups=500(testa),501(testb)

此時testa用戶的gid已改為501(testb)。

組之前的關系在文件/etc/group sh-3.2# tail -f /etc/group -n 4 sabayon:x:86:testa:x:500:testb:x:501:testa (最後壹列:組內用戶列表。即組testb裏包含testa,testa屬於testb組,大概就這意思吧……)

testc:x:502:

雖然知道控制組關系的文件,但不能直接修改些文件,否則執行newgrp時會出現"抱歉"錯誤提示。

當然root用戶權限是無限制的,它訪問文件時不需要進行權限檢查。

三、相關系統調用getuid();getgid();int setuid(uid_t uid);int setgid(gid_t gid);

只有超級用戶或者需要設置的uid和當前用戶的uid壹致才可以設置,否則返回-1,置errno = EPERM, errno可以通過strerror()翻譯。

其它:[testa@xxx home]$ su testa [testa@xxx home]$ sudo touch aa

testa is not in the sudoers file. This incident will be reported.

以root權限vim /etc/sudoers增加testa ALL=(ALL) ALL

參考:APUE2E,1.8, 4.4, 8.11

  • 上一篇:實訓實施效果怎麽寫
  • 下一篇:SCSI命令集介紹
  • copyright 2024編程學習大全網