写在前面:

本博客详解命令chmod,  chowm,  chgrp,  umask


linux权限管理

进程文件访问权限应用模型:

进程的属主与文件属主是否相同,如果相同,则应用属主权限

否则,检查文件的属主是否属于文件的属组,如果是,则应用属主权限

否则,应用other权限

 

文件权限的意义:

r:可获取文件数据

w:可修改文件数据

x:可将此文件运行为进程

 

目录权限的意义:

r:可使用ls获取文件列表

w:可修改此目录下文件列表,创建、删除文件

x:cd至此目录中,可以ls -l获取文件详细信息

注意:w不能随便给,rx一般都给

 

--- 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,go

 

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+rxo-

 

[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:查看当前umask

umas kMASK:设置umask

    umask 023

    666-023=644  rw--w--wx

    777-023=754

    对于文件:666rw-rw-rw-  023:----w--wx   减后:rw-r--r--:644


         [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:x:991:

[root@yph7 tmp]# useradd -r -M -g mariadb -s /sbin/nologinmariadb

[root@yph7 tmp]# tail -1 /etc/passwd

mariadb:x: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、新建GID5000的组mageedu,新建用户gentoo,要求其家目录为/users/gentoo,密码同用户名;

[root@yph7 tmp]# groupadd -g 5000 mageedu

groupadd: GID '5000' already exists

[root@yph7 tmp]# cat /etc/group

.......

distro:x:5000:

........

[root@yph7 tmp]# groupmod -g 4006 distro  -->修改组id

[root@yph7 tmp]# groupadd -g 5000 mageedu

[root@yph7 tmp]# tail -1 /etc/group

mageedu:x:5000:

[root@yph7 tmp]# mkdir /users

[root@yph7 users]# useradd -d /users/gentoo gentoo

[root@yph7 users]# tail -1 /etc/passwd

gentoo:x: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:x: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:x: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、为用户gentoofedora新增附加组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