Unix/Linux文件类型及访问权限
在Linux系统中,有7种文件类型。
- 普通文件 (regular file)
- 目录文件 (directory)
- 链接文件 (symbolic link)
- 管道文件 (FIFO)
- 套接字文件 (socket)
- 字符设备文件 (character device)
- 块设备文件 (block device)
在Solaris上,还有一种文件类型, 叫做door文件。
而一个文件的Unix访问权限,包括12位,通常用4个8进制位表示,
| 位标志 | 八进制值 | 含义 |
| S_ISUID | 04000 | set user ID on execution [1] |
| S_ISGID | 02000 | set group ID on execution [2] |
| S_ISVTX | 01000 | sticky bit [3] |
| S_IRUSR | 00400 | owner has read permission |
| S_IWUSR | 00200 | owner has write permission |
| S_IXUSR | 00100 | owner has execute permission |
| S_IRGRP | 00040 | group has read permission |
| S_IWGRP | 00020 | group has write permission |
| S_IXGRP | 00010 | group has execute permission |
| S_IROTH | 00004 | others have read permission |
| S_IWOTH | 00002 | others have write permission |
| S_IXOTH | 00001 | others have execute permission |
通常我们使用"chmod 777 <file>", 本质上是"chmod 0777 <file>"。
SUID, SGID, SVTX不常见,这里重点解释一下。
[1] SUID bit: Set User ID 位
When you execute a program that has the SUID bit enabled, you inherit the
permissions of that program's owner. Programs that do not have the SUID bit
set are run with the permissions of the user who started the program.
[2] SGID bit: Set Group ID位
The set-group-ID bit (S_ISGID) has several special uses. For a directory it
indicates that BSD semantics is to be used for that directory: files created
there inherit their group ID from the directory, not from the effective group ID
of the creating process, and directories created there will also get the S_ISGID
bit set. For a file that does not have the group execution bit (S_IXGRP) set,
the set-group-ID bit indicates mandatory file/record locking.
[3] SVTX bit: Sticky(粘滞)位
// From: https://en.wikipedia.org/wiki/Sticky_bit
When a directory's sticky bit is set, the filesystem treats the files in such
directories in a special way so only the file's owner, the directory's owner,
or root user can rename or delete the file. Without the sticky bit set, any user
with write and execute permissions for the directory can rename or delete
contained files, regardless of the file's owner. Typically this is set on the
/tmp directory to prevent ordinary users from deleting or moving other users' files.
e.g. (目录/tmp的sticky位已经设置,虽然文件/tmp/dorax的权限是0777, 但是它不能被用户veli删除,因为其ower是用户fanw。)
$ id -un
veli
$ stat /tmp | egrep 'Access:.*Uid'
Access: (/drwxrwxrwt) Uid: ( / root) Gid: ( / root)
$ ls -l /tmp/dorax
-rwxrwxrwx fanw fanw Feb : /tmp/dorax
$ rm -f /tmp/dorax
rm: cannot remove ‘/tmp/dorax’: Operation not permitted
扩展阅读:
2. Unix - File Permission / Access Modes
最后,给出一个文件类型及权限位识别的C代码实现。
/*
* foo.c - get Unix/Linux file type and its access,
* mostly simliar to what GNU stat does
*/ #include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h> int
main(int argc, char *argv[])
{
char *filepath = NULL;
char *filetype = NULL;
char s[] = "----------";
int rc;
struct stat sb; if (argc != ) {
(void) fprintf(stderr, "Usage %s <file>\n", argv[]);
return (-);
} filepath = argv[]; (void) memset(&sb, , sizeof (struct stat)); errno = ;
/* NOTE: don't use stat() as we will get symbolic link file type */
if ((rc = lstat(filepath, &sb)) != ) {
(void) fprintf(stderr, "%s: cannot stat `%s': %s\n",
argv[], filepath, strerror(errno));
return (-);
} /* get file type */
if (S_ISREG(sb.st_mode)) {
s[] = '-';
filetype = "regular file";
}
if (S_ISDIR(sb.st_mode)) {
s[] = 'd';
filetype = "directory";
}
if (S_ISCHR(sb.st_mode)) {
s[] = 'c';
filetype = "character special file";
}
if (S_ISBLK(sb.st_mode)) {
s[] = 'b';
filetype = "block special file";
}
if (S_ISFIFO(sb.st_mode)) {
s[] = 'p';
filetype = "fifo";
}
if (S_ISLNK(sb.st_mode)) {
s[] = 'l';
filetype = "symbolic link";
}
if (S_ISSOCK(sb.st_mode)) {
s[] = 's';
filetype = "socket";
}
#ifdef __sunos
if (S_ISDOOR(sb.st_mode)) {
s[] = 'D';
filetype = "door";
}
#endif /* parse set UID, set-group-ID, sticky bit */
if (sb.st_mode & S_ISUID)
s[] = 'S';
if (sb.st_mode & S_ISGID)
s[] = 'S';
if (sb.st_mode & S_ISVTX)
s[] = 'T'; /* parse owner rwx bit */
if (sb.st_mode & S_IRUSR)
s[] ='r';
if (sb.st_mode & S_IWUSR)
s[] ='w';
if (sb.st_mode & S_IXUSR)
s[] = (s[] == 'S') ? 's' : 'x'; /* parse group rwx bit */
if (sb.st_mode & S_IRGRP)
s[] = 'r';
if (sb.st_mode & S_IWGRP)
s[] = 'w';
if (sb.st_mode & S_IXGRP)
s[] = (s[] == 'S') ? 's' : 'x'; /* parse others rwx bit */
if (sb.st_mode & S_IROTH)
s[] = 'r';
if (sb.st_mode & S_IWOTH)
s[] = 'w';
if (sb.st_mode & S_IXOTH)
s[] = (s[] == 'T') ? 't' : 'x'; (void) printf(" File: %s\n", filepath);
(void) printf("Access: (%04o/%s)\n", (int)(sb.st_mode & ~S_IFMT), s);
(void) printf(" Type: %s\n", filetype); return ();
}
o 在Linux上编译并测试
$ gcc -g -Wall -m32 -o foo foo.c $ ./foo /usr/bin/passwd
File: /usr/bin/passwd
Access: (/-rwsr-xr-x)
Type: regular file $ ./foo /var/tmp
File: /var/tmp
Access: (/drwxrwxrwt)
Type: directory $ ./foo /bin/sh
File: /bin/sh
Access: (/lrwxrwxrwx)
Type: symbolic link $ mkfifo /tmp/fifo
$ ./foo /tmp/fifo
File: /tmp/fifo
Access: (/prw-rw-r--)
Type: fifo $ ./foo /dev/null
File: /dev/null
Access: (/crw-rw-rw-)
Type: character special file $ ./foo /dev/sda
File: /dev/sda
Access: (/brw-rw----)
Type: block special file $ ./foo /var/tmp/.sshmux
File: /var/tmp/.sshmux
Access: (/srw-------)
Type: socket
o 在Solaris上编译并测试
$ gcc -D__sunos -g -Wall -m64 -o foo foo.c $ ./foo /usr/bin/passwd
File: /usr/bin/passwd
Access: (/-r-sr-sr-x)
Type: regular file $ ./foo /var/tmp
File: /var/tmp
Access: (/drwxrwxrwt)
Type: directory $ ./foo /dev/null
File: /dev/null
Access: (/lrwxrwxrwx)
Type: symbolic link $ mkfifo /tmp/fifo && ./foo /tmp/fifo
File: /tmp/fifo
Access: (/prw-r--r--)
Type: fifo $ ./foo /var/tmp/.sshmux
File: /var/tmp/.sshmux
Access: (/srw-------)
Type: socket $ ./foo /devices/pseudo/mm@:null
File: /devices/pseudo/mm@:null
Access: (/crw-rw-rw-)
Type: character special file $ ./foo /devices/pci@,/pci8086,@/pci1000,@/sd@,:wd
File: /devices/pci@,/pci8086,@/pci1000,@/sd@,:wd
Access: (/brw-r-----)
Type: block special file $ ./foo /var/run/zonestat_door
File: /var/run/zonestat_door
Access: (/Drw-r--r--)
Type: door
Unix/Linux文件类型及访问权限的更多相关文章
- linux 文件类型和权限
linux 文件类型和权限 ls -l 显示: [user@wyf-201 ~]$ ll total 0 -rw-rw-r--. 1 user user 0 Aug 27 10:49 1.txt dr ...
- Linux 文件类型及操作
一. 文件类型 1.Linux文件类型如下图所示: 2.Linux文件类型有许多种,不同的文件类型代表特殊意义,使用以下命令可以查看文件类型: [root@VMredhat6 ~]# ls -l ...
- 每天一个linux命令(24):Linux文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...
- CentOS(七)--Linux文件类型及目录配置
这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...
- Linux文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...
- 每天一个linux命令(24)--Linux文件类型与扩展名
linux 文件类型和Linux 文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如 file.txt file.tar.gz.这些文件虽然要用不同的程序来打开,但放在Lin ...
- linux每日命令(25):Linux文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...
- linux常用命令:Linux 文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...
- 【转】每天一个linux命令(24):Linux文件类型与扩展名
原文网址:http://www.cnblogs.com/peida/archive/2012/11/22/2781912.html Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概 ...
随机推荐
- Linux(一) - Unix&Linux 历史
Unix Unix 的诞生 Unix的历史可以追溯到20世纪60年代中期,当时麻省理工学院,AT&T,贝尔实验室和通用电气公司联合开发了一种名为Multics的操作系统,Multics 中存在 ...
- 看懂gc日志
使用的是:+PrintGCDetails -XX:+PrintGCTimeStamps 输出的日志格式: [Times: user=0.03 sys=0.00, real=0.01 secs] 363 ...
- daylyknowledge1
1.数据库截取字符串:toFixed():四舍五入substring(cp_introduce,0,11) cp_introduce前台截取: field: 'an_content', title: ...
- xml和JSON格式相互转换的Java实现
依赖的包: json-lib-2.4-jdk15.jar ezmorph-1.0.6.jar xom-1.2.1.jar commons-lang-2.1.jar commons-io-1.3.2.j ...
- php中的XML DOM(10)
1.PHP DOM (1) Php中的DOM跟javascript不一样,属性不用另外增加一个节点 2.主要类 DOMDocument :文档类 DOMNodeList :节点列表类 DOMNode ...
- NOI2019省选模拟赛 第五场
爆炸了QAQ 传送门 \(A\) \(Mas\)的童年 这题我怎么感觉好像做过--我记得那个时候还因为没有取\(min\)结果\(100\to 0\)-- 因为是个异或我们肯定得按位考虑贡献了 把\( ...
- Docker的安装与启动教程
一.安装Docker Docker官方建议在Ubuntu中安装,因为Docker是基于Ubuntu发布的,而且一般Docker出现的问题Ubuntu是最先更新或者打补丁的.在很多版本的CentOS中是 ...
- 敏感词过滤的算法原理之 Aho-Corasick 算法
参考文档 http://www.hankcs.com/program/algorithm/implementation-and-analysis-of-aho-corasick-algorithm-i ...
- Keil RTX使用 os_mut_init 报Hard Fault 错误解决记录
首先确定你的软件是在互斥信号初始化的位置,在以下几个位置,将会报Hard Fault 错误: (1).os_sys_init_user 用户线程创建之前 (2).os_tsk_create_user之 ...
- PHP错误——Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题. 这里有两种方法解决 1.修改php.ini memory_limit = 128 打开终端输入下列bash命令 cd /pr ...