如果我不小心在系统目录(/、/etc、...)上运行命令“chmod -R”怎么办

fl0*_*00r 65 permissions sudo chmod

我不小心跑了

sudo chmod 755 -R /
Run Code Online (Sandbox Code Playgroud)

代替

sudo chmod 755 -R ./
Run Code Online (Sandbox Code Playgroud)

几秒钟后我停止了它,但是现在出现了一些问题,例如

sudo: must be setuid root
Run Code Online (Sandbox Code Playgroud)

如何恢复权限?

uli*_*tko 67

简而言之:你不能,重新安装你的系统。

我的意思是,Posix 权限被大量使用和依赖;在文件系统中有很多地方,错误的权限会破坏操作系统(SUID 标志),甚至更糟的是,/etc/ssh/ssh_host_rsa_key在它看起来工作正常时,使其暴露在安全方面 ( )。

因此,这种恢复很难正确进行。错过一件事- 你把它搞砸了。你已经搞砸了你的sudo chmod命令(如果那是你的朋友而不是你,她也可以学习一些 Linux 课程)——这是一个非常简单的命令。正确的恢复需要更多的命令和更多的警惕。即使您使用某人的脚本。

所以相信我,只需重新安装。这是一个安全的赌注,并保证让您远离麻烦。


最后,这里有一些相关的提示。

第一:如果您下次在单独的分区上设置,/home重新安装将不会那么痛苦。实际上,它们将是轻而易举的。

第二:考虑做疯狂的科学的Linux虚拟机像VirtualBox的,做你的快照。

第三:chmod -R .作品。点本身.是有效的目录名称。没有真正需要附加那个斜杠。你本来可以避免完全跳过点的灾难性风险;
仅仅是chmod: missing operand after ‘755’VS 一个毁坏的系统。

  • 好吧,您*可以*通过从另一个系统获取每个文件的所有权限,但是这样做的工作量很大,因此仅重新安装可能会更容易和更安全。 (15认同)
  • 不要难过!*拥有权利的同时也被赋予了重大的责任* (3认同)
  • 啊:)太伤心了。 (2认同)
  • 是的,我刚刚用这个毁了我的笔记本电脑......令人惊讶的是,你可以轻松地摧毁一台基于 linux 的机器。 (2认同)

Ale*_*huk 28

我编写并使用了几年的 Ruby 脚本来rsync获得权限和所有权。脚本get-filesystem-acl通过递归遍历所有文件来收集所有信息并将其全部放入文件中.acl。脚本.acl-restore将读取.acl并应用所有chown的和chmod

您可以运行get-filesystem-acl在一个类似Ubuntu的安装,然后在复制.acl文件到您的CHMOD损伤箱,放.acl.acl-restore中/,并运行.acl-restore

您将需要拥有 root 权限,因此请sudo按照 Marco Ceppi 的建议进行修复。

我可以.acl为我的 Ubuntu生成并给你文件。

get-filesystem-acl

#!/usr/bin/ruby

RM   = "/bin/rm"
SORT = "/usr/bin/sort"
TMP  = "/tmp/get_acl_#{Time.now.to_i}_#{rand * 899 + 100}"

require 'find'

IGNORE = [".git"]

def numeric2human(m)
  return sprintf("%c%c%c%c%c%c%c%c%c",
            (m & 0400 == 0 ? ?- : ?r),
            (m & 0200 == 0 ? ?- : ?w),
            (m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
                             (m & 04000 == 0 ? ?x : ?s)),
            (m & 0040 == 0 ? ?- : ?r),
            (m & 0020 == 0 ? ?- : ?w),
            (m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
                             (m & 02000 == 0 ? ?x : ?s)),
            (m & 0004 == 0 ? ?- : ?r),
            (m & 0002 == 0 ? ?- : ?w),
            (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
                             (m & 01000 == 0 ? ?x : ?t)))
end


File.open(TMP, "w") do |acl_file|

  # TODO: Instead of the current dir, find the .git dir, which could be
  #       the same or outside of the current dir
  Find.find(".") do |path|

    next if IGNORE.collect {|ig| !!(path[2..-1] =~ /\A#{ig}/)}.include? true
    next if File.symlink?(path)

    stat = File.lstat(path)
    group_id = stat.gid
    rules    = "#{type}#{numeric2human(stat.mode)}" 

    acl_file.puts "#{path} #{rules} #{owner_id} #{group_id}"
  end
end

`#{SORT} #{TMP} > .acl`
`#{RM}   #{TMP}`
Run Code Online (Sandbox Code Playgroud)

.acl-restore

#!/usr/bin/ruby

# This script will only work with .acl_ids

# Restore from...
FROM  = ".acl"

MKDIR = "/bin/mkdir"
CHMOD = "/bin/chmod"
CHOWN = "/bin/chown"
known_content_missing = false


def numeric2human(m)
  return sprintf("%c%c%c%c%c%c%c%c%c",
            (m & 0400 == 0 ? ?- : ?r),
            (m & 0200 == 0 ? ?- : ?w),
            (m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
                             (m & 04000 == 0 ? ?x : ?s)),
            (m & 0040 == 0 ? ?- : ?r),
            (m & 0020 == 0 ? ?- : ?w),
            (m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
                             (m & 02000 == 0 ? ?x : ?s)),
            (m & 0004 == 0 ? ?- : ?r),
            (m & 0002 == 0 ? ?- : ?w),
            (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
                             (m & 01000 == 0 ? ?x : ?t)))
end

def human2chmod(mode)
  raise unless mode =~ /([r-][w-][xtsTS-])([r-][w-][xtsTS-])([r-][w-][xtsTS-])/
  triple = [$1, $2, $3]
  u,g,o = triple.collect do |i|
    i.sub('s', 'sx').sub('t', 'tx').downcase.gsub('-', '')
  end

  return "u=#{u},g=#{g},o=#{o}" 
end



File.open(FROM).each do |acl|
  raise unless acl =~ /\A(([^ ]*? )+)([^ ]+) ([^ ]+) ([^ ]+)\Z/
  path, rules, owner_id, group_id = $1, $3, $4, $5
  path = path.strip
  owner_id = owner_id.to_i
  group_id = group_id.to_i

  if !File.exists?(path) and !File.symlink?(path)
    if rules =~ /\Ad/
      STDERR.puts "Restoring a missing directory: #{path}"
      STDERR.puts "Probably it was an empty directory. Git goes not track them."
      `#{MKDIR} -p '#{path}'` # Creating the any parents
    else
      known_content_missing = true
      STDERR.puts "ERROR: ACL is listed but the file is missing: #{path}"
      next
    end
  end

  s = File.lstat(path)
  t = s.ftype[0..0].sub('f', '-') # Single character for the file type
                                  # But a "-" istead of "f"

  # Actual, but not neccesarely Desired 
  actual_rules    = "#{t}#{numeric2human(s.mode)}"
  actual_owner_id = s.uid 
  actual_group_id = s.gid 

  unless [actual_rules, actual_owner_id, actual_group_id] ==
    [rules, owner_id, group_id]

    chmod_argument = human2chmod(rules)

    # Debug
    #p chmod_argument
    #p s.mode

    ## Verbose
    puts path
    puts "Wrong: #{[actual_rules, actual_owner_id, actual_group_id].inspect}"
    puts "Fixed: #{[rules, owner_id, group_id].inspect}"
    `#{CHMOD} #{chmod_argument} '#{path}'`

    #puts
  end

end

if known_content_missing
  STDERR.puts "-" * 80 
  STDERR.puts "Some files that are listed in #{FROM.inspect} are missing in " +
              "the current directory."
  STDERR.puts
  STDERR.puts "Is #{FROM.inspect} outdated?"
  STDERR.puts "(Try retrograding the current directory to an earlier version)"
  STDERR.puts
  STDERR.puts "Or is the current directory incomplete?"
  STDERR.puts "(Try to recover the current directory)"
  STDERR.puts "-" * 80 
end
Run Code Online (Sandbox Code Playgroud)

  • 有点矫枉过正…… find 做得很好:`find SOME_DIR -depth -printf 'chmod %m %p\n' > saved_permission` (11认同)

Mar*_*ppi 14

长:可以。您需要从 Live CD 挂载文件系统并开始在适当的位置恢复权限。至少要让 sudo 恢复,您需要sudo chmod u+s /usr/bin/sudo在 LiveCD 会话中运行- 这将解决必须是 setuid root 的问题。

但是,简单地重新安装系统可能会更容易。


Art*_*ild 5

如何预防未来发生事故

这里的所有答案(可以预见)都集中在将系统恢复到工作状态。但没有答案关注如何更好地防止未来发生类似事故。

为了完全防止将来发生此类事故,请将以下内容添加到您的~/.bashrc(或~/.bash_aliases)文件中:

# set Safetynets
alias sudo='sudo '    # This allows sudo to run aliases
alias chmod='chmod --preserve-root'
alias chown='chown --preserve-root'
alias chgrp='chgrp --preserve-root'
Run Code Online (Sandbox Code Playgroud)

另外,请确保用户的~/.bashrc(或~/.bash_aliases)文件中root也包含相同的内容(否则没关系!)。

这样,您就不能root在根文件系统上递归地使用这些命令(即使是 )。

将其添加到~/.bashrc

将以下行添加到您自己的行中~/.bashrc

echo "alias sudo='sudo '    # This allows sudo to run aliases" >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

向根添加行的一种简单方法~/.bashrc是使用“heredoc”。首先切换到root用户:

sudo -s
Run Code Online (Sandbox Code Playgroud)

然后运行以下文本块:

cat << EOT >> ~/.bashrc

# set Safetynets
alias sudo='sudo '    # This allows sudo to run aliases
alias chmod='chmod --preserve-root'
alias chown='chown --preserve-root'
alias chgrp='chgrp --preserve-root'
EOT
Run Code Online (Sandbox Code Playgroud)