linux特殊許可權和進階命令—-宋軼聰

來源:互聯網
上載者:User

特殊許可權

前面我們學習過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的許可權進入許可權匹配流程

  1. 是否是root  --> 否

  2. 是否是user  --> 否

  3. 是否是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就好了.如果是多個片的話則逐一指明.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.