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 ...
随机推荐
- 工欲善其事,必先利其器之open live writer写作
在博客园学习有一段时间,想想是不是自己也应该开始写点东西,做点总结,更加快速的提升自己. 查看小组/博客园使用帮助 得知目前windows live writer 已经停止更新并推荐安装 open l ...
- EXTENDED LIGHTS OUT poj1222 高斯消元法
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6443 Accepted: 42 ...
- Theano学习-scan循环
\(1.Scan\) 通用的一般形式,可用于循环 减少和映射(对维数循环)是特殊的 \(scan\) 对输入序列进行 \(scan\) 操作,每一步都能得到一个输出 \(scan\) 能看到定义函数的 ...
- C# 7 局部函数剖析
局部函数是C# 7中的一个新功能,允许在一个函数中定义另一个函数. 何时使用局部函数? 局部函数的主要功能与匿名方法非常相似:在某些情况下,创建一个命名函数在读者的认知负担方面代价太大.有时,函数本身 ...
- Android 中更新UI的方法
1)使用Activity.runOnUiThread(Runable action)方法 情景一: 在主线程中,定义方法,在方法中启动线程. public class MainActivity ext ...
- Python调用C# Com dll组件实战
之前公司有套C# AES加解密方案,但是方案加密用的是Rijndael类,而非AES的四种模式(ECB.CBC.CFB.OFB,这四种用的是RijndaelManaged类),Python下Crypt ...
- 在SQLSERVER中创建DBLINK,操作远程服务器数据库
--配置SQLSERVER数据库的DBLINK exec sp_addlinkedserver @server='WAS_SMS',@srvproduct='',@provider='SQLOLEDB ...
- js 将一大段时间均分为很多个小时间段
最近写项目,遇到一个将选中时间段平均分割为若干小段,然后根据小段时间在数据库查询求均值的问题,后台大哥犯懒,非说后台做不了,让我分好传给他ヾ(. ̄□ ̄)ツ゜゜゜好气呦,但还要保持微笑,我就是这么懂礼貌 ...
- intellij idea 插件开发--快速定位到mybatis mapper文件中的sql
intellij idea 提供了openApi,通过openApi我们可以自己开发插件,提高工作效率.这边直接贴个链接,可以搭个入门的demo:http://www.jianshu.com/p/24 ...
- 使用binlog2sql做数据恢复的简单示例
有时我们会遇到操作人员误删或者误更新数据的情况,这时我们迫切希望把原来的数据还原回来,今天我们介绍一个简单的工具来方便的实现此功能. 前提条件 在实现数据恢复之前,需要我们的MySQL满足以下配置条件 ...