[root@localhost ~]# ls -l
总用量 12
-rw-r--r--. 1 root root 0 10月 25 16:06 1.txt
-rw-------. 1 root root 1422 10月 21 00:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 10月 25 16:06 1.txt
[root@hf-01 ~]# ls -l
总用量 8
-rw-r--r--. 1 root root 924 10月 25 06:49 2.txt
-rw-------. 1 root root 973 8月 21 05:05 anaconda-ks.cfg.1
[root@hf-01 ~]# chmod 700 2.txt 更改2.txt文件权限
[root@hf-01 ~]# ls -l 2.txt
-rwx------. 1 root root 924 10月 25 06:49 2.txt 这里会发现2.txt权限改变了
[root@hf-01 ~]# getenforce 查看防火墙是否关闭
[root@hf-01 ~]# setenforce 0 临时关闭防火墙
若想永久关闭防火墙,则需要更改配置文件
[root@hf-01 ~]# vi /etc/selinux/config 在这个文件下更改
只有关闭了selinux,-rwx------. 最后的这个点才会消失
[root@hf-01 ~]# mkdir hf/ 新建目录hf/
[root@hf-01 ~]# ls
2.txt anaconda-ks.cfg.1 hf
[root@hf-01 ~]# cd hf/
[root@hf-01 hf]# touch 1.txt 新建文件1.txt
[root@hf-01 hf]# ls
1.txt
[root@hf-01 hf]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 hf]# cd
[root@hf-01 ~]# chmod 770 1.txt
chmod: 无法访问"1.txt": 没有那个文件或目录 这是因为1.txt在目录hf/下面
[root@hf-01 ~]# chmod 770 hf/ 更改hf/文件夹的权限
[root@hf-01 ~]# ls -l hf/ 会发现里面的1.txt权限没有发生变化
总用量 0
-rw-r--r--. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# ls -ld hf/ 而文件夹的权限则发生了变化
drwxrwx---. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod -R 661 hf/ 在加上了-R选项,文件和目录和子目录批量的更改了权限
[root@hf-01 ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# ls -ld hf/
drw-rw---x. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod u=rwx,g=w,o=r hf/ 字母缩写代替更改权限
[root@hf-01 ~]# ls -ld hf/
drwx-w-r--. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# chmod a+x hf/ 所有文件权限都加上x执行权限
[root@hf-01 ~]# ls -ld hf/
drwx-wxr-x. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod o+w hf/ 其他用户组加上w可写的权限
[root@hf-01 ~]# ls -ld hf/
drwx-wxrwx. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod a-w hf/ 所有文件权限减去w可写的权限
[root@hf-01 ~]# ls -ld hf/
dr-x--xr-x. 2 root root 18 10月 26 06:56 hf/
在Linux系统中。目录的默认权限为755,文件的默认权限为644
[root@hf-01 ~]# ls /tmp
aminglinux amning mysql.sock yum.log
[root@hf-01 ~]# ls -l /tmp/yum.log 会看到yum.log的所有者是root
-rw-r--r--. 1 root root 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown aming /tmp/yum.log
chown: 无效的用户: "aming" 这是因为在/etc/passwd中没有aming这个用户,需要useradd aming即可
[root@hf-01 ~]# chown hanfeng /tmp/yum.log 这时会看到所有者发生了变化,yum.log文件的所有者变化成hanfeng了
[root@hf-01 ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng root 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown user1:hanfeng /tmp/yum.log
[root@hf-01 ~]# !ls 这里会看到所属主和所属组发生了改变,用户和组中间用:隔开
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 hanfeng 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown -R hanfeng:user1 /tmp/aminglinux/
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 hanfeng user1 27 10月 25 07:29 /tmp/aminglinux/
[root@hf-01 ~]# touch /tmp/aminglinux/3.txt
[root@hf-01 ~]# chown -R user1:hanfeng /tmp/aminglinux/
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 user1 hanfeng 18 10月 24 07:21 2
-rw-r--r--. 1 user1 hanfeng 0 10月 26 08:23 3.txt
drwxr-xr-x. 4 user1 hanfeng 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 /tmp/aminglinux/
[root@hf-01 ~]# chown :root /tmp/yum.log
[root@hf-01 ~]# !ls 这里只更改了它的所属组
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 root 0 10月 26 07:48 /tmp/yum.log
-R 只用于目录,作用是级联更改子目录以及子文件。
[root@hf-01 ~]# chgrp user1 /tmp/yum.log
[root@hf-01 ~]# !ls 在之前的所属组是root,现在所属组是user1
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log
-R 只用于目录,作用是级联更改子目录以及子文件。
[root@hf-01 ~]# touch 11.txt
[root@hf-01 ~]# ls -l 11.txt
-rw-r--r--. 1 root root 0 10月 26 08:39 11.txt
[root@hf-01 ~]# mkdir 123
[root@hf-01 ~]# ls -ld 123
drwxr-xr-x. 2 root root 6 10月 26 08:39 123
[root@hf-01 ~]# umask 这是系统root用户的umask值0022,通过这个值就可以确认文件的默认权限,也可以确认目录的默认权限是什么
0022
[root@hf-01 ~]# umask 002 这里更改默认权限,写全了是0002,但一般会省去开头的0,写成002
[root@hf-01 ~]# touch 33.txt
[root@hf-01 ~]# ls -l 33.txt 这里和上面对比,会发现创建的文本权限发生了变化
-rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt
[root@hf-01 ~]# mkdir 234
[root@hf-01 ~]# ls -ld 234 这里的文件夹权限也发生了变化
drwxrwxr-x. 2 root root 6 10月 26 08:57 234
当umask=003
目录的权限:777(rwxrwxrwx)-003(-------wx)=774(rwxrrxr--)
普通文件的权限:666(rw-rw-rw-)-003(-------wx)=664(rw-rw-r--)
--x减去--w依然是什么都没有
[root@hf-01 ~]# chattr +i 33.txt 给33.txt(空文件)增加了隐藏属性
[root@hf-01 ~]# vi 33.txt 是无法进去编辑文件,增加内容的,强制保存都不可以,它会提示说只有可读权限
[root@hf-01 ~]# head -n2 /etc/passwd > 33.txt
-bash: 33.txt: 权限不够 将文件写入到33.txt文件中,也会提示权限不够
[root@hf-01 ~]# ls -l 33.txt 但查看的时候会看到有可读可写的权限,这时就要想要它是否添加了隐藏属性
-rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt
[root@hf-01 ~]# lsattr 33.txt 可查看33.txt添加了i隐藏属性
----i----------- 33.txt
[root@hf-01 ~]# touch ha.txt
[root@hf-01 ~]# lsattr ha.txt 查看ha.txt文件的隐藏属性
---------------- ha.txt
Try 'mv --help' for more information.
[root@hf-01 ~]# mv 33.txt 56.txt 这时修改将文件改名,会发现无法实现
mv: 无法将"33.txt" 移动至"56.txt": 不允许的操作
[root@hf-01 ~]# rm 33.txt 也无法去删除该文件,就算加-f强制去删除,再去查看的时候,会看到文件依旧存在
rm:是否删除普通空文件 "33.txt"?y
rm: 无法删除"33.txt": 不允许的操作
[root@localhost ~]# touch 1.txt 会发现也无法现更改
touch: 无法创建"1.txt": 权限不够
[root@localhost ~]# chattr -i 1.txt 删除隐藏属性
[root@localhost ~]# lsattr 1.txt 再来查看,会发现隐藏属性没了
---------------- 1.txt
[root@localhost ~]# mv 1.txt 3.txt 这时就可以更改名称了(编辑、删除都可以)
[root@localhost ~]# touch 3.txt
[root@localhost ~]# echo "gurui" > 3.txt
[root@localhost ~]# chattr +a 3.txt 给文件增加a属性,会发现无法删除、编辑,添加内容,只能追加内容
[root@localhost ~]# rm 3.txt
rm:是否删除普通空文件 "3.txt"?y
rm: 无法删除"3.txt": 不允许的操作
[root@localhost ~]# vi 3.txt 无法编辑3.txt文件
[root@localhost ~]# echo "hanfeng shuaiguo" > 3.txt
无法添加内容进去
-bash: 3.txt: 不允许的操作
[root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt 只能追加内容进3.txt文件中
[root@localhost ~]# cat 3.txt
gurui
hanfeng shuaiguo
[root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt 继续追加内容
[root@localhost ~]# cat 3.txt
gurui
hanfeng shuaiguo
hanfeng shuaiguo
[root@localhost ~]# touch 3.txt 可以更改时间信息
[root@localhost ~]# ls -l 3.txt
-rw-r--r--. 1 root root 40 10月 26 15:41 3.txt
[root@localhost ~]# lsattr 3.txt -----a---------- 3.txt [root@localhost ~]# chattr -a 3.txt 会看到a属性去除了 [root@localhost ~]# lsattr 3.txt ---------------- 3.txt
+、-、=分别表示增加、删除、设定 给目录加特殊权限,目录下的文件的文件内容是可以更改的,这个权限只是作用于目录本身。
[root@localhost ~]# mkdir 111
[root@localhost ~]# lsattr 111
[root@localhost ~]# mkdir 111/222/
[root@localhost ~]# lsattr 111/
---------------- 111/222
[root@localhost ~]# lsattr -d 111/
---------------- 111/
[root@localhost ~]# chattr +i 111/
[root@localhost ~]# lsattr -d 111/
----i----------- 111/
[root@localhost ~]# rm -r 111/
rm:是否进入目录"111/"? y
rm:是否删除目录 "111/222"?y
rm: 无法删除"111/222": 权限不够
[root@localhost ~]# mv 111 1212
mv: 无法将"111" 移动至"1212": 不允许的操作
[root@localhost ~]# touch 111/12.txt
touch: 无法创建"111/12.txt": 权限不够
[root@localhost ~]# chattr -i 111/ 给目录去除-i属性
[root@localhost ~]# lsattr -d 111/
---------------- 111/
[root@localhost ~]# chattr +a 111/ 给目录加上+a权限
[root@localhost ~]# touch 111/23.txt 只能追加一个文件(创建一个文件,也算是追加)
[root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt 给目录加了
[root@localhost ~]# cat 111/23.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# chattr -a 111
[root@localhost ~]# chattr +i 111/
[root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt
[root@localhost ~]#
[root@localhost ~]# lsattr -R 111/
在加上-R会查看111/目录下的文件和111/子目录下的文件
---------------- 111/222
111/222:
---------------- 111/23.txt
---------------- 111/12.txt
[root@localhost ~]# lsattr 111/ 若是不加,就仅仅显示一层的目录文件
---------------- 111/222
---------------- 111/23.txt
---------------- 111/12.txt
[root@localhost ~]# tree 111/
111/
├── 12.txt
├── 222
└── 23.txt
1 directory, 2 files