Linux 权限 | 操作命令及权限表示

注:本文为 "Linux 权限" 相关合辑。

英文讨论,机翻未校。

中文引文,略作重排。

如有内容异常,请看原文。


一、基础概念:Linux 权限定义

1.1 权限的作用

Linux 作为多用户多任务操作系统,通过严格的权限机制实现文件访问隔离与系统安全管控,任何用户执行文件相关操作,必须具备对应的权限方可成功。该机制可有效防止未授权程序(如病毒)擅自运行,保障系统稳定。

1.2 权限的构成要素

1.2.1 权限类型

Linux 文件的基础权限分为三类,对应特定字符标识与八进制数值,具体如下:

权限类型 字符标识 八进制数值 权限说明
读权限 r 4 可读取文件内容、查看目录结构
写权限 w 2 可修改文件内容、创建/删除目录内文件
执行权限 x 1 可执行文件、进入目录

1.2.2 权限归属(粒度)

每个文件的权限均针对三类主体配置,称为权限归属粒度,具体如下:

  • 拥有者(u,User):文件的创建者或指定所有者,其权限配置优先级高于其他主体
  • 群组(g,Group):文件所属的用户组,组内用户共享该组对应的权限
  • 其它组(o,Other):既非文件拥有者,也不属于文件所属群组的所有用户

补充说明

  • 单个文件仅能归属一个用户与一个用户组;
  • 其他用户可通过加入文件所属群组,获取该群组对应的权限;
  • 一个用户可同时归属多个用户组。

1.2.3 文件类型标识(权限首位)

Linux 权限表示的首位字符用于标识文件类型,而非权限,常见类型如下:

字符标识 文件类型 说明
d 目录文件 用于存储文件和子目录,如 /home
- 普通文件 常见的文本、可执行程序等,如 a.conf、c.sh
s 套接字文件 用于进程间通信,如 /var/run/docker.sock
p 管道/命名管道文件 用于同一主机内进程间数据传递
l 符号链接文件 指向其他文件/目录的快捷方式,如 ln -s 源文件 链接文件
b 块设备文件 面向块的存储设备,如硬盘 /dev/sda
c 字符设备文件 面向字符的输入输出设备,如键盘 /dev/tty1

二、权限操作:命令使用方法

2.1 更改文件权限(chmod 命令)

chmod 命令用于配置和修改文件/目录的权限,支持两种使用模式:符号模式和数字模式,语法一致。符号模式直观易记,数字模式简洁高效。

2.1.1 通用语法

bash 复制代码
chmod [可选项 ] <mode> <file...>

2.1.2 可选项说明

bash 复制代码
-c, --changes          仅在权限实际变更时输出信息
-f, --silent, --quiet  屏蔽大部分错误提示
-v, --verbose          输出所有文件的权限变更详情
--no-preserve-root     不对根目录 `/` 做特殊处理(默认)
--preserve-root        禁止对根目录执行递归权限修改
--reference=RFILE      参照 RFILE 的权限模式配置目标文件
-R, --recursive        递归修改目录及子目录、文件的权限
--help                 显示命令帮助信息
--version              显示命令版本信息

2.1.3 符号模式

符号模式通过字符组合描述权限变更,格式为:[ugoa...][[+-=][rwxX]...][,...]

  • 权限主体:u(拥有者)、g(群组)、o(其它组)、a(所有主体,即 u+g+o)
  • 操作符:+(追加权限)、-(撤销权限)、=(覆盖权限,清除原有权限后设置新权限)
  • 权限标识:r(读)、w(写)、x(执行)、X(仅对目录或已具备执行权限的文件赋予执行权限)
符号模式应用示例
bash 复制代码
# 1. 为所有用户添加文件 a.conf 的读权限
chmod a+r a.conf  # 等价于 chmod ugo+r a.conf

# 2. 为文件 c.sh 的拥有者配置读、写、执行权限,撤销其他所有权限
chmod u+rwx,go-rwx c.sh

# 3. 为文件 a.conf、b.xml 配置:所有用户可读,拥有者和群组可写,其它组不可写
chmod a+r,ug+w,o-w a.conf b.xml

# 4. 递归为当前目录及子目录下所有文件配置所有用户读写权限
chmod -R a+rw *

2.1.4 数字模式

数字模式通过八进制数值表示权限,逻辑为:将拥有者、群组、其它组的权限分别转换为一位八进制数,组合为三位(基础权限)或四位(含附加权限)数值,直接配置权限。数字模式适合熟练使用者。

权限组合与八进制数值对应关系:

text 复制代码
rwx = 4+2+1 = 7  # 读、写、执行权限
rw- = 4+2 = 6    # 读、写权限,无执行权限
r-x = 4+1 = 5    # 读、执行权限,无写权限
r-- = 4 = 4      # 仅读权限
-wx = 2+1 = 3    # 写、执行权限,无读权限
-w- = 2 = 2      # 仅写权限
--x = 1 = 1      # 仅执行权限
--- = 0 = 0      # 无任何权限

数字模式语法:chmod <abc> file...,其中 a(拥有者)、b(群组)、c(其它组)分别为对应权限的八进制数值。

数字模式应用示例
bash 复制代码
# 1. 所有用户具备读、写、执行权限,该权限存在安全风险,仅可临时使用
chmod 777 file  # 等价于 chmod u=rwx,g=rwx,o=rwx file

# 2. 仅拥有者具备读写权限,群组和其它组无任何权限,适用于个人隐私文件
chmod 600 file  # 等价于 chmod u=rw,g=---,o=--- file

# 3. 拥有者具备读写执行权限,群组和其它组具备读、执行权限,适用于可执行程序
chmod 755 file

# 4. 拥有者具备读写执行权限,群组和其它组仅具备执行权限,适用于公共可执行程序
chmod 711 file

2.2 更改文件拥有者(chown 命令)

chown 命令用于修改文件/目录的所有者及所属群组,仅系统管理员(root)具备执行权限,普通用户无法变更自身或他人文件的所有者。

2.2.1 语法格式

bash 复制代码
chown [可选项 ] user[:group] file...

2.2.2 参数说明

  • 可选项:与 chmod 命令可选项完全一致(如 -R 递归修改)
  • user:新的文件拥有者用户名
  • group:新的文件所属群组名称(可选,省略则仅修改所有者)

2.2.3 应用示例

bash 复制代码
# 1. 将文件 d.key、e.scrt 的所有者设为 tom,所属群组设为 users
chown tom:users d.key e.scrt

# 2. 递归将当前目录及子目录下所有文件的所有者设为 James,所属群组设为 users
chown -R James:users *

三、深入解析:权限表示与附加权限

3.1 十位权限表示(基础权限)

十位权限表示法是最常用的权限展示形式,格式为:文件类型 + 拥有者权限 + 群组权限 + 其它组权限(共 10 位字符)。

3.1.1 常见十位权限对应关系

十位权限字符 八进制数值 权限说明
-rw------- 600 仅拥有者具备读写权限,适用于个人隐私文件(如 ~/.ssh/id_rsa)
-rw-r--r-- 644 拥有者读写,群组和其它组只读,适用于普通配置文件(如 /etc/profile)
-rwx------ 700 仅拥有者具备读写执行权限,适用于个人可执行脚本
-rwxr-xr-x 755 拥有者读写执行,群组和其它组读执行,适用于系统可执行程序(如 /bin/ls)
-rwx--x--x 711 拥有者读写执行,群组和其它组仅执行,适用于公共可执行程序
-rw-rw-rw- 666 所有用户读写,无执行权限,适用于共享配置文件,使用时需谨慎
-rwxrwxrwx 777 所有用户读写执行,为最高权限,存在安全风险,仅可临时使用

3.1.2 十位权限解析逻辑

-rwxr-xr-x 为例,拆解如下:

  • 第 1 位(-):文件类型为普通文件
  • 第 2-4 位(rwx):拥有者权限,具备读、写、执行权限(对应数值 7)
  • 第 5-7 位(r-x):群组权限,具备读、执行权限(对应数值 5)
  • 第 8-10 位(r-x):其它组权限,具备读、执行权限(对应数值 5)

逻辑说明:后 9 位按"3 位一组"划分,分别对应拥有者、群组、其它组,每组内的三位字符依次对应读(r)、写(w)、执行(x)权限,缺失权限用"-"表示。

3.2 十二位权限表示(附加权限)

基础权限仅管控读写执行操作,Linux 额外提供附加权限(SET 位权限、粘滞位权限),用于满足特殊场景需求,其表示形式扩展为十二位二进制,可简化为四位八进制数值(如 4755、6755、7755)。

3.2.1 附加权限分类与定义

(1)SET 位权限(SUID、SGID)

SET 位权限用于临时提升进程身份,仅适用于可执行文件,作用是让普通用户执行特定程序时,临时获得该程序拥有者或所属群组的权限,以实现普通用户执行特权操作的需求。

  • SUID(Set User ID):执行文件时,进程临时获得文件拥有者的 UID(如 passwd 命令,普通用户执行时临时获得 root 权限,用于修改密码)
  • SGID(Set Group ID):执行文件时,进程临时获得文件所属群组的 GID;若应用于目录,目录内新建文件将继承该目录的所属群组

SET 位权限存在安全风险,仅对必要程序配置(如 passwd、sudo),不可滥用。

SET 位权限表示形式(十位字符)

SET 位权限体现在对应主体的执行位(x)上,区分大小写:

  • 小写 s:该主体具备执行权限,同时配置了 SET 位权限(如 -rwsr-xr-x,拥有者具备执行权限,配置 SUID)
  • 大写 S:该主体无执行权限,但配置了 SET 位权限(如 -rwSr--r--,拥有者无执行权限,配置 SUID)
text 复制代码
-rwsr-xr-x  # 配置 SUID,拥有者具备执行权限
-rwSr--r--  # 配置 SUID,拥有者无执行权限
-rwxr-sr-x  # 配置 SGID,群组具备执行权限
-rw-r-Sr--  # 配置 SGID,群组无执行权限
SET 位权限配置命令
bash 复制代码
chmod u+s filename   # 为文件配置 SUID 权限
chmod u-s filename   # 撤销文件的 SUID 权限
chmod g+s filename   # 为文件配置 SGID 权限
chmod g-s filename   # 撤销文件的 SGID 权限
(2)粘滞位权限(Sticky)

粘滞位权限仅适用于目录,作用是即使用户对该目录具备写权限,也仅能删除自身创建的文件,无法删除其他用户创建的文件,适用于共享目录(如 /tmp)。

粘滞位权限表示形式(十位字符)

粘滞位权限体现在"其它组"的执行位(x)上,区分大小写:

  • 小写 t:其它组具备执行权限,同时配置了粘滞位权限(如 -rwsr-xr-t)
  • 大写 T:其它组无执行权限,但配置了粘滞位权限(如 -rwSr--r-T)
粘滞位权限配置命令
bash 复制代码
chmod +t 目录名   # 为目录配置粘滞位权限
chmod -t 目录名   # 撤销目录的粘滞位权限

3.2.2 十二位权限的结构与转换

(1)十二位权限结构

十二位二进制权限按位定义,对应关系如下(位序从左到右为 11 至 0):

text 复制代码
位序:11 10 9  8 7 6  5 4 3  2 1 0
标识:S  G  T  r w x  r w x  r w x
  • 第 11 位(S):SUID 标识位(1=配置,0=未配置)
  • 第 10 位(G):SGID 标识位(1=配置,0=未配置)
  • 第 9 位(T):粘滞位标识位(1=配置,0=未配置)
  • 第 8-0 位:基础权限位(与十位权限的后 9 位一致,对应拥有者、群组、其它组的 rwx 权限)
(2)附加权限的八进制数值对应

将十二位权限按"3 位一组"拆分,转换为四位八进制数值,其中前三位(SGT)对应附加权限的八进制数值:

  • SUID 对应八进制数值:4(二进制 100)
  • SGID 对应八进制数值:2(二进制 010)
  • 粘滞位对应八进制数值:1(二进制 001)

附加权限组合数值:4(仅 SUID)、6(SUID+SGID)、1(仅粘滞位)、7(SUID+SGID+粘滞位)。

(3)四位八进制权限的使用

语法格式:chmod <sabc> file...,其中:

  • s:附加权限的八进制数值(0=无附加权限,4=SUID,2=SGID,1=粘滞位,6=SUID+SGID,7=全附加权限)
  • a、b、c:分别为拥有者、群组、其它组的基础权限数值(与三位八进制权限一致)
典型四位八进制权限对应关系
text 复制代码
-rw-r-Sr--  = 010 110 100 100  = 2644  # 配置 SGID,无执行权限
-rwsr-xr-x  = 100 111 101 101  = 4755  # 配置 SUID,基础权限 755
-rwsr-sr-x  = 110 111 101 101  = 6755  # 配置 SUID+SGID,基础权限 755
-rwsr-sr-t  = 111 111 101 101  = 7755  # 配置 SUID+SGID+粘滞位,基础权限 755
四位八进制权限应用示例
bash 复制代码
# 1. 配置基础权限 755(拥有者 rwx,群组/其它组 r-x),附加 SUID 权限
chmod 4755 netlogin
# 说明:普通用户执行 netlogin 时,临时获得该文件拥有者(如 root)的权限,可访问受保护资源

# 2. 配置基础权限 755,附加 SUID+SGID 权限
chmod 6755 netlogin

# 3. 配置基础权限 755,附加全量附加权限(SUID+SGID+粘滞位)
chmod 7755 netlogin

四、常见问题与补充说明

4.1 为什么使用 4755 而非 755?

以 root 创建的上网认证程序 netlogin 为例:

  • chmod 755 netlogin:普通用户可执行该程序,但无法访问 root 专属资源(如系统认证文件),可能导致程序执行失败;
  • chmod 4755 netlogin:普通用户执行该程序时,临时获得 root 权限,可正常访问专属资源,完成认证操作。

4.2 权限配置注意事项

  • 避免随意配置 777 权限,该权限会导致所有用户可修改、删除文件,存在严重安全风险;
  • SET 位权限仅对可执行文件有效,对普通文件、目录配置无效;
  • 粘滞位权限仅对目录有效,对普通文件配置无效;
  • 递归修改权限(-R)时,需谨慎操作,避免误改系统基础文件权限(如 /etc、/bin 目录)。

4.3 权限查询命令补充

使用ls -l 命令可查看文件/目录的权限、所有者、所属群组等信息,示例:

bash 复制代码
ls -l /etc/profile

输出示例:-rw-r--r-- 1 root root 1714 6 月 7 2023 /etc/profile

解析:权限 -rw-r--r--(644),所有者 root,所属群组 root


What are correct permissions for /tmp ? I unintentionally set it all public recursively

I have created a really really short life temporary directory that I wanted to share between some users for a few hours : /some/path/tmp

我创建了一个生命周期极短的临时目录 /some/path/tmp,原本希望在若干用户之间共享使用数小时。

Unfortunately I have launched sudo chmod 777 -R /tmp instead of sudo chmod 777 -R tmp, so my /tmp file is now completely public.

但我误执行了 sudo chmod 777 -R /tmp,而非 sudo chmod 777 -R tmp,导致 /tmp 目录现在权限完全公开。

Is it a security concern now that it is completely set to public? Should I change it back to more secure settings? What are the correct permissions for /tmp?

将其权限完全公开是否存在安全风险?我是否应恢复为更安全的配置?/tmp 的标准权限应当是什么?

edited Jun 13, 2024 at 5:59

Jonathan Gilbert

asked Apr 8, 2013 at 0:38

Stephane Rolland

Beware that I forgot something important in my initial answer: the X11 socket needs to be publicly accessible, otherwise you won't be able to start new GUI applications. I've updated my answer.

需要注意,我在最初回答中遗漏了一项重要内容:X11 套接字需要全局可访问,否则将无法启动新的图形界面应用程序。我已更新回答内容。

-- Gilles 'SO- stop being evil'

Commented Apr 8, 2013 at 9:36

Was it chown or chmod?

你执行的是 chown 还是 chmod

-- Melebius

Commented Jul 26, 2018 at 20:29

@Melebius I am a bit puzzled by your question: I did not mention chmod. chown (like ch - own), is about ownership of files, which user owns a file. chmod (like ch - modifify) is more about who can execute, or write inside, or read the content of a file.

我对你的问题有些疑惑:我并未提及 chmodchown 用于设置文件归属,即文件所属用户;chmod 用于控制用户对文件的执行、写入与读取权限。

-- Stephane Rolland

Commented Jul 29, 2018 at 8:17

@StephaneRolland Yes, that's what confused me. You can use chown 777 which sets the ownership of a file to the user with the ID 777. However, all the answers, including the accepted one, work with chmod. Since all of them set the permission to the same value for all the users (owner, group, others), most effects of the file ownership become irrelevant. However, the correct command to rectify the result of sudo chown 777 -R /tmp should be sudo chown root -R /tmp.

这正是我产生疑惑的地方。chown 777 会将文件归属设为 UID 为 777 的用户。但所有回答(包括采纳答案)均围绕 chmod 展开。由于这些命令对所有者、所属组、其他用户设置了相同权限,文件归属的影响基本可以忽略。不过,若要修正 sudo chown 777 -R /tmp 造成的结果,正确命令应为 sudo chown root -R /tmp

-- Melebius

Commented Jul 29, 2018 at 13:04

Answers

The normal settings for /tmp are 1777 aka a=rwx,o+t, which ls shows as drwxrwxrwt. That is: wide open, except that only the owner of a file (or of /tmp, but in that case that's root which has every right anyway) can remove or rename it (that's what this extra t bit means for a directory).
/tmp 的标准权限为 1777,等价于 a=rwx,o+tls 显示为 drwxrwxrwt。其含义为:目录全局可读写执行,但仅文件所有者(或 /tmp 所有者,此处为 root,本身拥有全部权限)可删除或重命名文件,这就是目录上附加 t 位的作用。

The problem with a /tmp with mode 777 is that another user could remove a file that you've created and substitute the content of their choice.

权限为 777 的 /tmp 存在的问题是,其他用户可删除你创建的文件并替换为任意内容。

If your /tmp is a tmpfs filesystem, a reboot will restore everything. Otherwise, run chmod 1777 /tmp.

/tmp 为 tmpfs 文件系统,重启即可恢复;否则执行 chmod 1777 /tmp

Additionally, a lot of files in /tmp need to be private. However, at least one directory critically needs to be world-readable: /tmp/.X11-unix, and possibly some other similar directories (/tmp/.XIM-unix, etc.). The following command should mostly set things right:

此外,/tmp 中大量文件需要保持私有。但至少有一个目录必须全局可读:/tmp/.X11-unix,还可能包含其他同类目录,如 /tmp/.XIM-unix 等。以下命令可基本修复权限问题:

bash 复制代码
chmod 1777 /tmp
find /tmp \
-mindepth 1 \
-name '.*-unix' -exec chmod 1777 {} + -prune -o \
-exec chmod go-rwx {} +

I.e. make all files and directories private (remove all permissions for group and other), but make the X11 sockets accessible to all. Access control on these sockets is enforced by the server, not by the file permissions. There may be other sockets that need to be publicly available. Run find /tmp -type s -user 0 to discover root-owned sockets which you may need to make world-accessible. There may be sockets owned by other system users as well (e.g. to communicate with a system bus); explore with find /tmp -type s ! -user $UID (where $UID is your user ID).

即:将所有文件与目录设为私有(移除所属组与其他用户的全部权限),但保证 X11 套接字全局可访问。这类套接字的访问控制由服务程序实现,而非文件权限。系统中可能存在其他需要全局可访问的套接字,可执行 find /tmp -type s -user 0 查找 root 所属的套接字并按需开放全局权限。同时也可能存在其他系统用户所属的套接字(如用于系统总线通信),可通过 find /tmp -type s ! -user $UID 排查,其中 $UID 为当前用户 ID。

edited Jun 13, 2024 at 6:55

Stéphane Chazelas

answered Apr 8, 2013 at 0:47

Gilles 'SO- stop being evil'

could you explain the second chmod more?

能否详细解释第二条 chmod 命令?

-- Bartlomiej Lewandowski

Commented Apr 8, 2013 at 8:38

@BartlomiejLewandowski go-rwx: no permissions for group and others. This sets the permissions to rwx------ (except that files that were created since the chmod may end up with fewer permissions, e.g. rw-------). In other words, the files will be accessible only by their owner. /tmp/.[!.]* is to include dot files, which commonly exist in /tmp.
go-rwx 表示移除所属组与其他用户的所有权限,将权限设为 rwx------;部分新创建文件权限可能更少,如 rw-------。换言之,文件仅对所有者可访问。/tmp/.[!.]* 用于匹配 /tmp 中常见的隐藏文件。

-- Gilles 'SO- stop being evil'

Commented Apr 8, 2013 at 9:28

The +t is referred to as the sticky bit. That's what keep anyone other than the owner from being able to remove files, even though the permissions are 777 otherwise. The sticky bit was originally to get the kernel to leave commonly programs in memory when they exited so they wouldn't have to be fetched from disk when next run. We're talking PDP11 days...
+t 称为粘滞位。即便目录权限为 777,粘滞位也会阻止非所有者删除文件。粘滞位最初用于让内核在常用程序退出后将其保留在内存中,避免下次运行时重新从磁盘加载,这一设计可追溯至 PDP‑11 时代。

-- kurtm

Commented Oct 8, 2013 at 21:51

@GabrielFair I replaced the command using wildcards by one using find which won't run into that problem.

我已将通配符命令替换为 find 命令,可避免相关问题。

-- Gilles 'SO- stop being evil'

Commented Apr 21, 2018 at 8:58

@alper No, do not do that. 1777 is for /tmp itself, which is writable by everybody. Almost all directories in /tmp should only be readable and writable by their owner.

请勿执行该操作。1777 仅适用于 /tmp 本身,用于实现全局可写;/tmp 下几乎所有子目录都应仅对其所有者可读可写。

-- Gilles 'SO- stop being evil'

Commented Jul 28, 2020 at 7:06

/tmp and /var/tmp should have read, write and execute rights for all; but you'd usually would also add the sticky-bit (o+t), to prevent users from removing files/directories belonging to other users. So chmod a=rwx,o+t /tmp should work.
/tmp/var/tmp 应支持所有用户的读、写、执行权限,同时通常需要添加粘滞位 o+t,防止用户删除他人文件或目录。因此执行 chmod a=rwx,o+t /tmp 即可。

As for changing permissions recursively... As long as the owner/group remains as it is for the files and directories, it shouldn't be that much of a problem. But you could perhaps change the permission of everything under /tmp (not /tmp itself) to ensure users' privacy, by removing the rx rights of others and perhaps the group.

关于递归修改权限......只要文件与目录的所有者和所属组保持不变,问题通常不大。但可修改 /tmp 内部内容(不含 /tmp 自身)的权限,移除其他用户及所属组的读、执行权限,以保障用户隐私。

Find is a good way of doing this. As root, do:
find 是实现该操作的合适工具,以 root 身份执行:

复制代码
cd /tmp
find . -type f -exec chmod u=rw,go= {} \; # (or u=rw,g=r,o= {})
find . -type d -exec chmod u=rwx,go= {} \; # (or u=rwx,g=rx,o= {})

edited Apr 8, 2013 at 22:50

Gilles 'SO- stop being evil'

answered Apr 8, 2013 at 10:05

Baard Kopperud

On a typical desktop system, you'd better make /tmp/.X11-unix/* world-readable as well, or you won't be able to start X applications any more.

在常规桌面系统中,需保证 /tmp/.X11-unix/* 全局可读,否则将无法启动 X 应用程序。

-- Gilles 'SO- stop being evil'

Commented Apr 8, 2013 at 22:51

chmod a=rwX,o+t /tmp -R should do the find magic.
chmod a=rwX,o+t /tmp -R 可实现与上述 find 命令相似的效果。

-- dhill

Commented Feb 4, 2016 at 22:12

@dhill, your command ends up with strange permissions: -rw-rw-rwT those are not the original permissions

该命令会产生异常权限 -rw-rw-rwT,与标准权限不符。

-- rubo77

Commented Apr 12, 2020 at 4:29


Tab completion errors: bash: cannot create temp file for here-document: No space left on device

When using the tab bar, I keep getting this error:

使用 Tab 补全时持续出现如下错误:

bash: cannot create temp file for here-document: No space left on device

bash:无法为 here-document 创建临时文件:设备空间不足

Any ideas?

请问该如何解决?

I have been doing some research, and many people talk about the /tmp file, which might be having some overflow. When I execute df -h I get:

我查阅了相关资料,多数观点指向 /tmp 目录空间溢出。执行 df -h 结果如下:

bash 复制代码
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.1G 8.7G 0 100% /
udev 10M 0 10M 0% /dev
tmpfs 618M 8.8M 609M 2% /run
tmpfs 1.6G 0 1.6G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 1.6G 0 1.6G 0% /sys/fs/cgroup
/dev/sda1 511M 132K 511M 1% /boot/efi
/dev/sda4 1.8T 623G 1.1T 37% /home
tmpfs 309M 4.0K 309M 1% /run/user/116
tmpfs 309M 0 309M 0% /run/user/1000

It looks like the /dev/data directory is about to explode, however if I tip:

表面上 /dev/sda2 空间占满,但执行如下命令:

bash 复制代码
$ du -sh /dev/sda2
0 /dev/sda2

It seems it's empty.

显示其占用空间为 0。

I am new in Debian and I really don't know how to proceed. I used to typically access this computer via ssh. Besides this problem I have several others with this computer, they might be related, for instance each time I want to enter my user using the GUI (with root it works) I get:

我刚接触 Debian 系统,不知如何处理。我通常通过 SSH 访问该计算机。除该问题外还存在其他异常,可能彼此相关。例如每次以普通用户登录图形界面时(root 登录正常),出现如下提示:

Xsession: warning: unable to write to /tmp: Xsession may exit with an error

Xsession 警告:无法写入 /tmp,Xsession 可能异常退出

edited Nov 19, 2017 at 19:37

Evan Carroll

asked Apr 18, 2016 at 22:07

lucasrodesg

You want to run something like du -hxd1 /, not du /dev/sda2. /dev/sda2 doesn't really exist on disk.

应执行 du -hxd1 / 而非 du /dev/sda2/dev/sda2 为设备文件,并非实际磁盘目录。

-- muru

Commented Apr 18, 2016 at 22:13

Answers

If anyone gets here with this error when their disk isn't full, be sure to check not just df but also df -i. There are a fixed number of inodes on a filesystem, and every file needs one. If you have just tons of small files, it's very easy for your filesystem to fill up with these small files while there's still plenty of space left on the drive when you run df.

若磁盘空间充足却出现该错误,除 df 外务必检查 df -i。文件系统的索引节点(inode)数量固定,每个文件均需占用一个索引节点。若存在大量小文件,极易出现 df 显示空间充足但索引节点耗尽的情况。

answered Nov 26, 2018 at 10:53

Michael Speer

This was the problem I had! I kept trying to find what was occupying space. That wasn't the problem at all. I had fully used up the inodes. /dev/root 4980000 4980000 0 100% / Maybe the system should respond with an appropriate error message?

这正是我遇到的问题!我一直排查空间占用,却忽略了索引节点耗尽。/dev/root 4980000 4980000 0 100% /,系统或许应给出更准确的错误提示。

-- Capstone

Commented May 22, 2019 at 6:34

Same for me, Plesk server with nginx sometimes crashing and sometimes working, couldn't work out what was wrong until one of our techs got this error on the command line. The number of nodes rather then the space was the issue as identified by df -i. Thanks

我也遇到相同问题。搭载 nginx 的 Plesk 服务器运行不稳定,直至技术人员在命令行发现该错误,通过 df -i 确认是索引节点耗尽而非空间不足。

-- Parvez Saleh

Commented Apr 9, 2020 at 9:03

What am I supposed to do to solve this issue?

我该如何解决该问题?

-- Shahriar Rahman Zahin

Commented Apr 26, 2021 at 8:16

@Shahriar Rahman Zahin if you have too many files, you'll have to get rid of some. move important stuff elsewhere, delete unneeded old logs or similar. if it's your software, maybe change it to use fewer files ( use an sqlite db instead of filesystem as db perhaps? ), you could use a cron job to purge old unneeded logs and make sure temp files aren't left sitting around. if you control the filesystem, backup and recreate with more inodes available perhaps? I can't give specific answers as I don't know your situation. good luck.

若文件数量过多,需清理部分文件:将重要数据迁移至其他位置,删除无用旧日志等文件。若是自研程序,可优化为减少文件数量,例如改用 SQLite 数据库替代文件存储。可通过定时任务清理过期日志,避免临时文件堆积。若可控制文件系统,可备份数据后重新创建文件系统并分配更多索引节点。因不了解具体环境,无法给出详细方案。

-- Michael Speer

Commented Apr 27, 2021 at 14:16

@Shahriar Rahman Zahin re: increasing inodes unix.stackexchange.com/questions/26598/...

关于增加索引节点的相关内容。

-- Michael Speer

Commented Apr 27, 2021 at 14:17

Your root file system is full and hence your temp dir (/tmp, and /var/tmp for that matter) are also full. A lot of scripts and programs require some space for working files, even lock files. When /tmp is unwriteable bad things happen.

根文件系统空间占满,导致 /tmp/var/tmp 无可用空间。大量脚本与程序需要空间存储工作文件与锁文件,/tmp 不可写会引发一系列异常。

You need to work out how you've filled the filesystem up. Typically places this will happen is in /var/log (check that you're cycling the log files). Or /tmp may be full. There's many, many other ways that a disk can fill up, however.

需要定位空间占用来源,常见位置为 /var/log,需检查日志轮转配置;也可能是 /tmp 占用过高。磁盘空间占满的原因还有很多。

bash 复制代码
du -hs /tmp /var/log

You may wish to re-partition to give /tmp it's own partition (that's the old school way of doing it, but if you have plenty of disk it's fine), or map it into memory (which will make it very fast but start to cause swapping issues if you overdo the temporary files).

可重新分区为 /tmp 分配独立分区,该方式为传统方案,在磁盘空间充足时适用;也可将其挂载为内存文件系统,提升读写速度,但临时文件过多时会引发交换分区占用。

edited Nov 19, 2017 at 19:33

Evan Carroll

answered Apr 18, 2016 at 22:48

Miles Gillham

Hi, I looked at both commands that you suggest and I would say that both /tmp and /var/log are quite empty: 60K and 49M respectively.

我执行了你建议的命令,/tmp/var/log 占用空间很小,分别为 60 K 与 49 M。

-- lucasrodesg

Commented Apr 19, 2016 at 19:18

Hi again. I finally got it. I don't know why did I place all the owncloud content under /var. It works again!

问题已解决,我误将 owncloud 数据存放于 /var 目录,清理后恢复正常。

-- lucasrodesg

Commented Apr 19, 2016 at 19:32

For anyone using ncdu to track down what's taking up space, try running it with sudo. It didn't show me the true size of /var/log until I ran it with sudo because /var/log/httpd was owned by root:root.

使用 ncdu 排查空间占用时,建议添加 sudo 执行。/var/log/httpd 归属 root:root,无管理员权限无法显示真实大小。

-- Illya Moskvin

Commented Jul 2, 2021 at 5:32

the du -hs /tmp /var/log command needs to be executed as super user (sudo) to avoid "Permission denied" errors while reading the aforementioned directories' content
du -hs /tmp /var/log 需以管理员权限执行,避免读取目录时出现权限拒绝错误。

-- KiriSakow

Commented Jul 23, 2022 at 23:56

You may also have lost write access to the /tmp/ directory.

也可能是 /tmp 目录丢失写入权限。

It should look like that:

正常权限应如下所示:

bash 复制代码
ls -l / |grep tmp
drwxrwxrwt 7 root root 4096 Nov 7 17:17 tmp

You can fix the permissions like that:

可通过如下命令修复权限:

bash 复制代码
chmod a+rwxt /tmp

answered Nov 7, 2016 at 17:38

dothebart

This worked for me!

该方法解决了我的问题!

-- Joseph Chambers

Commented Jun 15, 2018 at 5:36

That's a useless use of grep. Try ls -ld /tmp instead.

此处无需使用 grep,直接执行 ls -ld /tmp 即可。

-- user

Commented Feb 18, 2019 at 15:18

you just halted a near full on panic attack...worth a vote up for me

该回答解决了紧急问题,值得点赞。

-- sbeskur

Commented Nov 15, 2019 at 0:11

The fastest way to locate your folders that are too full is by narrowing down the folder file size in levels from the root folder. You start with the root folder by:

从根目录开始逐层排查,是定位空间占用过大目录的高效方式。首先执行:

bash 复制代码
sudo du -h --max-depth=1 /

Then - EITHER you increase the depth, i.e. the levels below:

之后可增加目录深度继续排查:

bash 复制代码
sudo du -h --max-depth=2 /

OR - quicker - you you look which folder has eaten up the most disk space, and do the same on this folder:

或直接定位占用最大的目录,对其执行相同命令:

bash 复制代码
sudo du -h --max-depth=1 /home/<user>/<overfull-folder>

Once you found it, just remove that one:

定位目标目录后直接删除:

bash 复制代码
rm -rf <path to overfull-folder>

answered Jun 10, 2018 at 3:47

Agile Bean

with many output lines it's nice to sort them by size with sudo du -h --max-depth=1 / | sort -h (bigger files at the bottom or sort -hr for bigger files on top)

输出内容较多时,可通过 sudo du -h --max-depth=1 / | sort -h 按空间大小排序,升序排列;sort -hr 为降序排列。

-- William Ranvaud

Commented Jul 16, 2019 at 21:42

This helped me to solve my problem. I was struggling with some hours with this.

该方法帮我解决了困扰数小时的问题。

-- AnotherDeveloper

Commented Jan 5, 2021 at 5:05

I was getting error, then I saw

我出现错误后查看到如下信息:

bash 复制代码
[ 672.995482] EXT4-fs (sda2): Remounting filesystem read-only
[ 672.999802] EXT4-fs error (device sda2): ext4_journal_check_start:60: Detected aborted journal

I was able to confirm this,

并通过如下命令确认:

bash 复制代码
mount | grep -i sda2
/dev/sda2 on / type ext4 (ro,relatime,errors=remount-ro,data=ordered)

answered Nov 19, 2017 at 19:36

Evan Carroll



reference