在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. [转载]WCF系列_分布式事务(下)

    浏览到chnking的WCF的分布式事务处理不错,转载过来分享一下. 1. WCF分布式事务例子这里也用转账的例子说事.用户在系统A和系统B都有账户,账户间的资金可以互转,系统A的资金减少多少,系统B ...

  2. Ubuntu下Jenkins(docker)配置Docker远程启动

    背景: 在做用Jenkins构建docker的操作,需要用Jenkins调用docker命令,需要先安装docker-build-step插件,然后开启docker远程访问. 默认情况下,Docker ...

  3. log4net 日志打印不全

    程序用的是log4net打印日志,偶现日志打印不全的问题,程序的log4net配置如下: <log4net> <root> <level value="ALL& ...

  4. C# print pos winform

    先将pos机设置为默认 控制面板->打印机和传真->右键->服务器属性 首先创建 ClassPrint 对象 using System; using System.Drawing; ...

  5. Java50道经典习题-程序5 判断分数等级

    题目:利用三元运算符来完成此题:从键盘录入一个整型的分数,没有负分满分为100分,学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示.分析:三元运算符的格式为:逻 ...

  6. mysql遇到的问题:can't creat/write to file "/var/mysql/xxxx.MYI"

    这个问题困扰了我,可能有两个原因. 1.文件夹权限不够,至少也要给出 USERS 组的可读可写权限: 2.文件夹的磁盘满了,文件写不进去了: 如果是这个不能创建和写的问题,很大的概率就是文件的权限.没 ...

  7. “全栈2019”Java异常第二十章:自定义异常详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  8. 操作mysql的指令

    1,通过ip,端口,用户名,密码登陆数据 命令格式为:mysql -h ip -u root -p -P 3306例如:mysql -h 127.0.0.1 -u root -p -P 3306 2, ...

  9. python获取aliyun ECS实例

    #!/usr/bin/env python #-*- coding:utf-8 -*- # Description : get ecs from aliyun # Author : quke # Da ...

  10. Jmeter之线程组详解

    hello,更新几天的分享,线程数现在才分享,感觉怪怪的,原谅我没有考虑到一个顺序问题哈,那里总结好了,我就发那里,先把组件都写完,再来项目实战,希望大家不要责怪哈,内容有写的不详细的,或者我说错了, ...