文件和目录权限管理(1)
$》:在Linux系统中,每个文件和目录都有访问的许可权限,用它来确定用户能以何种方式对文件和目录进行访问和操作。
$》:文件或目录的访问权限分为只读,只写和可执行三种。以文件为例,只读权限表示只允许读其内容,而不能对其内容进行任何的修改操作;可执行权限表示允许将该文件作为一个程序执行。
如图:
root@ubuntu:~# cd /home/weijunqiang/Documents root@ubuntu:/home/weijunqiang/Documents# ll total 36 drwxr-xr-x 9 weijunqiang weijunqiang 4096 Feb 7 07:48 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/
root@ubuntu:~# ll total 10588 drwx------ 4 root root 4096 Feb 7 03:28 ./ drwxr-xr-x 24 root root 4096 Feb 7 07:00 ../ -rw-r--r-- 1 root root 74 Feb 6 23:02 argv.awk -rw------- 1 root root 112 Feb 6 13:01 .bash_history -rw-r--r-- 1 root root 3106 Apr 19 2012 .bashrc -rw-r--r-- 1 root root 10796012 Feb 6 12:44 Bryan_Adams-Here_I_Am.mp3 drwx------ 2 root root 4096 Feb 6 22:42 .cache/ -rw-r--r-- 1 root root 3633 Jan 28 08:12 english-2015-1-28.txt -rw-r--r-- 1 root root 140 Apr 19 2012 .profile drwx------ 2 root root 4096 Feb 6 22:29 .pulse/ -rw------- 1 root root 256 Feb 6 19:19 .pulse-cookie -rw------- 1 root root 2652 Feb 7 03:28 .viminfo |
权限三位(rwx:读、写、可执行)为一组,开头(权限位的第一位)为d表示目录,开头(权限位的第一位)为-表示文件
drwxr-xr-x 24 root root 4096 Feb 7 07:00 ../
rwx(rwx:可读可写可执行):属主用户权限位
r-x(r-x:可读不可写可执行):与文件属主同组的用户权限位
r-x(rwx:可读不可写可执行):其它用户权限位
-rw-r--r-- 1 root root 74 Feb 6 23:02 argv.awk
rw-(rwx:可读可写不可执行): 属主用户权限位
r--(rwx:可读不可写不可执行):与文件属主同组的用户权限位
r-(rwx:可读不可写不可执行)-:其它用户权限位
文件和目录权限管理(2)
》文件和目录权限管理依赖于两个极其重要的命令:更改文件(目录)权限命令chmod和更改文件(目录)用户命令chown
》chmod命令格式:
chmod [userType] [signal] [type] [filename]
用户类型 |
数学符号 |
文件类型 |
u表示用户(user),即文件和目录的所有者 |
+ 添加某个权限 |
r可读 |
g表示同组(group),即与文件属主同组的用户 |
- 取消某个权限 |
w可写 |
o表示其他(other)用户 |
= 赋予给定权限并取消其他所有权限 |
x可执行 |
a表示所有(all)用户。它是系统默认值 |
|
|
root@ubuntu:/home/weijunqiang/Documents# ls java-ws mv_cp_temp mydirs mydirs001 notes rm01 workspace root@ubuntu:/home/weijunqiang/Documents# vi test.html <!doctype html> <html> <head> <title>Test</title> <head> <body> <b>Hello World!</b> </body> ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "test.html" [New] 8L, 92C written root@ubuntu:/home/weijunqiang/Documents# ls java-ws mv_cp_temp mydirs mydirs001 notes rm01 test.html workspace root@ubuntu:/home/weijunqiang/Documents# ll total 40 drwxr-xr-x 9 weijunqiang weijunqiang 4096 Feb 7 08:50 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rw-r--r-- 1 root root 92 Feb 7 08:50 test.html drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ root@ubuntu:/home/weijunqiang/Documents# ll test.html -rw-r--r-- 1 root root 92 Feb 7 08:50 test.html # u+x,g+x:用户主,同组用户都添加可执行的权限 root@ubuntu:/home/weijunqiang/Documents# chmod u+x,g+x test.html root@ubuntu:/home/weijunqiang/Documents# ll test.html -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* root@ubuntu:/home/weijunqiang/Documents# mkdir promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxr-xr-x 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ root@ubuntu:/home/weijunqiang/Documents# ll promiss_dir total 8 drwxr-xr-x 2 root root 4096 Feb 7 08:54 ./ drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ../ root@ubuntu:/home/weijunqiang/Documents# ll promiss_dir/ total 8 drwxr-xr-x 2 root root 4096 Feb 7 08:54 ./ drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ../ root@ubuntu:/home/weijunqiang/Documents# chmod o-x,a+r promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxr-xr-- 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ root@ubuntu:/home/weijunqiang/Documents# chmod u-r,u-x promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ d-w-r-xr-- 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ # u+x,u+r,g-x,g-r,o-r 用户主添加可执行可读权限,同组用户取消可执行可读权限,其他用户取消可读权限 root@ubuntu:/home/weijunqiang/Documents# chmod u+x,u+r,g-x,g-r,o-r promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwx------ 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ # a+x,a+r,a+w用户主、同组用户、其他用户均添加可执行可读可写的权限 root@ubuntu:/home/weijunqiang/Documents# chmod a+x,a+r,a+w promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxrwxrwx 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/
|
文件和目录权限管理(3)
》权限的数字表示方法
》0表示没有权限, 1表示可执行权限,2表示可写权限,4表示可读权限
》将上面的数字相加,形成3个从0到7的八进制数,其顺序是u(用户主user), g(同组用户group), o(其他用户other)
》chmod u+x,g+w test.txt等价于(假设test.txt文件的权限是 rw-r--r-- )
》chmod 764 test.txt 即表示test.txt 文件,用户主权限为可读可写可执行(4+2+1=7), 同组用户可读可写不可执行(4+2=6),其他用户可读不可写不可执行(4)。
root@ubuntu:/home/weijunqiang/Documents# clear root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ #当前所有用户都具有可读可写可执行权限 drwxrwxrwx 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ #将文件夹权限改为用户主拥有可读可写可执行权限(4+2+1=7),群组用户、其他用户权限改为可读不可写不可执行(4) root@ubuntu:/home/weijunqiang/Documents# chmod 744 promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ drwxr--r-- 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ |
文件和目录权限管理(4)
》chown命令可以该变变文件或目录的属主。一般来说,这个指令只有是由系统管理者(root)所使用的,一般使用者没有权限改变别人的文件或目录属主。
》chown [option] [owner] [filename]
》 -R是最常用的选项,对目前目录下的所有文件与子目录进行相同的拥有者变更。
root@ubuntu:/home/weijunqiang/Documents# clear root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ #文件夹promiss_dir所属主为root drwxr--r-- 2 root root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ #文件夹promiss_dir所属主改为weijunqiang root@ubuntu:/home/weijunqiang/Documents# chown weijunqiang promiss_dir root@ubuntu:/home/weijunqiang/Documents# ll total 44 drwxr-xr-x 10 weijunqiang weijunqiang 4096 Feb 7 08:54 ./ drwxr-xr-x 24 weijunqiang weijunqiang 4096 Feb 7 03:09 ../ drwxrwxr-x 3 weijunqiang weijunqiang 4096 Feb 7 02:40 java-ws/ drwxr-xr-x 4 root root 4096 Feb 7 07:14 mv_cp_temp/ drwxr-xr-x 3 root root 4096 Feb 7 07:45 mydirs/ drwxrwxrwx 2 root root 4096 Feb 7 07:48 mydirs001/ drwxrwxr-x 2 weijunqiang weijunqiang 4096 Feb 7 03:47 notes/ #文件夹promiss_dir所属主已被改为weijunqiang drwxr--r-- 2 weijunqiang root 4096 Feb 7 08:54 promiss_dir/ drwxr-xr-x 3 root root 4096 Feb 7 07:30 rm01/ -rwxr-xr-- 1 root root 92 Feb 7 08:50 test.html* drwxr-xr-x 7 root root 4096 Feb 7 07:05 workspace/ |