简体   繁体   English

可以在Linux中创建禁止命令rm的脚本吗?

[英]Is possible to create a script that disallows the command rm in linux?

可以在Linux中创建禁止命令rm的脚本吗?

You could create your own shell that mimics the command-line and sends all normal input to the actual command-line except for rm commands. 您可以创建自己的外壳程序来模仿命令行,并将除rm命令之外的所有普通输入发送到实际命令行。

I wouldn't be surprised if there's a more elegant solution though. 如果有更优雅的解决方案,我也不会感到惊讶。

you could add a alias in your .bashrc 您可以在.bashrc添加别名

alias rm='rm -i'

this will force you to confirm each time you rm a file unless you run rm -f 除非您运行rm -f否则这将迫使您每次确认文件时都要进行确认。

and if you alias like this: 如果您这样别名:

alias rm='echo "command rm is forbidden"'

this will forbid the rm command 这将禁止rm命令

Note that alias will only prevent you from a miss-typing. 请注意,别名只会防止您键入错误。 not a system security fasten, if you really want user to have root privilege to run rm command: 如果您确实希望用户具有root特权来运行rm命令,则不会提高系统安全性:

$which rm
-rwxr-xr-x 1 root root 64424 Jan 20 22:46 /bin/rm
$sudo chomd 744 /bin/rm

that will force the user to be root to run rm command. 这将迫使用户以root身份运行rm命令。

Technically you can't disable root to delete files on your system, because root can do anything, but if user gain the privilege through sudo (which is not the real user root but gain privilege temporarily) there are ways to disable specific command from running even by sudo through the sudo config file check this post 从技术上讲,您不能禁用root来删除系统上的文件,因为root可以执行任何操作,但是如果用户通过sudo (不是真正的用户root,但会暂时获得特权)获得特权,则可以使用某些方法来禁止运行特定命令甚至通过sudo通过sudo配置文件sudo 检查这篇文章

for a specific user you can do like this 对于特定用户,您可以这样做

Lshell is configured via an INI file. Lshell是通过INI文件配置的。 By default, it holds a whitelist of allowed commands, but it can be easily configured to prohibit user from using a specific command. 默认情况下,它保留允许命令的白名单,但可以轻松配置为禁止用户使用特定命令。

This configuration (default conf /etc/lshell.conf) prohibits user foo from using mkdir: 此配置(默认conf /etc/lshell.conf)禁止用户foo使用mkdir:

 [foo]
 allowed = 'all' - ['mkdir', 'bash', 'sh', 'csh', 'dash', 'env']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM