玩Linux五年積累的技巧(1) -- 檔案的屬性

來源:互聯網
上載者:User

玩Linux五年積累的技巧(1) -- 檔案的屬性 本文主要介紹了檔案的隱藏屬性和特殊許可權,一個讓指令碼具有類似SUID位的技巧,還有一個解決mp3檔案亂碼的方法。  www.2cto.com  作者 JH Gao 從最開始接觸Linux到現在已經有5年了,和所有人一樣,少不了折騰。折騰後偶爾我會把方法記錄下來,現在簡單總結一下。所以的命令功能通過man都能找到具體用法,我只把自己覺得常用的列舉出來。 1. 檔案的隱藏屬性  www.2cto.com  lsattr : 列出檔案的隱藏屬性chattr : 修改檔案的隱藏屬性[plain] [root@www ~]# chattr [+-=][ASacdistu] FileName   + : Add one attribute  - : Remove one attribute   = : Set to be the only attributes that the files have  重要選項  "a":只能追加檔案的內容,但不能修改或刪除內容  "i":檔案不能被刪除、改名、不能建立指向它的連結,不能向檔案寫內容   2. 檔案的特殊許可權 SUID/SGID/Sticky Bit 如果對一個可執行檔設定了SUID或者SGID位,則檔案執行時,將會擁有檔案所有者(設定了SUID)或者所在組(設定了SGID)的許可權。例子:普通使用者不能開啟httpd服務,因為httpd服務需要用到80連接埠,而1024以下的連接埠只有root使用者才能使用。如果我們把httpd可執行檔的所有者設定為root,同時設定SUID位,則普通使用者也可以開啟httpd服務了。對一個目錄設定了Sticky位,則只有檔案的所有者能刪除這個檔案。在Linux系統中,/tmp目錄預設設定了這個位:[plain] drwxrwxrwt 12 root root 16384 Mar 6 09:04 tmp/  主要使用方法如下:[plain] SUID  對於檔案:以檔案所有者的許可權運行  對於目錄:不能對目錄設定SUID  設定SUID:  chmod u+s FILE chmod 4755 FILE    SGID  對於檔案:以檔案所屬組的許可權運行  對於目錄:目錄裡面的檔案會繼承目錄的屬性  設定SGID:  chmod g+s FILE/DIR chmod 2771 FILE/DIR    Sticky  對於檔案:不能對檔案設定Sticky位  對於目錄:對於該目錄下的檔案,只有它們的所有者才能刪除它們。  設定Sticky:  chmod o+t DIR chmod 1777 DIR    用字母設定特殊許可權:  u+s g+s o+t  用數字表示特殊許可權,則是:  4 for SUID  2 for SGID  1 for Sticky   需要注意的是,shell、python、perl等指令檔不能設定SUID位,因為它們事實上是由bash、python、perl解譯器解釋啟動並執行。要讓指令檔也有類似於SUID這樣的功能,需要做一點小花招。簡單地說,我們需要一層殼,這層殼可以設定SUID/SGID位,殼裡面真正工作的還是指令碼。比如說我們有一個指令碼/home/jh/bin/myscript.sh,所有者是普通使用者,但指令碼裡面的操作需要root許可權,現在我們用C語言來寫這層殼,名稱叫做transeuid.c:[cpp] /* author: JH Gao  # Create Date: 2012-06-05 # Function: transmit euid and egid to other scripts #   since shell/python/... scripts can't get suid permission in Linux # ******************************************************************** */    #include  #include  #include  #define BUFFSIZE 1024    /*  * usually euid is the uid who run the program  * but when stick is setted to the program  * euid is the uid or the program's owner  */  int main(int argc, char *argv[]) {      char *cmd = "/home/jh/bin/myscript.sh";      char *pars[] = {"/home/jh/bin/myscript.sh", "par1", "par2"};      // set uid and gid to euid and egid      setuid(geteuid());      setgid(getegid());      if (execvp(cmd, pars)) {          printf("error");          free(cmd);          exit(1);      }      free(cmd);  }   編譯這個程式,在給這個程式設定希望取得的使用者,再設定suid,然後就可以用這個使用者的許可權執行指令碼或命令了: [plain] $ gcc -t transeuid transeuid.c   $ sudo chown root transeuid   $ sudo chmod +s transeuid   $ ./transeuid  ......DO SOMETHING   當然具體要執行的指令碼和參數可以從外部擷取,具體看我以前寫的:《傳遞euid和egid給指令碼,使指令碼具有特殊使用者的許可權》。下文:

-------------------------------------------------------
傳遞euid和egid給指令碼,使指令碼具有特殊使用者的許可權 使指令碼實作類別似於設定了stick位的效果   www.2cto.com  作者:高鵬 <gaopenghigh@gmail.com>  shell, python, perl等指令碼、程式不能取得suid,因為這些指令碼程式需要解譯器-/bin/bash, /usr/bin/python等來執行,而這些解譯器本身沒有suid也不方便設定suid。碰到這種情況可以用c寫一個外殼,對這個外殼設定suid,而在c程式裡面把自身的uid,gid傳遞給實際執行任務的指令碼。(這個方法是在讀周鵬(Roc Zhou <roczhou.zhoup@alibaba-inc.com>)寫的工具時學到的)   www.2cto.com  c程式如下: C代碼  /* # ScriptName: transeuid.c # Author: JH Gao <gaopenghigh@gmail.com> # Create Date: 2012-06-05 # Function: transmit euid and egid to other scripts #   since shell/python/... scripts can't get suid permission in Linux #   usage: transeuid xxx.sh par1 par2 par3 #          xxx.sh will get the euid and egid from transeuid # ******************************************************************** */    #include <stdio.h>  #include <stdlib.h>  #include <unistd.h>  #define BUFFSIZE 1024    /*  * usually euid is the uid who run the program  * but when stick is setted to the program  * euid is the uid or the program's owner  */  int main(int argc, char *argv[]) {      char *cmd = malloc(BUFFSIZE);      // set uid and gid to euid and egid      setuid(geteuid());      setgid(getegid());      cmd = argv[1];      int i = 0;      for(i = 0;i < argc - 1;i++) {          argv[i] = argv[i+1];      }      argv[argc-1] = NULL      // search $PATH find this cmd and run it with pars:argv      if (execvp(cmd, argv)) {          printf("error");          free(cmd);          exit(1);      }      free(cmd);  }    編譯這個程式,在給這個程式設定希望取得的使用者,再設定suid,然後就可以用這個使用者的許可權執行指令碼或命令了: Shell代碼  $ gcc -t transeuid transeuid.c  $ sudo chown root transeuid  $ sudo chmod +s transeuid  $ ./transeuid ls /root /home  /home:  .  ..  data  .directory  gp_old  jh  jh_old  lost+found    /root:  .  ..  .bash_history  .bashrc  .cache  .dbus  .profile  .pulse  .pulse-cookie  .viminfo  

-------------------------------------------------------------------------------------------------但是需要注意的是,這種花招有很大的安全隱患。  3. 最後Linux中解決mp3檔案亂碼的命令是: [plain] find . iname "*.mp3" -execdir mid3iconv -e gbk --remove-v1 {} \; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.