免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1683 | 回复: 1
打印 上一主题 下一主题

[学习分享] linux基本权限管理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-12-18 19:37 |只看该作者 |倒序浏览
写在前面:
本博客详解命令chmod,  chowm,  chgrp,  umask,  install,  mktemp


权限管理:
进程文件访问权限应用模型:
进程的属主与文件属主是否相同,如果相同,则应用属主权限
否则,检查文件的属主是否属于文件的属组,如果是,则应用属主权限
否则,应用other权限

文件权限的意义:
r:可获取文件数据
w:可修改文件数据
x:可将此文件运行为进程

目录权限的意义:
r:可使用ls获取文件列表
w:可修改此目录下文件列表,创建、删除文件
x:可cd至此目录中,可以ls -l获取文件详细信息
注意:w不能随便给,r,x一般都给

--- 000  0;  --x 001  1;   -w- 010  2; r-- 100  4
常用权限组合:
rw-rw-r-- 664
rwxrwxr-x 775
rwxr-x--- 750
rwxr-xr-x 755

三类用户:
u:属主
g:属组
o:其它用户
a: 所有用户,包含u,g和o

chmod: - change file mode bits
(1)mode表示法:便于操作所有权限位rwx
赋值表示法:u g o a
例:u=rwx,g=rw,o=r
a=775
ug=r,o=

[root@yph7 tmp]# ll -d /tmp/gentoo
d---------. 2 flimmer breeze 27 Dec 13 18:31 /tmp/gentoo
[root@yph7 tmp]# chmod ug=rx,o= /tmp/gshadow
[root@yph7 tmp]# ll -d /tmp/gshadow
-r-xr-x---. 1 root root 1033 Dec 14 00:29 /tmp/gshadow

授权表示法:便于操作某一位权限
u+x,g+r,o+r
a+x
ug+rx,o-

[root@yph7 tmp]# chmod u-x,o+rx /tmp/gshadow
[root@yph7 tmp]# ll -d /tmp/gshadow
-r--r-xr-x. 1 root root 1033 Dec 14 00:29 /tmp/gshadow

(2)八进制表示法:
chmod  775  /etc/text

--reference改成与某文件一样
chmod --reference=/tmp/txt /tmp/yum.1
[root@yph7 ~]# ll /tmp
-rw-r--r--. 1 breeze breeze   0 Dec 13 18:15 txt
-rwxrwxrwx. 1 root   root     0 Dec 13 03:08yum.1
[root@yph7 ~]# chmod --reference=/tmp/txt /tmp/yum.1
[root@yph7 ~]# ll /tmp
-rw-r--r--. 1 breeze breeze   0 Dec 13 18:15 txt
-rwxrwxrwx. 1 root   root     0 Dec 13 03:08yum.1

-R,--recursive:递归修改(一般只用+、-法修改)
例:chmod -R g+r /tmp/sysconfig

[root@yph7 ~]# ll /tmp
drwxrwxrwx. 2 root   root     6 Dec 13 17:16gentoo
drwxrwxrwx. 2 root   root     6 Dec 12 20:06 ta
[root@yph7 ~]# ll -d /tmp
drwxrwxrwx. 10 root root 4096 Dec 13 18:15 /tmp
[root@yph7 ~]# chmod -R go-w /tmp
[root@yph7 ~]# ll -d /tmp
drwxr-xr-x. 10 root root 4096 Dec 13 18:15 /tmp
[root@yph7 ~]# ll /tmp
drwxr-xr-x. 2 root   root     6 Dec 13 17:16gentoo
drwxr-xr-x. 2 root   root     6 Dec 12 20:06 ta

chown:  - change file owner and group

修改文件的属主
drwxr-xr-x. 8 root root 111 Dec 13 18:32 /tmp
[root@yph7 tmp]# chown breeze /tmp;ll -d /tmp
drwxr-xr-x. 8 breeze root 111 Dec 13 18:32 /tmp

修改文件属主和组,"."也可换成冒号
[root@yph7 tmp]# chown flimmer.hadoop /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer hadoop 111 Dec 13 18:32 /tmp
修文件改的属组
[root@yph7 tmp]# chown .stupid /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer stupid 111 Dec 13 18:32 /tmp

-R,--recursive:递归修改,连带修改目录里所有文件
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer hadoop 111 Dec 13 18:32 /tmp
[root@yph7 tmp]# ll /tmp
drwxr-xr-x. 2 root   root   27 Dec 13 18:31 gentoo
-rw-r--r--. 1 breeze breeze  0 Dec 13 18:15 txt
[root@yph7 tmp]# chown -R gentoo:apache /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 gentoo apache 111 Dec 13 18:32 /tmp
[root@yph7 tmp]# ll -R /tmp
/tmp:
total 0
drwxr-xr-x. 2 gentoo apache 27 Dec 13 18:31 gentoo
-rw-r--r--. 1 gentoo apache  0 Dec 13 18:15 txt
/tmp/gentoo:
total 0
-rw-r--r--. 1 gentoo apache 0 Dec 13 18:31 text
-rw-r--r--. 1 gentoo apache 0 Dec 13 18:31 yum

--reference:指定与某文件相同的相关属性
[root@yph7 tmp]# chown -R --reference=/home/flimmer /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer flimmer 111 Dec 13 18:32 /tmp
[root@yph7 tmp]# ll /tmp
total 0
drwxr-xr-x. 2 flimmer flimmer 27 Dec 13 18:31 gentoo
-rw-r--r--. 1 flimmer flimmer  0 Dec 13 18:15 txt

chgrp: - change group ownership修改组

[root@yph7 tmp]# chgrp oracle1 /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer oracle1 111 Dec 13 18:32 /tmp

-R;--recursive:递归修改,连带修改里面所有的目录和文件
--reference:指定与某文件相同的相关属性

[root@yph7 tmp]# chgrp -R --reference=/home/breeze /tmp
[root@yph7 tmp]# ll -d /tmp
drwxr-xr-x. 8 flimmer breeze 111 Dec 13 18:32 /tmp
[root@yph7 tmp]# ll /tmp
total 0
drwxr-xr-x. 2 flimmer breeze 27 Dec 13 18:31 gentoo
-rw-r--r--. 1 flimmer breeze  0 Dec 13 18:15 txt

注意:普通用户仅可修改属主为自己的文件的权限
[breeze@yph7 ~]$ ll
-rw-rw-r--. 1 breeze breeze 0 12月 13 06:18 text
[breeze@yph7 ~]$ chmod 777 text;ll
总用量 0
-rwxrwxrwx. 1 breeze breeze 0 12月 13 06:18 text

思考:用户对目录有写权限,但对目录下的文件没有写权限时,能否修改此文件内容?能否删除此文件?
模拟之;
[root@yph7 tmp]# ll -d /tmp
drwxrwxrwt. 9 root root 4096 Dec 13 06:32 /tmp
[root@yph7 tmp]# chown root:root /tmp/yum.1
[root@yph7 tmp]# ll tmp/yum.1
-rwxr-xr-x. 1 root root 0 Dec 13 03:08 /tmp/yum.1
切换到breeze
[breeze@yph7 tmp]$ head -2 /etc/shells | tee /tmp/yum.1
tee: /tmp/yum.1: 权限不够
/bin/sh
/bin/bash
[breeze@yph7 tmp]$ rm -rf /tmp/yum.1
rm: 无法删除"/tmp/yum.1":不允许的操作
这种情况下正常是可以删除文件但不能修改文件的。但上面演示好像不顺利,原因如下:
用root设置下权限777就好了后来发现上面的的/tmp权限为t,不是x,这是特殊权限,用户只能删除自己的文件,而不能删除其他人的
[root@yph7 tmp]# chmod 777 /tmp

[breeze@yph7 tmp]$ ll -d /tmp
drwxrwxrwx. 9 root root 4096 12月 13 07:00 /tmp
[breeze@yph7 tmp]$ ll /tmp/yum.2
----------. 1 root root 19 12月 13 07:00 /tmp/yum.2
[breeze@yph7 tmp]$ rm -rf /tmp/yum.2
[breeze@yph7 tmp]$ ll /tmp/yum.2
ls: 无法访问/tmp/yum.2:没有那个文件或目录

umask:文件权限反向掩码,遮罩码
文件:666-umask
目录:777-umask

注意:之所以文件用666去减,表示文件默认不能拥有执行权限,如果减得的结果中有执行权限,则需要加1
umask: 023
666-023=644  rw--w--wx
777-023=754
对于文件:666:rw-rw-rw-  023:----w--wx   减后:rw-r--r--:644
umask命令:
umask:查看当前umask
umas kMASK:设置umask

[flimmer@yph7 ~]$ umask
0002
[flimmer@yph7 ~]$ umask 023;umask
0023

注意:此类设定仅对当前shell有效

练习:完成以下任务
1、新建系统组mariadb,新建系统用户mariadb, 属于mariadb组,要求其没有家目录,且shell为/sbin/nologin;尝试root切换至用户,查看其命令提示符;
[root@yph7 tmp]# groupadd -r mariadb
[root@yph7 tmp]# tail -1 /etc/group
mariadb:991:
[root@yph7 tmp]# useradd -r -M -g mariadb -s /sbin/nologinmariadb
[root@yph7 tmp]# tail -1 /etc/passwd
mariadb:994:991::/home/mariadb:/sbin/nologin
[root@yph7 tmp]# id mariadb
uid=994(mariadb) gid=991(mariadb) groups=991(mariadb)
[root@yph7 tmp]# ls /home
bree    fedora   flivfox  hadoop   mageedu  mysql10  mysql3  mysql6  mysqle  slackware
breeze  flimmer  gentoo   magebird  mydql4  mysql2   mysql5  mysql8  oracle1
[root@yph7 tmp]# su - mariadb
su: warning: cannot change directory to /home/mariadb: No suchfile or directory
This account is currently not available.
如果组事先存在,添加与组同名的用户默认添加不成功的
删除用户默认删除基本组
2、新建GID为5000的组mageedu,新建用户gentoo,要求其家目录为/users/gentoo,密码同用户名;
[root@yph7 tmp]# groupadd -g 5000 mageedu
groupadd: GID '5000' already exists
[root@yph7 tmp]# cat /etc/group
.......
distro:5000:
........
[root@yph7 tmp]# groupmod -g 4006 distro  -->修改组id
[root@yph7 tmp]# groupadd -g 5000 mageedu
[root@yph7 tmp]# tail -1 /etc/group
mageedu:5000:
[root@yph7 tmp]# mkdir /users
[root@yph7 users]# useradd -d /users/gentoo gentoo
[root@yph7 users]# tail -1 /etc/passwd
gentoo:4006:5003::/users/gentoo:/bin/bash
[root@yph7 users]# echo "gentoo" | passwd --stdingentoo
Changing password for user gentoo.

3、新建用户fedora,其家目录为/users/fedora,密码同用户名;
[root@yph7 users]# useradd -d /users/fedora fedora
Creating mailbox file: File exists
[root@yph7 users]# tail -1 /etc/passwd
fedora:4007:4007::/users/fedora:/bin/bash
[root@yph7 users]# echo "fedora" | passwd --stdinfedora
Changing password for user fedora.
passwd: all authentication tokens updated successfully.

4、新建用户www, 其家目录为/users/www;删除www用户,但保留其家目录;
[root@yph7 users]# useradd -d /users/www www
[root@yph7 users]# tail -1 /etc/passwd
www:4008:4008::/users/www:/bin/bash
[root@yph7 users]# userdel www;cd /users/www;pwd
/users/www
[root@yph7 www]# id www
id: www: no such user

5、为用户gentoo和fedora新增附加组mageedu;
[root@yph7 www]# id -nG gentoo;id -nG fedora
gentoo
fedora
[root@yph7 www]# usermod -aG mageedu gentoo
[root@yph7 www]# usermod -aG mageedu fedora
[root@yph7 www]# id -nG gentoo;id -nG fedora
gentoo mageedu
fedora mageedu

6、复制目录/var/log至/tmp/目录,修改/tmp/log及其内部的所有文件的属组为mageedu,并让属组对目录本身拥有写权限;
[root@yph7 www]# cp -r /var/log /tmp/
[root@yph7 tmp]# chown -R  .mageedu /tmp/log
[root@yph7 tmp]# chmod g+w /tmp/log
[root@yph7 tmp]# ll -d /tmp/log
drwxrwxr-x. 14 root mageedu 4096 Dec 13 22:08 /tmp/log


install命令:copyfiles and set attributes复制文件并设置属性(不能复制目录)

-m,--mode=MODE:设定目标文件权限,默认755
[root@yph7 tmp]# install -m 632 /tmp/yum.log /tmp/yum ;ll
-rw--wx-w-. 1 root root   0 12月 12 05:08 yum
-rw-------. 1 root root   0 12月  8 19:08 yum.log

-o,--owner=OWNER:设定目标文件属主
[root@yph7 tmp]# install -o flimmer /tmp/yum.log /tmp/yum.1 ;ll
-rwxr-xr-x. 1 flimmer root   0 12月 12 05:16 yum.1

-g,--group=GROUP:设定目标文件属组
[root@yph7 tmp]# install -g flimmer /tmp/yum.log /tmp/yum.2;ll
-rwxr-xr-x. 1 root    flimmer   0 12月 12 05:19 yum.2

-d,--directory:创建目录
[root@yph7 tmp]# install -d /tmp/config



mktemp: create atemporary file or directory创建临时文件或目录(一段时间后会自动清除)
mktemp [OPTION]... [TEMPLATE]
[root@yph7 ~]# mktemp /tmp/a.XXXXXXXX
/tmp/a.L9fheln9

-d,--dirctory:创建临时目录,也要用x
[root@yph7 tmp]# mktemp -d /tmp/XXXXXXXX
/tmp/wf0txj7U

-u,--dry-run:虚拟创建文件,表面创建好了,其实没有。测试服务器是否能正常运行
[root@yph7 tmp]# mktemp -u /tmp/config.XXXXXX
/tmp/config.DAObQW
[root@yph7 tmp]# ls   结果发现没有这个文件

通过变量取随机数
[root@yph7 ~]# myfile=$(basename `mktemp /tmp/XXXXXXXXX )
[root@yph7 ~]# echo "$myfile"
ja9FD8QSf

注意:会将创建的临时文件名直接返回,可通过命令引用保存起来

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
2 [报告]
发表于 2015-12-18 21:30 |只看该作者
w:可修改此目录下文件列表,创建、删除文件

经常会踩坑,不看目录权限,而看文件属主属组权限
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP