在Linux系统中,有7种文件类型。

  1. 普通文件 (regular file)
  2. 目录文件 (directory)
  3. 链接文件 (symbolic link)
  4. 管道文件 (FIFO)
  5. 套接字文件 (socket)
  6. 字符设备文件 (character device)
  7. 块设备文件    (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

扩展阅读:

1. Unix File Permissions

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文件类型及访问权限的更多相关文章

  1. linux 文件类型和权限

    linux 文件类型和权限 ls -l 显示: [user@wyf-201 ~]$ ll total 0 -rw-rw-r--. 1 user user 0 Aug 27 10:49 1.txt dr ...

  2. Linux 文件类型及操作

    一.  文件类型 1.Linux文件类型如下图所示: 2.Linux文件类型有许多种,不同的文件类型代表特殊意义,使用以下命令可以查看文件类型: [root@VMredhat6 ~]# ls  -l  ...

  3. 每天一个linux命令(24):Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  4. CentOS(七)--Linux文件类型及目录配置

    这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...

  5. Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  6. 每天一个linux命令(24)--Linux文件类型与扩展名

    linux 文件类型和Linux 文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如 file.txt  file.tar.gz.这些文件虽然要用不同的程序来打开,但放在Lin ...

  7. linux每日命令(25):Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  8. linux常用命令:Linux 文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  9. 【转】每天一个linux命令(24):Linux文件类型与扩展名

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/22/2781912.html Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概 ...

随机推荐

  1. [leetcode] 6. Balanced Binary Tree

    这个题目纠结了一会儿,终于从二叉树转化到AVL了.题目如下: Given a binary tree, determine if it is height-balanced. For this pro ...

  2. 博客和Github简单练习

    我的第一篇博客 1.首先是自我介绍 姓名:孙弘毅 班级:网工142 学号:1413042050 兴趣:游戏,看书 至于我写了多少代码我也不清楚,反正不多 2.Github  注册流程 (1)百度Git ...

  3. linux查看占用内存多的进程

    update一个简单的方法 ps aux | sort -k4nr | head -10 ps -e  -o "%C  : %p : %z : %a"|sort -k5 -nr|h ...

  4. C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字

    最近写了个小程序,用于将固态硬盘的写入量等信息显示在任务栏,最开始使用Windows API也可以实现,但是当任务栏托盘增加的时候,会被遮盖,最终采用了DeskBand来实现,填了很多坑. 参考的Gi ...

  5. 【cocos2d-x 环境配置-Mac配置篇】

    目前我配置的环境需求如下: JDK 1.6 XCode Version 4.6 (4H127) Cocos2d-x 2.2.0  Android Developer  一,下载安装 要配置环境一次性下 ...

  6. 「HAOI2010」 弹飞绵羊

    题目链接 戳我 \(Solution\) \(LCT\)裸题 我们首先先新建一个节\(n+1\)点,表示被弹飞 对于点\(i,link(i,min(n+1,i+k_i))\) 再看看修改: 现在要将点 ...

  7. iOS NSMutableArray "removeObjectIdenticalTo" vs "removeObject"

    NSMutableArray 有多种可以删除元素的方法. 其中 removeObject,removeObjectIdenticalTo 这两个方法是有区别的. [anArray removeObje ...

  8. DOM学习日记1

    1.通过 id 查找 HTML 元素 var x=document.getElementById("intro"); 2.通过标签名查找 HTML 元素本例查找 id=" ...

  9. bash脚本编程学习笔记(二)

    1.脚本编程之函数 函数是实现结构化编程重要的思想,主要目的是实现代码重用 定义一个函数: function FUNCNAME { command //函数体 }   FUNCNAME(){ //函数 ...

  10. 使用java执行ffmpeg命令进行推流操作

    视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...