特殊權限
前面我們學習過linux的基本權限,但如果只有基本權限,可能無法滿足各式各樣的要求
例如:建立一個公共目錄 任何人都可以在目錄裏建立自己的文件,但只能刪除自己的文件,此時基本權限就無能爲力了.
如果你想要完成這種需求就必須要藉助linux的特殊權限;特殊權限可以更精密的定義文件的權限;
之前我們看到的umask是0022,其中第一個0就是描述的特殊權限.
這類特殊權限共有三種;
suid sgid sticky在瞭解特殊權限的功能前,先來複習一下基本權限的獲取流程
開始--->[ root ]--->[ 賦予所有權限 ]---/
[ user ]--->[ 委派user位權限 ]----/
[ group]--->[ 委派group位權限]----/ 結束
[ other]--->[ 委派other位權限]---/
那現在來看下第一個特殊權限 SUID
限定:只能設置在二進制可執行程序上,對目錄無效和文本無效
功能:不管誰來執行程序,linux都以程序的擁有者身份進入權限獲取流程中從而決定存取權限
特徵:在uesr位的x顯示爲S或s,s代表包含了x權限,S代表未包含x權限
試驗演示:
試驗一: 用戶修改密碼藉助root身份
# ll /usr/bin/passwd
-rwsr-xr-x 1 root root /usr/bin/passwd
# ll /etc/shadow
-r-------- 1 root root /etc/shadow
# chmod u-s /usr/bin/passwd
# ll /usr/bin/passwd
-rwxr-xr-x 1 root root /usr/bin/passwd
# su - seker
$ passwd
Changing password for user seker.
Changing password for seker
(current) UNIX password:
passwd: Authentication token manipulation error
$
試驗二:用戶無法讀取/etc/shadow,借用root身份使用cat命令則可
# su - seker
$ cat /etc/shadow
cat: /etc/shadow: 權限不夠
$ exit
logout
# chmod u+s /bin/cat 用戶權限添加執行權限
# su - seker
$ cat /etc/shadow
root:$1$EV/a2BnK$pRN0qjwqLf8zvpK8w1MFT.:14360:0:99999:7:::
瞭解了SUID,我們再來看看SGID
限定:SGID既可以作用於二進制文件又可以作用於目錄,但兩者的意義卻截然不同
功能:
先說在二進制文件上,與前面講的SUID類似:不管是誰來執行,都以文件的所屬組身份來決定權限
學員自己測試
再說作用於目錄上:默認情況下用戶建立文件時,文件的所屬組是用戶的主組,如果在設置了SGID目錄下建立文件,則文件的所屬組是繼承目錄
的屬組,並且新建立的目錄也繼承g+s權限 組權限添加執行權限
特徵:在group位的x顯示爲S或s,s代表包含了x權限,S代表未包含x權
試驗一:
# mkdir /home/public
# chmod g+s !$
# su - seker
$ cd ../public
$ touch sgid_yes
$ ll sgid_yes
-rw-rw-r-- 1 seker root sgid_yes
確切的說:UID GID共有四種,
一種就是前面我們學習的,也是常見的用戶的UID和GID,它們的真實有名字叫做真實UID和真實GID
而另外兩種叫做EUID EGID,就是有效UID和有效GID.有效的這組是爲進程訪問文件存取而存在的.
我們的命令大部分都會產生進程;系統就可靠euid egid來判斷能否存取文件
在沒有set之前 euid=uid egid=gid;而設置了後,則各自獨立;
-
set uid 會改變euid;
-
set gid 會改變egid;
[seker@stu254 ~]$ id zorro
uid=501(zorro) gid=501(zorro) groups=501(zorro)
[seker@stu254 ~]$
[seker@stu254 ~]$ id
uid=500(seker) gid=500(seker) groups=500(seker)
[seker@stu254 ~]$ ll /bin/cat
---S-----x 1 zorro zorro 23100 2006-11-28 /bin/cat
[seker@stu254 ~]$ ll /opt/file
----r----- 1 root seker 7 06-15 19:19 /opt/file
[seker@stu254 ~]$ cat /opt/file
sdfsdf
cat文件被設置了SET UID,則seker用戶執行時有效這組是這樣: euid=501(zorro) egid=500(seker)
針對/opt/file的權限進入權限匹配流程
-
是否是root --> 否
-
是否是user --> 否
-
是否是group --> 是 於是拿到 r-- 的權限 所以能查看/opt/file的內容
那現在把/opt/file改動一下
[root@stu254 opt]# chown :zorro /opt/file
[seker@stu254 ~]$ ll /opt/file
----r----- 1 root zorro 7 06-15 19:19 /opt/file
[seker@stu254 ~]$
[root@stu254 opt]# cat /opt/file
cat: /opt/file: 權限不夠
[root@stu254 opt]#
是否是root --> 否
是否是user --> 否
是否是group --> 否
於是拿到others的 --- 的權限 所以不能查看/opt/file的內容
[seker@stu254 ~]$
[seker@stu254 ~]$ id seker
uid=500(seker) gid=500(seker) groups=500(seker)
[seker@stu254 ~]$ ll /bin/cat
------s--x 1 zorro zorro 23100 2006-11-28 /bin/cat
[seker@stu254 ~]$ ll /opt/file
----r----- 1 root zorro 7 06-15 19:19 /opt/file
[seker@stu254 ~]$ cat /opt/file
sdfsdf
[seker@stu254 ~]$
cat 被設置了 set gid,則seker用戶執行時有效這組是這樣:euid=500(seker) egid=501(zorro)
是否是root --> 否
是否是user --> 否
是否是group --> 是 於是拿到 r-- 的權限 所以能查看/opt/file的內容
sticky 冒險位(黏貼位)
限定:只作用於目錄
功能:任何人都可以在一個目錄下建立文件,卻只有root和建立者本人纔可以刪除文件
特徵:在other位的x顯示爲T或t,t代表包含了x權限,T代表未包含x權限
# ll /tmp -d
drwxrwxrwt 5 root root /tmp
# su - seker
$ cd /tmp
-rw------- 1 zorro zorro zorro-file
$ rm -rf zorro-file
rm: 無法刪除 ”r;zorro-file”: 不允許的操作
設定方法:
字符模式
chmod u+s file
chmod g+s dir/file
chmod o+t dir
數字模式:
chmod 4755 file
chmod 2755 dir/file
chmod 1777 dir
文字處理高級命令
輸入輸出重定向
標準輸入 設備:鍵盤 文件 標記:0
標準輸出 設備:屏幕 終端 標記:1
錯誤輸出 設備:屏幕 終端 標記:2
輸入輸出流程:
APP 輸入<-- 鍵盤
|
APP 處理
|
/ 1 標準輸出 /
APP 輸出--> ---> 屏幕
/ 2 錯誤輸出 /
如果我想把一個程序的輸出錯誤存放到單獨的一個文件中,那麼我們在這個流程中該如何介入呢?
其實很簡單,只是用標記符來控制輸入的源和輸出的目標.
試驗:
ls > out.file 將標準輸出定向到文件 如果文件不存在則創建,如果文件存在則覆蓋
ls >> out.file 將標準輸出定向到文件 如果文件不存在則創建,如果文件存在則追加
ls 2> err.file 將標準輸出定向到文件 如果文件不存在則創建,如果文件存在則覆蓋
ls 2>> err.file 將標準輸出定向到文件 如果文件不存在則創建,如果文件存在則追加
ls > out.file 2> err.file 將標準輸出與標準錯誤分別定向到文件
ls &> all.file 將標準錯誤和標準輸出合併定向到文件
ls >/dev/null 2>&1 講標準錯誤和標準輸出合併定向到系統黑洞
cat < infile 將文件內容讀出做cat命令的輸入
# cat << EOF here document
> 123
> abc
> EOF
123
abc
#
用here document避免交互輸入
# passwd << EOF
> linuxcom
> linuxcom
> EOF
Changing password for user root.
passwd: all authentication tokens updated successfully.
#
/dev/null /dev/zero 介紹
/dev/null 是系統的黑洞
/dev/zero 是系統的零發生器
dd if=/dev/zero of=./big_file bs=10 count=1M
wc 計算文件的行數,單詞數,字節數
# wc /etc/passwd
40 59 1800 /etc/passwd
# wc -l /etc/passwd
40 /etc/passwd
# wc -w /etc/passwd
59 /etc/passwd
# wc -c /etc/passwd
1800 /etc/passwd
#
cut 按列提取文件
-d 指明列分隔符 -f 選擇輸出的區域 -c 指定字符位置
# cut -d: -f 1,7 /etc/passwd |head -n 2
root:/bin/bash
bin:/sbin/nologin
# cut -c 1-3,6-9 /etc/passwd |head -n 2
tr 字符的刪除替換
-d 刪除
# tr -d : < /etc/passwd |head -n 2
替換
# tr [a-z] [A-Z] < /etc/passwd |head -n 2
sort 排序輸出
默認按首字符從頭至尾的順序排序
-r 逆序
-n 按數字排序
-t 指明分隔符 與 -k 連用
-k 按指定的域排序
sort -t: -gk 3 /etc/passwd
# sort /etc/passwd -t: -gk 3
diff 對比兩文件的差異
d 刪除了(delete) -a 新增了(append) -c 改變了(change)
cp /etc/passwd .
刪幾行 改幾行 加幾行
diff /etc/passwd passwd
做解釋
| 管道妙用
將上一個命令的標準輸出,傳遞給下一個命令做標準輸入
cat /etc/passwd | head -n 3 | cut -d: -f 1,3,7 |sort -rt: -k 3 |tr [a-z] [A-Z] | wc
xargs
前面我們學習了管道,管道只是讓後面的命令從前一個命令獲取輸入
那我們要建立一個/etc/passwd第一域(用戶名)的目錄的話,只利用管道就無法實現了
# cut -d: -f 1 /etc/passwd |head -n 5 | mkdir
mkdir: 缺少操作數
請嘗試執行”r;mkdir --help”來獲取更多信息。
# cut -d: -f 1 /etc/passwd |head -n 5 | xargs mkdir
# ls
adm bin daemon lp root
#
AWK/SED簡單使用
awk -F: '{print}' file
sed -n 's/old/new/p' file
grep家族
grep
fgrep
pgrep
egrep
正則介紹
^ 行首
$ 行尾
. 除了換行符以外的任意單個字符
* 前導字符的零個或多個
.* 所有字符
[] 字符組內的任一字符
[^] 對字符組內的每個字符取反(不匹配字符組內的每個字符)
^[^] 非字符組內的字符開頭的行
[a-z] 小寫字母
[A-Z] 大寫字母
[a-Z] 小寫和大寫字母
[0-9] 數字
/< 單詞頭 單詞一般以空格或特殊字符做分隔,連續的字符串被當做單詞
/> 單詞尾
擴展正則 sed 加 -r 參數 或轉義
grep 加 -E 或 egrep 或轉義
AWK 直接支持
sed -n '/roo/?/p' /etc/passwd
sed -rn '/roo?/p' /etc/passwd
? 前導字符零個或一個
+ 前導字符一個或多個
abc|def abc或def
a(bc|de)f abcf 或 adef
x/{m/} x出現m次
x/{m,/} x出現m次至多次(至少m次)
x/{m,n/} x出現m次至n次
查找文件
which 搜索命令的位置 搜索的源是內存中的命令別名和$PATH
# which lslll
/usr/bin/which: no lslll in ($PATH)
# which ls
alias ls='ls --color=tty'
/bin/ls
#
locate 搜索所有文件 搜索的源始updatadb庫 庫定期更新 所以不能搜到最新的資料
locate passwd | head -n 3
find 搜索真是文件系統,搜索方式多樣
find .
-type 類型 f d l p c b
-name 名稱 可以通配
-size 大小 +1M 大於1M,-1M 小於1M,1M 等於1M
-user 文件擁有者
-group文件屬組
-maxdepth 搜索的目錄級別
-perm 權限 有+ -時0是通配;
+代表(或)三組權限匹配其中之一;比如 r-x 滿足r-- --x r-x 三個都成立
-代表(與)三組權限同時匹配; 比如 r-xr-xr-x 滿足r----xr-x 也算成立
-o 或
-not 非
-ls 詳細信息
-exec CMD {} /; -ok CMD {} /;
-mtime +3 從當天向歷史天數推算的第三天前(三天前 不包含第三天)
-atime -3 從當前向歷史天數推算的前三天至當天這個段範圍
-ctime 3 從當天向歷史天數推算的第三天
與管道連用 | xargs
壓縮和解壓
.gz
解壓1:gunzip FileName.gz
解壓2:gzip -d FileName.gz
壓縮:gzip FileName
.bz2
解壓1:bzip2 -d FileName.bz2
解壓2:bunzip2 FileName.bz2
壓縮: bzip2 -z FileName
創建各種不同類型的壓縮文件
tar cvf etc_init.d.tar /etc/init.d/
tar xvf etc_init.d.tar /etc/init.d/
查看壓縮文件中的內容
tar tvf etc_init.d.tar
tar 不過是一個打包工具;
若需要進行對打包文件進行壓縮 還需要其他工具gzip gunzip bzip2 bunzip2
這些工具已經被tar所集成
tar cvzf etc_init.d.tar.gz /etc/init.d/
tar cvjf etc_init.d.tar.bz2 /etc/init.d/
zip etc-backup.tar.bz2.zip etc-backup.tar.bz2
unzip etc-backup.tar.bz2.zip
gz gunzip etc-backup.gz
備份還原dump restore
tar也可以備份,若對小量數據備份沒有問題,但數據量每日的地增量不多,原始數據又很大的話
用tar備份就很不適合了.因爲相同的數據每天都要重複備份,既佔空間又耗費時間和資源
用dump則可以做差異備份
差異:只做上一次備份後的變更數據
備份級別 0-9,0是完全備份,1,2,3...做上一次備份後的變更數據
-u 更新 /etc/dumpdatas 數據庫
-f 備份文件
試驗:
完全備份/boot分區到/tmp/boot.dump文件
# dump -0uf /tmp/boot.dump /boot
備份自上一次備份(0級)後的所有變更數據
# dump -1uf /tmp/boot.dump /boot
備份自上一次備份(1級)後的所有變更數據
# dump -2uf /tmp/boot.dump /boot
備份自上一次備份(0級)後的所有變更數據,也自0級備份後的所有變更
# dump -1uf /tmp/boot.dump /boot
查看備份文件中的內容
restore -tf /tmp/boot.dump
恢復 完全恢復 指定文件恢復
完全恢復
# restore -rf /tmp/boot.dump
交互式部分恢復
# restore -if /tmp/boot.dump
restore > ls initrd-2.6.18-128.el5.img
initrd-2.6.18-128.el5.img
restore > add initrd-2.6.18-128.el5.img
restore > ls initrd-2.6.18-128.el5.img
*initrd-2.6.18-128.el5.img
restore >
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
Mount tape volume 1
Enter ``none'' if there are no more tapes
otherwise enter tape name (default: /tmp/boot.dump)
resync restore, skipped 3 blocks
set owner/mode for '.'? [yn] y
restore >
add後文件會被標記爲*
因爲備份時可能會分片,我們備份出來的只是一個文件,所以寫1就好了.如果是多個片的話則逐一指明.