二周第三次课(12月20日)

二周第三次课(12月20日)
2.14 更改文件和目录权限chmod
2.15 更改所有者和所属组chown
2.16 umask
2.17 隐藏权限lsattr/chattr

更改文件和目录权限:
命令:
#chmod >> change mode
(-R=文件所有者·文件所属组·其他用户一起更改)

# rw- >>(权限:文件所有者,r是否可读·w是否可写·x是否可执行)
(权限:rw-可读·可写·不可执行)

#r-- >> (权限:文件所组,r是否可读·w是否可写·x是否可执行)
(权限:r--可读·不可写·不可执行)


#r-- >> ( 权限:其他用户,r是否可读·w是否可写·x是否可执行)
(权限:r--可读·不可写·不可执行)

#权限:数字表示(r=4·w=2·x=1 rwx=7 rw-=6 --x=1 )
( rw-r--r--=644 rw-r-xr-x=655)
例:rwx rw- r--
r=读取属性  //值=4
w=写入属性  //值=2
x=执行属性  //值=1


# chmod u=rwx,g=r,o=rx admin2/
(U=文件所有者·g=文件所组·o=其他用户)

# chmod a+x admin2/
(a=全部·文件所有者·文件所组·其他用户)

权限范围的表示法如下:
u User,即文件或目录的拥有者;
g Group,即文件或目录的所属群组;
o Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围;
a All,即全部的用户,包含拥有者,所属群组以及其他用户;
r 读取权限,数字代号为“4”;
w 写入权限,数字代号为“2”;
x 执行或切换权限,数字代号为“1”;
- 不具任何权限,数字代号为“0”;
s 特殊功能说明:变更文件或目录的权限。

 

[root@localhost ~]# ls -l
总用量 8
-rw-r--r-- 1 root root 1049 12月 19 11:12 2.txt
-rw-------. 1 root root 1406 11月 10 22:32 anaconda-ks.cfg.1

[root@localhost ~]# chmod 700 2.txt
[root@localhost ~]# ls -l 2.txt
-rwx------ 1 root root 1049 12月 19 11:12 2.txt

[root@localhost ~]# ls -ld /root/.ssh/
drwx------. 2 root root 187 12月 14 14:12 /root/.ssh/


[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls -l
总用量 0
drwxr-xr-x 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# chmod 777 admin2
[root@localhost tmp]# ls -ld admin2/
drwxrwxrwx 4 root root 29 12月 19 11:38 admin2/
[root@localhost tmp]# ls -l admin2/
总用量 0
drwxr-xr-x 2 root root 19 12月 18 12:21 2
drwxr-xr-x 4 root root 53 12月 19 11:41 admin2


[root@localhost tmp]# chmod -R 770 admin2
[root@localhost tmp]# ls -l admin2/
总用量 0
drwxrwx--- 2 root root 19 12月 18 12:21 2
drwxrwx--- 4 root root 53 12月 19 11:41 admin2


[root@localhost tmp]# ls -l admin2/
总用量 0
drwxrwx--- 2 root root 19 12月 18 12:21 2
drwxrwx--- 4 root root 53 12月 19 11:41 admin2


[root@localhost tmp]# chmod u=rwx,g=r,o=r admin2/
[root@localhost tmp]# chmod u=rwx,g=r,o=rx admin2/


[root@localhost tmp]# chmod a=x admin2/
[root@localhost tmp]# ls -ld admin2/
d--x--x--x 4 root root 29 12月 19 11:38 admin2/


[root@localhost tmp]# chmod a+x admin2/
[root@localhost tmp]# ls -ld admin2/
d--x--x--x 4 root root 29 12月 19 11:38 admin2/


[root@localhost tmp]# chmod u=rwx,g=r,o=rx admin2/
[root@localhost tmp]# ls -ld admin2/
drwxr--r-x 4 root root 29 12月 19 11:38 admin2/

 

[root@localhost tmp]# chmod a+x admin2/
[root@localhost tmp]# ls -ld admin2/
drwxr-xr-x 4 root root 29 12月 19 11:38 admin2/

 

[root@localhost tmp]# chmod a-x admin2/
[root@localhost tmp]# ls -ld admin2/
drw-r--r-- 4 root root 29 12月 19 11:38 admin2/
[root@localhost tmp]#

 

更改所有者和所属组:

#chown命令:
文件权限属性设置:
chown命令改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,
使该用户变成指定文件的所有者或者改变文件所属的组。
用户可以是用户或者是用户D,用户组可以是组名或组id。
文件名可以使由空格分开的文件列表,在文件名中可以包含通配符。
只有文件主和超级用户才可以便用该命令。
语法:
chown(选项)(参数)
选项:
-c或——changes:效果类似“-v”参数,但仅回报更改的部分;
-f或--quite或——silent:不显示错误信息;
-h或--no-dereference:只对符号连接的文件作修改,而不更改其他任何相关文件;
-R或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;
-v或——version:显示指令执行过程;
--dereference:效果和“-h”参数相同;
--help:在线帮助;
--reference=<参考文件或目录>:把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同;
--version:显示版本信息。
参数:
用户:组:指定所有者和所属工作组。当省略“:组”,仅改变文件所有者;
文件:指定要改变所有者和工作组的文件列表。支持多个文件和目标,支持shell通配符。


#chgrp命令:
文件权限属性设置:
chgrp命令用来改变文件或目录所属的用户组。
该命令用来改变指定文件所属的用户组。其中,组名可以是用户组的id,也可以是用户组的组名。
文件名可以 是由空格分开的要改变属组的文件列表,也可以是由通配符描述的文件集合。
如果用户不是该文件的文件主或超级用户(root),则不能改变该文件的组。
在UNIX系统家族里,文件或目录权限的掌控以拥有者及所属群组来管理。
您可以使用chgrp指令去变更文件与目录的所属群组,设置方式采用群组名称或群组识别码皆可。

语法:
chgrp(选项)(参数)
选项:
-c或——changes:效果类似“-v”参数,但仅回报更改的部分;
-f或--quiet或——silent:不显示错误信息;
-h或--no-dereference:只对符号连接的文件作修改,而不是该其他任何相关文件;
-R或——recursive:递归处理,将指令目录下的所有文件及子目录一并处理;
-v或——verbose:显示指令执行过程;
--reference=<参考文件或目录>:把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同;

参数:组:指定新工作名称;

文件:指定要改变所属组的文件列表。多个文件或者目录之间使用空格隔开。


[root@localhost tmp]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
adkee:x:1000:1000::/home/adkee:/bin/bash


[root@localhost tmp]# ls /tmp/
admin2
systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# ls -l /tmp/admin2
总用量 0
drwxrwx--- 2 root root 19 12月 18 12:21 2
drwxrwx--- 4 root root 53 12月 19 11:41 admin2


[root@localhost tmp]# chown adkee /tmp/admin2
[root@localhost tmp]# ls -l
总用量 0
drw-r--r-- 4 adkee root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# chgrp adkee /tmp/admin2
[root@localhost tmp]# ls -l
总用量 0
drw-r--r-- 4 adkee adkee 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# chown adkee:adkee /tmp/admin2
[root@localhost tmp]# ls -l
总用量 0
drw-r--r-- 4 adkee adkee 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# chown :root /tmp/admin2
[root@localhost tmp]# ls -l
总用量 0
drw-r--r-- 4 adkee root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-


[root@localhost tmp]# chown -R root:root /tmp/admin2
[root@localhost tmp]# ls -l
总用量 0
drw-r--r-- 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP

 


umask命令
文件权限属性设置
umask命令用来设置限制新建文件权限的掩码。当新文件被创建时,其最初的权限由文件创建掩码决定。
用户每次注册进入系统时,umask命令都被执行, 并自动设置掩码mode来限制新文件的权限。
用户可以通过再次执行umask命令来改变默认值,新的权限将会把旧的覆盖掉。
语法:
umask(选项)(参数)
选项:
-p:输出的权限掩码可直接作为指令来执行;
-S:以符号方式输出权限掩码。
参数:
权限掩码:指定权限掩码。

# 022+644+755
# 002+664=775
# 777-022=755
# 666-022=644
# 666-002=664

# 664
# 666-003=663
# 666-003=(rw-rw-rw-)-(-------wx)=rw-rw-r--=664
# 777-003=(rwxrwxrwx) - (--------wx =rwxrwxr--) =774

  (--x --w =0)

 

[root@localhost tmp]# touch 11.txt

[root@localhost tmp]# ls -l 11.txt
-rw-r--r-- 1 root root 0 12月 20 10:33 11.txt

[root@localhost tmp]# mkdir 123
[root@localhost tmp]# ls -ld 123
drwxr-xr-x 2 root root 6 12月 20 10:34 123

[root@localhost tmp]# umask
0022

[root@localhost tmp]# umask 002

[root@localhost tmp]# touch 21.txt
[root@localhost tmp]# ls -l 21.txt
-rw-rw-r-- 1 root root 0 12月 20 10:37 21.txt

[root@localhost tmp]# mkdir 332
[root@localhost tmp]# ls -ld 332
drwxrwxr-x 2 root root 6 12月 20 10:37 332

[root@localhost tmp]# 022 644 755^C
[root@localhost tmp]# 002 664 775^C
[root@localhost tmp]# 777-022=755^C
[root@localhost tmp]# 666-022=644^C
[root@localhost tmp]# 666-002=664^C
[root@localhost tmp]# umask 003
[root@localhost tmp]# umask
0003
[root@localhost tmp]# ls
11.txt
123
21.txt
332
admin2
systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP
[root@localhost tmp]# touch 1.txt
[root@localhost tmp]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 12月 20 10:33 11.txt
drwxr-xr-x 2 root root 6 12月 20 10:34 123
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:37 21.txt
drwxrwxr-x 2 root root 6 12月 20 10:37 332
drw-r--r-- 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP
[root@localhost tmp]# 664^C
[root@localhost tmp]# 666-003=663^C
[root@localhost tmp]# 666=(rw-rw-rw-)-(-------wx)=rw-rw-r--=664
[root@localhost tmp]# 777-003=rwxrwxrwx - --------wx =rwxrwxr-- =774
[root@localhost tmp]#

 


隐藏权限:
#lsattr
lsattr命令文件权限属性设置
lsattr命令用于查看文件的第二扩展文件系统属性。

语法 lsattr(选项)(参数)
选项:
-E:可显示设备属性的当前值,但这个当前值是从用户设备数据库中获得的,而不是从设备直接获得的。
-D:显示属性的名称,属性的默认值,描述和用户是否可以修改属性值的标志。
-R:递归的操作方式;
-V:显示指令的版本信息;
-a:列出目录中的所有文件,包括隐藏文件。

参数:
文件:指定显示文件系统属性的文件名。

 

#chattr
chattr命令文件权限属性设置
chattr命令用来改变文件属性。
这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式:
a:让文件或目录仅供附加用途;
b:不更新文件或目录的最后存取时间;
c:将文件或目录压缩后存放;
d:将文件或目录排除在倾倒操作之外;
i:不得任意更动文件或目录;
s:保密性删除文件或目录;
S:即时更新文件或目录;
u:预防意外删除。
语法:
chattr(选项)
选项:
-R:递归处理,将指令目录下的所有文件及子目录一并处理;
-v<版本编号>:设置文件或目录版本;
-V:显示指令执行过程;
+<属性>:开启文件或目录的该项属性;
-<属性>:关闭文件或目录的该项属性;
=<属性>:指定文件或目录的该项属性。

 

[root@localhost tmp]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 12月 20 10:33 11.txt
drwxr-xr-x 2 root root 6 12月 20 10:34 123
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:37 21.txt
drwxrwxr-x 2 root root 6 12月 20 10:37 332
drw-r--r-- 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP

[root@localhost tmp]# chattr +i 1.txt
[root@localhost tmp]# vi 1.txt
[root@localhost tmp]# head -n2 /etc/passwd > 1.txt
-bash: 1.txt: 权限不够

[root@localhost tmp]# ls -l 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt

[root@localhost tmp]# lsattr 1.txt
----i----------- 1.txt

[root@localhost tmp]# touch 2.txt
[root@localhost tmp]# lsattr 2.txt
---------------- 2.txt

[root@localhost tmp]# mv 1.txt 3.txt
mv: 无法将"1.txt" 移动至"3.txt": 不允许的操作


[root@localhost tmp]# rm -f 3.txt
[root@localhost tmp]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 12月 20 10:33 11.txt
drwxr-xr-x 2 root root 6 12月 20 10:34 123
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:37 21.txt
-rw-rw-r-- 1 root root 0 12月 20 11:21 2.txt
drwxrwxr-x 2 root root 6 12月 20 10:37 332
drw-r--r-- 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP

[root@localhost tmp]# lsattr 1.txt
----i----------- 1.txt

[root@localhost tmp]# touch 1.txt
touch: 无法创建"1.txt": 权限不够

[root@localhost tmp]# touch 2.txt
[root@localhost tmp]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 12月 20 10:33 11.txt
drwxr-xr-x 2 root root 6 12月 20 10:34 123
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:37 21.txt
-rw-rw-r-- 1 root root 0 12月 20 11:27 2.txt
drwxrwxr-x 2 root root 6 12月 20 10:37 332
drw-r--r-- 4 root root 29 12月 19 11:38 admin2
drwx------ 3 root root 17 12月 20 08:59 systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# chattr -i 1.txt
[root@localhost tmp]# ls -l 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt

[root@localhost tmp]# ls -l 1.txt
-rw-rw-r-- 1 root root 0 12月 20 10:54 1.txt


[root@localhost tmp]# ls
11.txt
123
1.txt
21.txt
2.txt
332
admin2
systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP


[root@localhost tmp]# vi 1.txt
[root@localhost tmp]# cat 1.txt
sdkalshdjkas
asda
asd
asd
af


[root@localhost tmp]# chattr +a 1.txt
[root@localhost tmp]# rm 1.txt
rm:是否删除普通文件 "1.txt"?y
rm: 无法删除"1.txt": 不允许的操作


[root@localhost tmp]# vi 1.txt
[root@localhost tmp]# head -n2 /etc/passwd > 1.txt
-bash: 1.txt: 不允许的操作


[root@localhost tmp]# head -n2 /etc/passwd >> 1.txt
[root@localhost tmp]# !cat
cat 1.txt
sdkalshdjkas
asda
asd
asd
af

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost tmp]# head -n2 /etc/passwd >> 1.txt
[root@localhost tmp]# cat 1.txt
sdkalshdjkas
asda
asd
asd
af

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost tmp]# mv 1.txt 3.txt
mv: 无法将"1.txt" 移动至"3.txt": 不允许的操作

[root@localhost tmp]# touch 1.txt

[root@localhost tmp]# ls -l 1.txt
-rw-rw-r-- 1 root root 160 12月 20 11:34 1.txt

[root@localhost tmp]# lsattr 1.txt
-----a---------- 1.txt

[root@localhost tmp]# chattr -a 1.txt

[root@localhost tmp]# ls -l 1.txt
-rw-rw-r-- 1 root root 160 12月 20 11:34 1.txt

[root@localhost tmp]# lsattr 1.txt
---------------- 1.txt

[root@localhost tmp]# ls
11.txt
123
1.txt
21.txt
2.txt
332
admin2
systemd-private-f93c6cd09e5d45d6badb9f26bfe179be-vmtoolsd.service-4HhGmP

[root@localhost tmp]# mkdir 123/332

[root@localhost tmp]# lsattr 123
---------------- 123/332

[root@localhost tmp]# lsattr -d 123
----i----------- 123

[root@localhost tmp]# rm -r 123
rm:是否进入目录"123"? y
rm:是否删除目录 "123/332"?y
rm: 无法删除"123/332": 权限不够

[root@localhost tmp]# mv 123 332
mv: 无法将"123" 移动至"332/123": 不允许的操作

[root@localhost tmp]# touch 123/16.txt
touch: 无法创建"123/16.txt": 权限不够

[root@localhost tmp]# chattr -i 123
[root@localhost tmp]# chattr +a 123
[root@localhost tmp]# touch 123/31.txt
[root@localhost tmp]# vi 123/32.txt
[root@localhost tmp]# cat 123/32.txt
sdasdasas

 

[root@localhost tmp]# head -n2 /etc/passwd > 123/31.txt
[root@localhost tmp]# cat !$
cat 123/31.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost tmp]# chattr -a 123

[root@localhost tmp]# chattr +i 123

[root@localhost tmp]# head -n2 /etc/passwd > 123/31.txt

[root@localhost tmp]# touch 123/41.txt
touch: 无法创建"123/41.txt": 权限不够

[root@localhost tmp]# lsattr -R 123
---------------- 123/332

123/332:

---------------- 123/31.txt
---------------- 123/32_txt.swp
---------------- 123/32.txt

[root@localhost tmp]# lsattr 123
---------------- 123/332
---------------- 123/31.txt
---------------- 123/32_txt.swp
---------------- 123/32.txt


[root@localhost tmp]# tree 123
123
├── 31.txt
├── 32.txt
├── 32_txt.swp
└── 332

1 directory, 3 files
[root@localhost tmp]# chattr +i +a -i -a ^C
[root@localhost tmp]# lsattr -R -a -d

posted @ 2017-12-20 13:00  两颗白菜  阅读(225)  评论(0编辑  收藏  举报