Linux服务器开发之:chmod()函数,chmod命令,以及文件屏蔽umask命令,程序修改umask,详细介绍+案例演示
1.依賴的頭文件
#include<sys/stat.h>
2.函數定義:
//通過傳入path中給定的文件名的方式來改變文件制定的權限
int?chmod(const char *path,mode_t mode);
//通過傳入文件描述符的方式為一個文件重設權限
int fchmod(int fd,mode_t mode);
注意:如果使用Linux的chmod命令時,得有root權限
3.關于mode_t的定義;
??A:mode_t的定義實際就是unsigned?int?形式的
? B:但是函數chmod(const char *path,mode_t mode)在解釋mode_t時時將這里的mode當成8進制去解釋
4.參數mode有以下幾種組合:
?????? S_ISUID? (04000)? set-user-ID? (set? process? effective? user?? ID?? on
???????????????????????? execve(2))???????????????????????????????????????????????????????????????????????????????????????????//文件的(set user-id on execution)位
?????? S_ISGID? (02000)? set-group-ID? (set? process? effective? group? ID? on????????
???????????????????????? execve(2);? mandatory? locking,?? as?? described?? in
???????????????????????? fcntl(2);? take a new file's group from parent direc‐
???????????????????????? tory, as described in chown(2) and mkdir(2))????????????????????????????????????//文件的(set group-id on execution)位
?????? S_ISVTX? (01000)? sticky bit (restricted deletion flag, as described in
???????????????????????? unlink(2))???????????????????????????????????????????????????????????????????????????????????????????//文件的sticky位
?????? S_IRUSR? (00400)? read by owner?????????????????????????????????????????????????????????????????????//文件所有者可讀取權限????????????????????????????????????????????????????????????????????
?????? S_IWUSR? (00200)? write by owner??????????????????????????????????????????????????????????????????? //文件所有者具有可寫入權限
?????? S_IXUSR? (00100)? execute/search? by owner ("search" applies for direc‐
???????????????????????? tories, and means that entries within? the? directory
???????????????????????? can be accessed)?????????????????????????????????????????????????????????????????????????????//文件所有者具有可執行權限
?????? S_IRGRP? (00040)? read by group????????????????????????????????????????????????????????????????????//用戶組具有可讀取權限
?????? S_IWGRP? (00020)? write by group???????????????????????????????????????????????????????????????????//用戶組具有可執行權限
?????? S_IXGRP? (00010)? execute/search by group???????????????????????????????????????????????????//用戶組具有可執行權限
?????? S_IROTH? (00004)? read by others???????????????????????????????????????????????????????????????????//其他用戶具有可讀取權限???????????????
?????? S_IWOTH? (00002)? write by others??????????????????????????????????????????????????????????????????//其他用戶具有可寫入權限
?????? S_IXOTH? (00001)? execute/search by others??????????????????????????????????????????????????//其他用戶具有可執行權限
5.比如要將文件test的權限修改為644,那么可以采用以下幾種方法:
???????chmod("文件名",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
?????? chmod("文件名",0644);
?????? chmod("文件名",420);
???????說明:
???????一、第一種方法是將00400和00200和00040和00004進行或運算,最終得到的結果就是0644(八進制),而八進制的0644就等于十進制的420,所以上面的幾種方式等價的。
???????二、當我們給chmod函數傳遞參數時他會將對應的十進制的mode參數轉換為相應的八進制進行運算。所以,當我們要給函數中傳入的文件的權限改為644時傳遞給函數chmod的參數不能直接是644.而應該是420.這是因為十進制的420就等于八進制的644.
???????三、但是我們使用chmod命令時卻可以直接輸入644
??
???????chmod()函數案例:
??????
??????
6.關于Linux中chmod命令的使用:
??????數字表示法:
----------------------------------------------------
??????讀取權限(符號:r)-- 4
??????寫入權限(符號:w)-- 2
??????執行權限(符號:x)-- 1
??????沒有授予權限?-- 0
?????案例說明:
?????chmod 644 test?
?????chmod 666 /mydoc/*????????? //設置目錄mydoc中所有文件的權限
?????chmod?-R 666 /mydoc/*???? //包括目錄
???----------------------------------------------------
???????使用文字改變權限
???????chmod u = rwx file1???????? file1文件的擁有者權限為rwx,??? 這句也可以改成:chmod u+rwx file1
?????? chmod g = rwx file1???????? file1文件的組擁有的權限為rwx,這句也可以改成:chmod g+rwx file1
?????? chmod o?= rwx file1?????????file1文件的其他用戶權限為rwx,這句也可以改成:chmod o+rwx?file1?
???????chmod u-x filex????????????????刪除file1文件擁有者的x權限
???????chmod ug=rw file1???????????同時將file1文件擁有者及組的權限設為rwx
?????? chmod ug+x? file??????????????同時增加file1文件擁有者及組的x權限
-------------------------------------------------------
???????文件權限屏蔽
???????umask [-S] [權限屏蔽]
???????所謂權限屏蔽是由4個八進制數字所組成的,這4個八進制數字會被用來決定新產生文件的訪問權限。umask命令的設置就是不希望新文件的權限被開放。
???????一般而言,系統默認新建文件的訪問權限是0666,新建目錄的默認值是0777.而這個訪問權限減去umask的設置,就是此文件真正的訪問權限。
???????例如:若將umask值設為0222,則每個新建文件的默認權限為:
?????? 0666?- 0022?= 0644(等于rw-r--r--)
???????而目錄的默認權限則為:
?????? 0777?- 0022?= 0755(等于rwxr-xr-x)
???????案例說明:
??????
???????運行結果:
??????
總結
以上是生活随笔為你收集整理的Linux服务器开发之:chmod()函数,chmod命令,以及文件屏蔽umask命令,程序修改umask,详细介绍+案例演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 馒头一般蒸多少分钟才能熟(蒸馒头多长时间
- 下一篇: 齐齐哈哪齐齐哈尔哪里有喜来健体验店个小区