linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解
文件权限详解
一、chmod命令详解
文件权限和所有权是Unix/Linux文件系统最显著的特征之一。linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种:
用户(User)是文件的所有者;
用户组(Group)是多个用户的集合,系统允许用户进行某些形式的访问;
其他用户(others)是除用户和用户组以外的任何用户。
用命令ls -l(或者ll)可以列出文件的权限:
实例:
amosli@amosli-pc:/$ ls -l
drwxr-xr-x root root 6月 usr
lrwxrwxrwx root root 12月 : initrd.img -> /boot/initrd.img-3.2.--generic
-rw-rw-r-- amosli amosli 12月 : bdlogo.jpg
prw-rw-r-- amosli amosli 12月 : 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: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'chmod invocation'
参数说明(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 -l
total
-rw-rw-r-- amosli amosli 12月 : a.txt
amosli@amosli-pc:~/learn/re$ chmod o+x a.txt
amosli@amosli-pc:~/learn/re$ ls -l
total
-rwxrw-r-x amosli amosli 12月 : a.txt
2.将档案a.txt设为所有人皆不可读取 :
方法1:
amosli@amosli-pc:~/learn/re$ chmod a-r a.txt
amosli@amosli-pc:~/learn/re$ ls -l
total
--wx-w---x amosli amosli 12月 : a.txt
方法2:
amosli@amosli-pc:~/learn/re$ chmod ugo-r a.txt
amosli@amosli-pc:~/learn/re$ ls -l
total
--wx-w---x amosli amosli 12月 : 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 -l
total
-rwxrwxrwx amosli amosli 12月 : 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 a.txt
amosli@amosli-pc:~/learn/re$ ls -l
total
-rwxr-xr-x amosli amosli 12月 : 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 processed The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one 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 changed
to 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: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'chown invocation'
语法格式:
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
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rwxr-xr-x amosli amosli 12月 : 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
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rwxr-xr-x root amosli 12月 : a.txt*
ll的结果返回七列,分别表示如下含义:
第一栏 [文件属性] drwxrwxr-x
第二栏 [文件数] 2
第三栏 [拥有者] amosli
第四栏 [所有者群组] amosli
第五栏 [大小] 4096
第六栏 [建档日期] 12月 27 00:18
第七栏 [档名] ./
2.将整个目录下的文件的所有者都改为root
改之前:
root@amosli-pc:/home/amosli/learn# ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxr-xr-x amosli amosli 12月 : ../
---------- amosli amosli 12月 : a1
-rw-rw-r-- amosli amosli 12月 : a2
-rw-rw-r-- amosli amosli 12月 : a3
-rw-rw-r-- amosli amosli 12月 : a.mp3
更之后:
root@amosli-pc:/home/amosli/learn# chown root . -R
root@amosli-pc:/home/amosli/learn# ll
total
drwxrwxr-x root amosli 12月 : ./
drwxr-xr-x amosli amosli 12月 : ../
---------- root amosli 12月 : a1
-rw-rw-r-- root amosli 12月 : a2
-rw-rw-r-- root amosli 12月 : a3
-rw-rw-r-- root amosli 12月 : 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# ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rw-r--r-- root root 12月 : a.txt
root@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 a.txt
chmod: changing permissions of `a.txt': Operation not permitted
root@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# ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rw-r--r-- root root 12月 : a.txt
root@amosli-pc:/home/amosli/learn/re# chmod a+rwx a.txt
root@amosli-pc:/home/amosli/learn/re# ll
total
drwxrwxr-x amosli amosli 12月 : ./
drwxrwxr-x amosli amosli 12月 : ../
-rwxrwxrwx root root 12月 : a.txt*
linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解的更多相关文章
- linux shell 脚本攻略学习20--awk命令入门详解
awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...
- Linux shell 脚本攻略之统计文件的行数、单词数和字符数
摘自:<Linux shell 脚本攻略>
- linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解
一.生成任意大小的文件(dd命令): 举例: amosli@amosli-pc:~/learn/example$ ; + records in + records out bytes ( MB/s a ...
- linux shell 脚本攻略学习2
1.关于文件描述符和重定向: 文件描述符是与一个打开的文件或数据流相关联的整数.文件描述符0.1以及2是系统预留的. 0——stdin(标准输入) 1——stdout(标准输出) 2——stderr( ...
- linux shell 脚本攻略学习11--mkdir和touch命令详解
一.创建目录(mkdir命令详解) amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amo ...
- linux shell 脚本攻略学习9--rename命令详解
rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...
- linux shell 脚本攻略学习5---find命令详解
1.find命令详解 语法: find base_path#base_path可以是任何位置,find会从该位置向下找 实例: amosli@amosli-pc:~$ find /home/amosl ...
- Linux Shell脚本攻略学习总结:一
终端打印 终端打印的常用命令有两个:echo和print 首先,我先介绍echo 1.echo echo这个命令接受三种形式的参数,实例如下: echo "Hello World" ...
- linux shell 脚本攻略学习16--wc命令详解,tree命令详解
在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...
随机推荐
- bzoj1040 骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 【转】vi 写完文件保存时才发现是 readonly
在MAC上编辑apache配置文件,老是忘记sudo…… readonly的文件保存时提示 add ! to override, 但这仅是对root来说的啊! 百毒了一下竟然还有解决方案!! :w ! ...
- hdu5449 Robot Dog (树形dp+倍增lca)
(警告:本篇博客包含大量人类本质内容) 先处理出来lca,然后就只需要知道从每个点到他的父亲.和从他的父亲到这个点的期望时间就可以了 我们设f[x]为x到他父亲的期望时间:g[x]为从x的父亲到x的期 ...
- 解题:THUWC 2017 在美妙的数学王国中畅游
题面 _“数字和数学规律主宰着这个世界.”_ 在 @i207M 帮助下折腾了半天终于搞懂了导数和泰勒展开,引用某学长在考场上的感受:感觉整个人都泰勒展开了 显然是个奇奇怪怪的东西套上LCT,发现直接维 ...
- jsoncpp的安装与使用示例
安装: 生成静态库 生成静态库: 第一步:生成目标文件: g++ -g -Wall -c json_reader.cpp json_value.cpp json_writer.cpp -I. -I.. ...
- NodeJS API Process全局对象
Process 全局对象,可以在代码中的任何位置访问此对象,使用process对象可以截获进程的异常.退出等事件,也可以获取进程的当前目录.环境变量.内存占用等信息,还可以执行进程退出.工作目录切换等 ...
- django-simple-captcha 验证码插件
官方文档:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation github:https://gi ...
- Python基础数据类型-字典(dict)
Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...
- 介绍C++11标准的变长参数模板
目前大部分主流编译器的最新版本均支持了C++11标准(官方名为ISO/IEC14882:2011)大部分的语法特性,其中比较难理解的新语法特性可能要属变长参数模板(variadic template) ...
- li分两列显示
只要控制了li的宽度,利用浮动就能实现<style type="text/css"> .my ul { width: 210px; } .my li { width: ...