在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. Git Note - Branch

    1. add a new branch cd workspace git branch user1/newbranch1 git checkout user1/newbranch1 or git ch ...

  2. Tempdb--查看TempDB上是否需要增加文件

    SELECT [owt].[session_id], [owt].[exec_context_id], [owt].[wait_duration_ms], [owt].[wait_type], [ow ...

  3. maven项目打jar包

    打包有两种方式: 1.直接 项目--右键--export,选择JAR file打包(不推荐这种方式): 这样直接打的包通过java -jar 会提示“没有主清单属性”,需要修改jar包中的MANIFE ...

  4. C#注册表操作类(完整版) 整理完整

    /// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...

  5. 读取本地json文件,转出为指定格式json

    引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件 StreamReader sr = new StreamReader(@" ...

  6. python中的函数(基础)

    1.什么是函数 函数是指将一组数据的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用函数名即可 (函数就是对功能或者动作的封装) 2.函数的语法和定义 def 函数名() 函数体 调用: ...

  7. django 模型 使用 DateTimeFields 字段 auto_now_add 属性 实现 插入数据时 自动记录时间

    class MyModel(models.Model): user_name = models.CharField() created = models.DatedTimeField(auto_now ...

  8. vue $emit 父组件与子组件之间的通信(父组件向子组件传参)

    1.首先新建一个子页面为 env.vue的文件(名字这里大家可以自取) 2.然后把子页面引入父页面,代码如图: import env from '@/components/common/env' ex ...

  9. 【vim】正常模式下的一般操作

    正常模式一般用于浏览文本,其实也就是通过键盘命令让光标在文本中跳来跳去,在任何模式下按一次或两次<Esc>会进入正常模式. 基本思想 vim对光标的定位操作非常精确和高效,这是它的一个非常 ...

  10. python------对于面向对象的理解

    python中一切皆为对象 其实面向对象没什么高大上的东西,只不过把我们平时对于事物的描述和动作系统的总结成了一个定义事物的方法而已. 我们平时向别人介绍一个他(她)从未见过的东西,会从外形和外貌特征 ...