在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. Android-自定义TabHost

    效果图: 布局代码相关: <!-- 自定义简单的TabHost选项卡 --> <LinearLayout xmlns:android="http://schemas.and ...

  2. docker查看挂载目录命令

    docker inspect -f "{{.Mounts}}"  692691b7416 692691b7416为containerId

  3. 解决“找不到请求的 .Net Framework Data Provider。可能没有安装.”错误

    问题: 这几天在装.NET 的开发环境,在装好VS2013和Oracle 11g之后,做了一个测试项目,运行调试没问题 但是涉及到数据库相关操作,如新建数据集.连接数据库等在调试的时候则会出现如下错误 ...

  4. WPF透明窗体不支持缩放解决方案

    方案一 WPF中的无边框透明窗体,由于没有边并且透明,窗体无法进行缩放操作,今天来讲解如何解决这个问题. 先说一下思路,我们先手为该窗体添加4个边,4个角用于缩放操作,然后再为他们写事件,完成拖放操作 ...

  5. 如何让Syncthing始终使用同一个设备ID?

    设备ID(device id)是Syncthing最重要的参数之一,所有节点的发现.连接等操作,全部是基于这个设备ID!对于已经建立起来的P2P网络,保持重要节点的设备ID唯一性是非常重要的!!!!那 ...

  6. 【12c OCP】最新CUUG OCP-071考试题库(52题)

    52.(12-11) choose the best answer: Examine the structure and data in the PRICE_LIST table: You plan ...

  7. Code Chef IMPO(计算几何+扫描线+积分)

    题面 传送门 前置芝士 扫描线,积分求面积 题解 我怎么老是忘了积分可以求面积-- 首先,这两个投影的最小的\(x\)坐标和最大的\(x\)坐标必须相等,否则肯定无解 我们考虑一种方法,枚举\(x\) ...

  8. LOJ#3083. 「GXOI / GZOI2019」与或和(单调栈)

    题面 传送门 题解 按位考虑贡献,如果\(mp[i][j]\)这一位为\(1\)就设为\(1\)否则设为\(0\),对\(or\)的贡献就是全为\(1\)的子矩阵个数,对\(and\)的贡献就是总矩阵 ...

  9. 洛谷P1742 最小圆覆盖(计算几何)

    题面 传送门 题解 之前只是在抄题解--这篇才算是真正自己想的吧-- 首先我们把输入序列给\(random\)一下防止出题人好心送你一个毒瘤序列 我们设\(r\)为当前最大半径,\(o\)为此时对应圆 ...

  10. 浅析group by,having count()

    SELECT COUNT(*) FROM (SELECT COUNT(id),order_type,city_id,category_id,major_category_id,puid,user_id ...