linux学习(八)chmod、chown、umask、lsattr、chattr
一、权限位
权限位分为两个部分,第一个部分是谁的权限,第二部分是权限是多少。其中第一个部分一般分为:用户,用户组,其他用户。第二部分分为r:读权限,w:写权限,x:执行权限。可读,可写,可执行的权限,用数字表示分别是:421。
用一段代码来说明:
[root@iZ25lzba47vZ ~]# ls -ld
drwxr-xr-x root root Oct :
rwxr-xr-x:表示权限位的值。
root root 分别代表文件的所有者,和所属组。那么他们的权限分别是多少呢?只需要把rwxr-xr-x每三个字母切割开来就是了:
root:rwx root用户的权限是可读可写可执行。
root:r-x root组织有读和执行的权限。
other:r-x 其他用户只有读和执行的权限。
上面这个目录1的权限用数字表示是:755。
可读,可写,可执行对目录的意义:
[ruanwenwu@iZ25lzba47vZ ~]$ mkdir test
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch /test/.txt
touch: cannot touch ‘/test/.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ touch test/.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
ls: cannot access test/.txt: Permission denied
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
drw-rw-rw- ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l test
ls: cannot access test/.txt: Permission denied
total
-????????? ? ? ? ? ? .txt
[ruanwenwu@iZ25lzba47vZ ~]$ cd test/
bash: cd: test/: Permission denied
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test/
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
drwxrwxrwx ruanwenwu ruanwenwu Oct : test/
以上代码说明:
1.切换到ruanwenwu普通用户。
2.在家目录下建立test目录。
3.在test目录下简历1.txt文件。
4.修改test目录为644的权限。
5.ls test发现下面的1.txt为乱码,切提示“Permission denied”。这里说明,我们已经不能看到test目录下的1.txt了。
6.ls -ld test发现一切正常。说明有r的权限,目录就可以用ls -ld看的。
7.cd test/ 发现也是不允许的。
可执行,对于目录的意义,就是可不可以往目录里增减文件:
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ mv .txt test/
mv: cannot stat ‘.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch .txt
[ruanwenwu@iZ25lzba47vZ ~]$ mv .txt test/
mv: cannot move ‘.txt’ to ‘test/.txt’: Permission denied
可读,可写,可执行对文件的意义比较好理解:
可读就是可以查看,可写就是可修改,可执行就是能执行。
二、chmod
chmod用来修改文件的权限位。
使用数字的方法修改权限(建议使用):
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test/
使用针对用户的方法:
修改所有用户:
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rw-rw-r-- ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod a=rwx .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rwxrwxrwx ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
分别写:
[ruanwenwu@iZ25lzba47vZ ~]$ chmod u=rx,g=rwx,o=x .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-r-xrwx--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
使用增加和减少的办法:
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-r-xrwx--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod u+w .txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod g-w .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rwxr-x--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
三、chown
修改文件的所属者或者所属组。
修改文件的所有者:
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu root Oct :
[root@iZ25lzba47vZ ~]# chown root
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :
修改文件的所属组:
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :
[root@iZ25lzba47vZ ~]# chown :ruanwenwu
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root ruanwenwu Oct :
同时修改所有者和所属组:
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :
[root@iZ25lzba47vZ ~]# chown ruanwenwu:ruanwenwu
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu ruanwenwu Oct :
如果目录下有文件需要修改权限,可以用-R参数,级联修改:
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu ruanwenwu Oct :
[root@iZ25lzba47vZ ~]# cd
[root@iZ25lzba47vZ ]# ls [root@iZ25lzba47vZ ]# ls -l
total
drwxr-xr-x root root Oct :
[root@iZ25lzba47vZ ]# cd
[root@iZ25lzba47vZ ]# ls
.txt
[root@iZ25lzba47vZ ]# ls -l
total
-rw-r--r-- root root Mar .txt
[root@iZ25lzba47vZ ]# cd ..
[root@iZ25lzba47vZ ]# cd ..
[root@iZ25lzba47vZ ~]# ls
.ipt Application Document.pdf npm-debug.log ruanwenwu syncwithgit.sh
.cap a.php a.txt iptables.bak oneinstack shellscripts
[root@iZ25lzba47vZ ~]# chown -R ruanwenwu.ruanwenwu
[root@iZ25lzba47vZ ~]# cd
[root@iZ25lzba47vZ ]# ls -l
total
drwxr-xr-x ruanwenwu ruanwenwu Oct :
[root@iZ25lzba47vZ ]# cd
[root@iZ25lzba47vZ ]# ls -l
total
-rw-r--r-- ruanwenwu ruanwenwu Mar .txt
四、umask
umask决定了我们默认建立一个文件或者目录的默认权限。
查看当前umask:
[root@iZ25lzba47vZ ]# umask
那么在当前0022的umask下新建目录或者文件的权限是多少呢?我们看看:
[root@iZ25lzba47vZ ]# umask [root@iZ25lzba47vZ ]# touch b.txt
[root@iZ25lzba47vZ ]# ls -l b.txt
-rw-r--r-- root root Oct : b.txt
[root@iZ25lzba47vZ ]# mkdir
[root@iZ25lzba47vZ ]# ls -ld
drwxr-xr-x root root Oct :
可见,文件是644,目录是755。那么,这两组权限是怎么算出来的呢?
正确的算法:
目录:rwxrwxrwx 减去 ----w--w- = rwxr-xr-x。
文件没有执行权限,为rw-r--r--所以就是644了。
修改umask:
[root@iZ25lzba47vZ ]# umask
[root@iZ25lzba47vZ ]# umask [root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# ls -l .txt
-rw-rw-r-- root root Oct : .txt
五、lsattr,chattr
lsattr用来查看文件的隐藏权限。
chattr用来修改隐藏权限。
那么隐藏权限可以用来干嘛呢?比方说,你有一个文件谁都不让动,连root用户都不让动,也就是不让修改,删除等任何操作。这个时候就可以用到隐藏权限。常用的隐藏权限有i和a。
i属性的特征是不让修改,删除,也不能touch。a属性的特征是可以追加和touch,别的操作不允许。
添加i属性:
[root@iZ25lzba47vZ ]# lsattr .txt
-------------e-- .txt
[root@iZ25lzba47vZ ]# chattr +i .txt
[root@iZ25lzba47vZ ]# lsattr .txt
----i--------e-- .txt
现在我们尝试对它进行删除,修改,touch操作:
[root@iZ25lzba47vZ ]# lsattr .txt
----i--------e-- .txt
[root@iZ25lzba47vZ ]# touch .txt
touch: cannot touch ‘.txt’: Permission denied
[root@iZ25lzba47vZ ]# cat > .txt
-bash: .txt: Permission denied
[root@iZ25lzba47vZ ]# mv .txt .txt
mv: cannot move ‘.txt’ to ‘.txt’: Operation not permitted
[root@iZ25lzba47vZ ]# ls -l .txt
-rw-rw-r-- root root Oct : .txt
刚才试了文件。如果是对一个目录添加i属性呢?
首先看一下,如何看目录的隐藏属性:
[root@iZ25lzba47vZ ]# lsattr -d
-------------e--
[root@iZ25lzba47vZ ]# lsattr
-------------e-- /
----i--------e-- /.txt
-------------e-- /b.txt
-------------e-- /.txt
发现,加-d属性是看目录的,如果不加-d,可以看到目录下所有文件的隐藏权限。
如果要看到子目录下的隐藏属性呢?
[root@iZ25lzba47vZ ]# lsattr -R
-------------e-- / /:
-------------e-- //b.txt ----i--------e-- /.txt
-------------e-- /b.txt
-------------e-- /.txt
好了,现在我们给目录4添加i属性:
[root@iZ25lzba47vZ ]# chattr +i
[root@iZ25lzba47vZ ]# lsattr -d
----i--------e--
[root@iZ25lzba47vZ ]# ls [root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# mv .txt /
mv: cannot move ‘.txt’ to ‘/.txt’: Permission denied
[root@iZ25lzba47vZ ]# mv /
mv: cannot move ‘’ to ‘/’: Operation not permitted
[root@iZ25lzba47vZ ]# echo >> /b.txt
[root@iZ25lzba47vZ ]# cat /b.txt
通过上面这段代码我们发现,给目录加了i属性后,目录不能重命名,也不能往里面放新的文件。但是,并不妨碍我们修改里面的文件4/b.txt,但是不能删除:
[root@iZ25lzba47vZ ]# rm -rf /b.txt
rm: cannot remove ‘/b.txt’: Permission denied
[root@iZ25lzba47vZ ]# echo >> /b.txt
我们再来看一下a属性:
[root@iZ25lzba47vZ ]# lsattr .txt
-------------e-- .txt
[root@iZ25lzba47vZ ]# chattr +a .txt
[root@iZ25lzba47vZ ]# lsattr .txt
-----a-------e-- .txt
[root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# echo > .txt
-bash: .txt: Operation not permitted
[root@iZ25lzba47vZ ]# echo >> .txt
[root@iZ25lzba47vZ ]# mv .txt .txt
mv: cannot move ‘.txt’ to ‘.txt’: Operation not permitted
[root@iZ25lzba47vZ ]# rm -rf .txt
rm: cannot remove ‘.txt’: Operation not permitted
上面的代码说明,有a属性的文件,同样是不能删除,修改,但是可以追加和touch的。那么对于有a属性的目录呢?
[root@iZ25lzba47vZ ]# lsattr -d
-------------e--
[root@iZ25lzba47vZ ]# chattr +a
[root@iZ25lzba47vZ ]# lsattr -d
-----a-------e--
[root@iZ25lzba47vZ ]# ls
.txt
[root@iZ25lzba47vZ ]# mv .txt /
mv: cannot move ‘.txt’ to ‘/.txt’: Operation not permitted
删除对应的隐藏属性:
[root@iZ25lzba47vZ ]# chattr -a
[root@iZ25lzba47vZ ]# lsattr -d
-------------e--
同样,修改目录的隐藏属性,并不影响我们对目录下面的文件的修改。
linux学习(八)chmod、chown、umask、lsattr、chattr的更多相关文章
- 第四节 mount /who / mkdir /rmdir /rm /cp /mv /touch /cat /tac/head /tail /more /less / chmod /chown /umask /chattr /lsattr /history /echo
***Linux下的文件类型如下: 9 8 7 6 5 4 3 2 1 0- r w x r - x r - x 第9位表示文件类型,可以为p.d.l.s.c.b和-:p表示命名管道文件 -pipe ...
- Linux第四节 组管理、用户管理、权限管理 / chmod /chown / umask / vim
三期第三讲1.组管理/用户管理(重要文件系统会实时备份 file-) vim/etc/group: 组管理文件://组名:密码控位键:组id:成员 vim/etc/gshadow:组密码管理文件:// ...
- linux下的chmod,chown和chgrp
对于linux的权限掌握以下几个命令就可以非常熟练的操作系统中的各种权限了. 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode f ...
- Linux 学习 (八) Shell
Linux达人养成计划 I 学习笔记 Shell 是什么: Shell 是一个命令解释器 Shell 还是一个功能相当强大的编程语言,易编写,易调试,灵活性较强 Shell 的分类: Bourne S ...
- 【Linux学习八】脚本编程
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.多层bash#.和source都是当前bash [root@nod ...
- Linux 学习
远程登录Linux(05) 文本方式远程: putty SecureCRT winSCP SshClient图形方式远程:Xmanager Xming ifconfigps -ef | gr ...
- Linux CentOS7 VMware 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr
一.文件和目录权限chmod u User,即文件或目录的拥有者:g Group,即文件或目录的所属群组:o Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围:a All,即全部 ...
- /文件和目录权限chmod /更改所有者和所属组chown/umask/隐藏权限lsattr/chattr
2.14 文件和目录权限chmod 2.15 更改所有者和所属组chown2.16 umask2.17 隐藏权限lsattr/chattr 文件和目录权限chmod 文件权限: r 4 ...
- 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr 使用介绍
第2周第3次课(3月28日) 课程内容:2.14 文件和目录权限chmod2.15 更改所有者和所属组chown2.16 umask2.17 隐藏权限lsattr/chattr 2.14 文件和目录权 ...
- linux基础2-cd、mkdir、touch、umask、chattr、lsattr、SUID/SGID/Sticky Bit
一 cd : . 代表当前目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表[目前用户身份]所在的自家目录 与cd效果相同 ~account 代表 account 这个用户的自家家目录 二m ...
随机推荐
- Flask-WTF 创建表单P2
表单安全 无需任何配置,FlaskForm将提供具有CSRF(Cross-site request forgery,也被称为one-click attack 或者session riding,通常缩写 ...
- 框架应用:Spring framework (三) - JDBC支持
Spring框架是一个一站式的框架,也就是对很多技术和框架做了封装,使其应用更加简便. JDBC的代码过程 /STEP 1. Import required packages import java. ...
- jmeter 分布式实战
最近作者在公司部署公司的分布式压力测试情况的时候,遇到了问题,什么问题呢,各种错误,于是大晚上的为了不耽误压测,我们就两个同事两台电脑搞,可是还是不行的呢,我要研究研究这个是什么梗,于是乎,大晚上加班 ...
- js 各种循环的区别与用法(for in,forEach,for of)
1,forEach循环 不能跳过或者终止循环 const a = ["a","ss","cc"] a.dd="11" ...
- DOM中元素对象的属性方法
在 HTML DOM (文档对象模型)中,每个部分都是节点. 节点是DOM结构中最基本的组成单元,每一个HTML标签都是DOM结构的节点. 文档是一个 文档节点 . 所有的HTML元素都是 ...
- Coin Change (IV) (dfs)
Coin Change (IV) Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Subm ...
- 吐槽CSDN--想赚钱想疯了--强行升级皮肤--增加广告位
一直对CSDN很有好感,和博客园同是技术分享的好平台,但是界面更清爽,用户间互动也较多.在学生时代就一直在用,平时抄个作业,竞赛搜个题,论文需要凑字数等等.当年为了下载一份源代码,或者为了下载某本买不 ...
- 【转载】使用CSS将图片转换成黑白(灰色、置灰)
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=2547原文摘要: . ...
- fatal: The remote end hung up unexpectedly
git push 的时候出错,提示: fatal: The remote end hung up unexpectedly 遇见几次了,原因是因为文件太大,把限制放宽就好了.命令: git confi ...
- JAVA中子类会不会继承父类的构造方法
声明:刚刚接触java不久,如果理解有错误或偏差望各位大佬强势批判 java中子类能继承父类的构造方法吗? 父类代码: class Father { String name ; //就不set/get ...