解,chown命令详。。。⽂件权限详解
⼀、chmod命令详解
⽂件权限和所有权是Unix/Linux⽂件系统最显著的特征之⼀。linux中的每⼀个⽂件都与多种权限类型相关联,在这些权限中主要分类为3种:
⽤户(User)是⽂件的所有者;
⽤户组(Group)是多个⽤户的集合,系统允许⽤户进⾏某些形式的访问;其他⽤户(others)是除⽤户和⽤户组以外的任何⽤户。⽤命令ls -l(或者ll)可以列出⽂件的权限:实例:
amosli@amosli-pc:/$ ls -l
drwxr-xr-x 11 root root 4096 6⽉ 17 2013 usr
lrwxrwxrwx 1 root root 33 12⽉ 5 23:52 initrd.img -> /boot/initrd.img-3.2.0-57-generic-rw-rw-r-- 1 amosli amosli 1575 12⽉ 26 21:25 bdlogo.jpgprw-rw-r-- 1 amosli amosli 0 12⽉ 20 01:21 scriptfifo
第1列输出明确了后⾯的输出。其中第⼀个字母的对应关系如下:
- 普通⽂件d ⽬录
c 字符设备b 块设备l 符号链接s 套接字p 管道
剩下的每3个字符分为⼀组,共3组,如下所⽰:
d rwx r-x r-x
d表⽰⽬录,第⼀组的3个字符rwx表⽰对应⽤户的所有权限(所有者User),第⼆组对应⽤户组(Group)权限,第三组对应其他⽤户(Others)权限。这9个字符(即9个权限).
rwx分别表⽰read,write,execute,读权限,写权限,执⾏权限。如何更改⽂件的权限??这⾥将⽤到的是chmod命令
amosli@amosli-pc:~/learn/re$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE... or: chmod [OPTION]... OCTAL-MODE FILE... or: chmod [OPTION]... --reference=RFILE FILE...Change the mode of each FILE to MODE.
-c, --changes like verbose but report only when a change is made --no-preserve-root do not treat `/' specially (the default) --preserve-root fail to operate recursively on `/' -f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed --reference=RFILE use RFILE's mode instead of MODE values -R, --recursive change files and directories recursively --help display this help and exit
--version output version information and exit
Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.Report chmod bugs to bug-coreutils@gnu.org
GNU coreutils home page:
参数说明(cfvR):
-c : 若该档案权限确实已经更改,才显⽰其更改动作 -f : 若该档案权限⽆法被更改也不要显⽰错误讯息
-v : 显⽰权限变更的详细资料
-R : 对⽬前⽬录下的所有档案与⼦⽬录进⾏相同的权限变更(即以递回的⽅式逐个变更), 这个-R ⽤的还是很多的。关于MODE:(都是同⼀种格式)
`[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.
说明:u 表⽰该档案的拥有者,g 表⽰与该档案的拥有者属于同⼀个群体(group)者,o 表⽰其他以外的⼈,a 表⽰这三者皆是。 + 表⽰增加权限、- 表⽰取消权限、= 表⽰唯⼀设定权限。
r 表⽰可读取,w 表⽰可写⼊,x 表⽰可执⾏,X 表⽰只有当该档案是个⼦⽬录或者该档案已经被设定过为可执⾏。 实例:
1.将档案 a.txt 设为其他⼈皆可执⾏ :
amosli@amosli-pc:~/learn/re$ ls -ltotal 0
-rw-rw-r-- 1 amosli amosli 0 12⽉ 27 00:18 a.txtamosli@amosli-pc:~/learn/re$ chmod o+x a.txt amosli@amosli-pc:~/learn/re$ ls -ltotal 0
-rwxrw-r-x 1 amosli amosli 0 12⽉ 27 00:18 a.txt
2.将档案a.txt设为所有⼈皆不可读取 :⽅法1:
amosli@amosli-pc:~/learn/re$ chmod a-r a.txt amosli@amosli-pc:~/learn/re$ ls -ltotal 0
--wx-w---x 1 amosli amosli 0 12⽉ 27 00:18 a.txt
⽅法2:
amosli@amosli-pc:~/learn/re$ chmod ugo-r a.txt amosli@amosli-pc:~/learn/re$ ls -ltotal 0
--wx-w---x 1 amosli amosli 0 12⽉ 27 00:18 a.txt
反之,若将读权限赋给所有⼈,则将ugo-r 和a-r改为ugo+r 、a+r即可。3.将读写执⾏三种权限赋给所有⼈
amosli@amosli-pc:~/learn/re$ chmod a+rwx a.txt amosli@amosli-pc:~/learn/re$ ls -ltotal 0
-rwxrwxrwx 1 amosli amosli 0 12⽉ 27 00:18 a.txt
使⽤数字来进⾏权限管理:r-- = 4;对应的⼆进制:100-w-=2;对应的⼆进制: 010--x=1; 对应的⼆进制:001
将对应的值相加即可进⾏权限管理,如:rw-=4+2=6;r-x=4+1=5;rwx=4+2+1=7;-wx=2+1=3;实例:
755就表⽰rwx r-x r-x
amosli@amosli-pc:~/learn/re$ chmod 755 a.txt amosli@amosli-pc:~/learn/re$ ls -ltotal 0
-rwxr-xr-x 1 amosli amosli 0 12⽉ 27 00:18 a.txt
其他皆可依照。如:
chmod 777 file1 <==> chmod a=rwx file chmod 771 file <==> chmod ug=rwx,o=x file
⼆、更改⽂件所有权(chown命令详解)
看⼀下提⽰信息:
amosli@amosli-pc:~/learn/re$ chown --help
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE... or: chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those 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)
--from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute --no-preserve-root do not treat `/' specially (the default) --preserve-root fail to operate recursively on `/' -f, --silent, --quiet suppress most error messages
--reference=RFILE use RFILE's owner and group rather than specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively -v, --verbose output a diagnostic for every file processedThe following options modify how a hierarchy is traversed when the -Roption is also specified. If more than one is specified, only the finalone takes effect.
-H if a command line argument is a symbolic link to a directory, traverse it
-L traverse every symbolic link to a directory encountered
-P do not traverse any symbolic links (default) --help display this help and exit
--version output version information and exit
Owner is unchanged if missing. Group is unchanged if missing, but changedto login group if implied by a `:' following a symbolic OWNER.OWNER and GROUP may be numeric as well as symbolic.Examples:
chown root /u Change the owner of /u to \"root\".
chown root:staff /u Likewise, but also change its group to \"staff\". chown -hR root /u Change the owner of /u and subfiles to \"root\".Report chown bugs to bug-coreutils@gnu.org
GNU coreutils home page:
语法格式:
chown [OPTION]... [OWNER][:[GROUP]] FILE...chown [OPTION]... --reference=RFILE FILE...
参数说明: 必要参数:
-c 若该档案拥有者确实已经更改,才显⽰其更改动作
-f 忽略错误信息,若该档案拥有者⽆法被更改也不要显⽰错误讯息 -h 修复符号链接,只对于连结(link)进⾏变更,⽽⾮该 link 真正指向的档案
-R 处理指定⽬录以及其⼦⽬录下的所有⽂件 -v 显⽰详细的处理信息
-deference 作⽤于符号链接的指向,⽽不是链接⽂件本⾝ 选择参数:
--reference=<⽬录或⽂件> 把指定的⽬录/⽂件作为参考,把操作的⽂件/⽬录设置成参考⽂件/⽬录相同拥有者和群组 --from=<当前⽤户:当前群组> 只有当前⽤户和群组跟指定的⽤户和群组相同时才进⾏改变 --help 显⽰帮助信息 --version 显⽰版本信息常⽤格式:
chown user.group filename
实例:
1.将所有者改为root:
amosli@amosli-pc:~/learn/re$ ll #改之前total 8
drwxrwxr-x 2 amosli amosli 4096 12⽉ 27 00:18 ./drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ../-rwxr-xr-x 1 amosli amosli 0 12⽉ 27 00:18 a.txt*amosli@amosli-pc:~/learn/re$ sudo su [sudo] password for amosli:
root@amosli-pc:/home/amosli/learn/re# chown root a.txt root@amosli-pc:/home/amosli/learn/re# ll #改之后total 8
drwxrwxr-x 2 amosli amosli 4096 12⽉ 27 00:18 ./drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ../-rwxr-xr-x 1 root amosli 0 12⽉ 27 00:18 a.txt*
ll的结果返回七列,分别表⽰如下含义:第⼀栏 [⽂件属性] drwxrwxr-x第⼆栏 [⽂件数] 2第三栏 [拥有者] amosli第四栏 [所有者群组] amosli第五栏 [⼤⼩] 4096
第六栏 [建档⽇期] 12⽉ 27 00:18第七栏 [档名] ./
2.将整个⽬录下的⽂件的所有者都改为root改之前:
root@amosli-pc:/home/amosli/learn# lltotal 128
drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ./drwxr-xr-x 69 amosli amosli 4096 12⽉ 26 23:55 ../---------- 1 amosli amosli 3 12⽉ 18 22:49 a1-rw-rw-r-- 1 amosli amosli 3 12⽉ 18 22:49 a2-rw-rw-r-- 1 amosli amosli 3 12⽉ 18 22:49 a3-rw-rw-r-- 1 amosli amosli 0 12⽉ 26 00:39 a.mp3
更之后:
root@amosli-pc:/home/amosli/learn# chown root . -Rroot@amosli-pc:/home/amosli/learn# lltotal 128
drwxrwxr-x 7 root amosli 4096 12⽉ 26 22:32 ./drwxr-xr-x 69 amosli amosli 4096 12⽉ 26 23:55 ../---------- 1 root amosli 3 12⽉ 18 22:49 a1-rw-rw-r-- 1 root amosli 3 12⽉ 18 22:49 a2-rw-rw-r-- 1 root amosli 3 12⽉ 18 22:49 a3
-rw-rw-r-- 1 root amosli 0 12⽉ 26 00:39 a.mp3
三、chattr命令详解
使⽤chattr命令创建不可修改的⽂件
终端⾥,chattr命令的提⽰信息⾮常少,只给了⼀个语法格式:
root@amosli-pc:/home/amosli/learn/re# chattr --help
Usage: chattr [-RVf] [-+=AacDdeijsSu] [-v version] files...
常⽤语法格式:
sudo chattr +i a.txt或者
chattr +i a.txt
实例:
root@amosli-pc:/home/amosli/learn/re# chattr +i a.txt root@amosli-pc:/home/amosli/learn/re# lltotal 8
drwxrwxr-x 2 amosli amosli 4096 12⽉ 27 01:02 ./drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ../-rw-r--r-- 1 root root 0 12⽉ 27 01:02 a.txtroot@amosli-pc:/home/amosli/learn/re# rm a.txt rm: cannot remove `a.txt': Operation not permitted
使⽤chattr命令更改的权限即使是chmod命令也不能更改⽂件现有权限,如下:
root@amosli-pc:/home/amosli/learn/re# chmod 777 a.txt
chmod: changing permissions of `a.txt': Operation not permittedroot@amosli-pc:/home/amosli/learn/re# chmod a+rwx a.txt chmod: changing permissions of `a.txt': Operation not permitted
但是如果⽂件要重新获取可写应该怎么办呢??如下,chattr -i a.txt即可
root@amosli-pc:/home/amosli/learn/re# chattr -i a.txt root@amosli-pc:/home/amosli/learn/re# lltotal 8
drwxrwxr-x 2 amosli amosli 4096 12⽉ 27 01:02 ./drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ../-rw-r--r-- 1 root root 0 12⽉ 27 01:02 a.txt
root@amosli-pc:/home/amosli/learn/re# chmod a+rwx a.txt root@amosli-pc:/home/amosli/learn/re# lltotal 8
drwxrwxr-x 2 amosli amosli 4096 12⽉ 27 01:02 ./drwxrwxr-x 7 amosli amosli 4096 12⽉ 26 22:32 ../-rwxrwxrwx 1 root root 0 12⽉ 27 01:02 a.txt*
因篇幅问题不能全部显示,请点此查看更多更全内容