AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 410759
Accepted
terdon
terdon
Asked: 2017-12-14 14:47:00 +0800 CST2017-12-14 14:47:00 +0800 CST 2017-12-14 14:47:00 +0800 CST

从什么时候开始 POSIX 和 GNU rm 不删除 /?

  • 772

几年来,除非使用该选项调用 GNU 实用程序,否则它rm不会删除。然而,该命令长期以来一直被集体潜意识视为危险,人们仍然经常将其称为“可怕”的命令。/--no-preserve-rootrm -rf /

我想知道这个rm不能删除的规则是什么时候/出现的。我检查了 POSIX 规范,我可以看到虽然POSIX:2008包含此安全功能,但POSIX:2001没有。由于POSIX规范的在线版本不时更新,随着每个新的子发布,我也检查了回程机器,找到了2010年的POSIX:2008的相关页面,并且能够确认rm无法删除的规则/那时已经上市了。

所以,我的问题是:

  • rm无法删除的规则何时/添加到 POSIX 规范中?它是在最初的 2008 年版 Single UNIX Specification 第 4 版中还是在修订版中添加的?
  • 这个限制是什么时候添加到 GNU 的rm?我很确定它是在它被添加到 POSIX 之前,但它是什么时候发生的?
posix rm
  • 1 1 个回答
  • 3083 Views

1 个回答

  • Voted
  1. Best Answer
    Stéphane Chazelas
    2017-12-14T15:19:55+08:002017-12-14T15:19:55+08:00

    您可以在线找到所有 POSIX 2008 版本的 HTML 版本:

    • 原文: http: //pubs.opengroup.org/onlinepubs/9699919799.2008edition/utilities/rm.html
    • TC1(2013 版)http://pubs.opengroup.org/onlinepubs/9699919799.2013edition/utilities/rm.html
    • TC2(2016 版)http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/rm.html

    这是在 2008 年版中添加的。

    技术勘误通常不会添加新功能。

    您可以看到以前的版本 ( http://pubs.opengroup.org/onlinepubs/009695399/utilities/rm.html ) (POSIX 2004) 没有该文本。

    新文本在2003-05-09 奥斯汀集团会议上被接受,以包含在该标准的后续修订版中。

    同年 3 月,Sun Microsystems 的 John Beck 提出了请求(链接需要 opengroup 注册,另请参阅此处的增强请求编号 5)。

    约翰贝克在 2003 年 3 月 11 日星期二写道:

    @ page 820 line 31681-31683 section rm comment {JTB-1}
    
    Problem:
    
    Defect code :  3. Clarification required
    
    An occasional user mistake, with devastating consequences, is to
    write a shell script with a line such as:
          rm -rf $VARIABLE1/$VARIABLE2
    or
          rm -rf /$VARIABLE1
    without verifying that either variable is set, which can lead to
          rm -rf /
    being the resulting command.  Since there is no plausible
    circumstance under which this is the desired behavior, it seems
    reasonable to disallow this.  Such a safeguard would, however,
    violate the current specification.
    
    Action:
    
    Either extend the exceptions for . and .. on the noted lines
    to list / as well, or specify that the behavior of rm if an
    operand resolves to / is undefined.
    

    GNU在此 2003-11-09 提交rm中添加了--preserve-root和--no-preserve-root选项,但仅在此 2006-09-03 提交中成为默认值,因此在 coreutils 6.2 中--preserve-root

    自 2004 年 10 月 4 日提交以来, FreeBSD 一直保留斜线(带有“找出我的内衣到底有多防火”提交日志),但最初不是在 下,直到十年后他们记得检查 POSIX 现在强制它在这一点上也是在 POSIX 模式下完成的。POSIXLY_CORRECT

    FreeBSD 最初的提交提到当时 Solaris 已经在这样做了。

    @JdePB(在下面的评论中)发现指向 Sun 内部故事的链接证实并提供了有关 Solaris 起源的更多详细信息,并表明 Solaris 在他们向 Austin 小组提出请求之前已经采取了保护措施。

    它解释了添加该排除项的理由。虽然如果他们这样做了只能责怪自己,但有一种情况是,如果在没有检查/是否提供rm -rf /的情况下,脚本可以做到这一点,这在误用 Solaris 补丁时对一些 Sun 客户造成了不好的影响(根据该链接)。rm -rf -- "$1/$2"$1$2

    在此之前很久就添加了禁止删除.and..以防止潜在的事故。rm仍然是一个危险的命令。它做了它应该做的事情:删除你告诉它的东西。

    rm -rf /*
    cd /tmp &&  rm -rf .*/   # on some systems where rm -rf ../ still removes
                             # the content of ../ and shells that still
                             # may include . and .. in glob expansions.
    rm -rf -- "$diretcory"/* # note the misspelled variable name
    dir='foo '; rm -rf $dir/*
    

    还会删除所有内容。众所周知,Shell 文件名完成会导致此类问题

    rm -rf someth<Tab>/*
    

    扩展为:

    rm -rf something /*
    

    因为something碰巧不是一个目录。

    当尝试使用通配符(默认情况下不是)调用时, Shell 喜欢tcsh或zsh将添加额外的提示。rm*tcsh

    • 32

相关问题

  • 如何在 POSIX shell 脚本中创建算术循环?

  • 处理最后一个命令的退出代码的正确方法是什么?

  • 另一个用户的非空子目录是否可以安全地从我的目录中删除?

  • 当会话负责人退出时,unix 会话会发生什么?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    JSON数组使用jq来bash变量

    • 4 个回答
  • Marko Smith

    日期可以为 GMT 时区格式化当前时间吗?[复制]

    • 2 个回答
  • Marko Smith

    bash + 通过 bash 脚本从文件中读取变量和值

    • 4 个回答
  • Marko Smith

    如何复制目录并在同一命令中重命名它?

    • 4 个回答
  • Marko Smith

    ssh 连接。X11 连接因身份验证错误而被拒绝

    • 3 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Marko Smith

    systemctl 命令在 RHEL 6 中不起作用

    • 3 个回答
  • Marko Smith

    rsync 端口 22 和 873 使用

    • 2 个回答
  • Marko Smith

    以 100% 的利用率捕捉 /dev/loop -- 没有可用空间

    • 1 个回答
  • Marko Smith

    jq 打印子对象中所有的键和值

    • 2 个回答
  • Martin Hope
    EHerman JSON数组使用jq来bash变量 2017-12-31 14:50:58 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Drux 日期可以为 GMT 时区格式化当前时间吗?[复制] 2017-12-26 11:35:07 +0800 CST
  • Martin Hope
    AllisonC 如何复制目录并在同一命令中重命名它? 2017-12-22 05:28:06 +0800 CST
  • Martin Hope
    Steve “root”用户的文件权限如何工作? 2017-12-22 02:46:01 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST
  • Martin Hope
    Cbhihe 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _ 2017-12-03 10:11:38 +0800 CST
  • Martin Hope
    showkey 如何下载软件包而不是使用 apt-get 命令安装它? 2017-12-03 02:15:02 +0800 CST
  • Martin Hope
    youxiao 为什么目录 /home、/usr、/var 等都具有相同的 inode 编号 (2)? 2017-12-02 05:33:41 +0800 CST
  • Martin Hope
    user223600 gpg —list-keys 命令在将私钥导入全新安装后输出 uid [未知] 2017-11-26 18:26:02 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve