linux為多使用者作業系統,為了實作友善使用者管理,采用分組的方式管理使用者
每個使用者都位于一個使用者組中,都有一個uid,而每個使用者組又有一個gid.root作為一個特别的使用者,其uid=gid=0,其所在使用者組名為root.使用者資訊儲存在/etc/passwd中
cat /etc/passwd可看到使用者資訊。
niuxinli@niuxinli-desktop:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
。。。。。。。
niuxinli:x:1000:1000:niuxinli,,,:/home/niuxinli:/bin/bash
。。。。。。。。。。。。。
其中每行為一個使用者,資訊用:分開,從左向右一次為:
使用者名:密碼資訊:uid:gid:使用者根目錄:所用的bash
關于使用者與使用者組就說這麼多,現在詳細的講一下檔案的權限。
用“ls -l 檔案名” 可以檢視該檔案的詳細資訊
如
niuxinli@niuxinli-desktop:~$ ls -l 12.txt
-rwxr-xr-x 1 niuxinli niuxinli 12 2009-10-05 12:56 12.txt
隻看前邊的-rwxr-xr-x 1 niuxinli niuxinli
權限就用-rwxr-xr-x表示,什麼意思呢?
這裡一共有10個字元,第一個表示檔案類型,常見檔案類型:
-:普通檔案,如文本檔案和二進制檔案
d:目錄,linux将目錄看成檔案
l:連結檔案,如
niuxinli@niuxinli-desktop:~$ ln -s 12.txt 11(建立連結)
niuxinli@niuxinli-desktop:~$ ls -l 11
lrwxrwxrwx 1 niuxinli niuxinli 6 2009-10-05 13:14 11 -> 12.txt
p:管道檔案
c:字元裝置檔案
b:塊裝置檔案
(不必了解太細)
接下來一共還有9位,這是表示權限的每三位一組,分别表示檔案所有者對檔案的權限,檔案所在組其他使用者的權限,其他的使用者的權限。權限分為r(讀),w(寫),x(執行)
例如上邊的rwxr-xr-x表示檔案所有者(niuxinli)對檔案可讀可寫可執行,所在組使用者(niuxinli組)對檔案可讀可執行但不能修改,同樣,剩下的其他使用者也是如此。-表示沒有此權限。
跟在權限後面的是連接配接數,緊接着後面是檔案所有者和所在使用者組。
即使權限為---------,root使用者也能對檔案進行操作,因為root在linux中是無所不能的。
還有幾個特殊的權限,後面再詳細說。
那麼我們怎麼來修改一個檔案的權限呢?
chmod 修改使用者的權限
chown 修改檔案的所有者
chgrp 修改檔案的使用者組
chmod用法:
說到chmod用法,先說一下權限的數字表示
用字母需要9位,而用數字隻需3為,r=4,w=2,x=1,相加即可
若某個檔案的權限為761 等價于rwxrw---x;345等價于-wxr--r-x
第一種用法:直接用數字指定權限
如 chmod 754 1.txt
第二種用法:用+,-,=
如chmod +x 1.txt
chmod a+rw 1.txt 表示給所用使用者都加上rw權限
chmod u+w,g+x,o-r 1.txt表示給所有者(u)加上w權限,同組使用者(g)加上x權限,其他使用者(o)減去r權限。
chmod u=rwx,g=rwx,o-rwx
還有指定參考檔案等,不需要記這麼多。
chown:
chown niuxinli 1.txt
把1.txt的所有者修改為1.txt
chgrp
chgrp niuxinli 1.txt
把1.txt的使用者組改為niuxinli
關于linux檔案權限還有很多,比如umask,SUID,SGID,我以後的文章會繼續講。上面很多指令不夠詳細,可以man一下,比如
niuxinli@niuxinli-desktop:~$ man chgrp
CHGRP(1) User Commands CHGRP(1)
NAME
chgrp - change group ownership
SYNOPSIS
chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...
DESCRIPTION
Change the group of each FILE to GROUP. With --reference, change the
group of each FILE to that of RFILE.
-c, --changes
like verbose but report only when a change is made
--dereference
affect the referent of each symbolic link (this is the default),
rather than the symbolic link itself
-h, --no-dereference
affect each symbolic link instead of any referenced file (useful
only on systems that can change the ownership of a symlink)
本文轉自nxlhero 51CTO部落格,原文連結:http://blog.51cto.com/nxlhero/209625,如需轉載請自行聯系原作者