寫在前面:
本博客詳解命令chmod, chowm, chgrp, umask
linux權限管理
進程文件訪問權限應用模型:
進程的屬主與文件屬主是否相同,如果相同,則應用屬主權限
否則,檢查文件的屬主是否屬於文件的屬組,如果是,則應用屬主權限
否則,應用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:查看當前umask
umas kMASK:設置umask
umask: 023
666-023=644 rw--w--wx
777-023=754
對於文件:666:rw-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、新建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: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、爲用戶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