How to prevent accidently deleting important files as root user

时间:2015-09-01 22:21:35

标签: linux shell

I need to use root for the installation and maintenance for the database and application server that I am playing around with. The database and application server create files and directories in the /usr directory that I need to manipulate.I have accidentally deleted important files in /usr in the past , making the system act funny.I was wondering how to prevent this from happening in the future. I have a habit of rm -rf developed from years of non root user use.

I thought of moving /usr to /usr_bkp and creating a soft_link /usr to point to /usr_bkp. I am afraid that moving or removing /usr even temporarily will have unpredictable consequences. What is the best known method to avoid such errors.

4 个答案:

答案 0 :(得分:2)

The purpose of root is to do everything he wants. However you could use the -i option to have to confirm to delete the files.

答案 1 :(得分:1)

我这些天大多以root用户身份工作,并且意外删除了重要文件”:这两个人不能一起工作。错误地删除文件是具有直接和可见后果的操作之一(还有其他可能会在以后出现并且需要大量时间来调试)。

创建一个单独的用户(这是我建议的,直到获得更多的XP)并赋予它sudo权限(好的是,只有在 sudo 模式下才需要特别小心,不是默认的。)

至于第二部分:

  • 我很确定符号链接 / usr 不是 Linux 管理手册推荐的

    < / LI>
  • rm -rf /usr/SOME_FILES_OR_FOLDERS / usr 符号链接 usr_bkp (或其名称可能是什么)时,将会仍然删除位于 / usr 文件夹中的文件/文件夹,因此没有保护。

  • 我看到的唯一变化是每次访问来自 / usr 的内容,而不是直接访问时,必须解析符号链接 (每一次 ?); (更多操作,需要更多时间 - >性能下降)

  • (如果我没有遗漏一些明显的东西,)做符号链接的事情会引入一些“陷阱”,因为mvln,... ELFs (默认情况下)驻留在 / usr / bin 中,第一个命令(mv)会成功,但是你将不再拥有ln(除非指定其完整的路径),或换句话说“ROUText:ti-ai taiat craca:d”。关于运行应用程序,我认为你会好的(最多需要重启)。

所以,考虑到4项我建议不要使用 root (至少目前为止)。

答案 2 :(得分:0)

What x4rkz said but also if you're concerned of 'rm'ing files as root, create an alias in your shell's rc file to always confirm the removal of files. In Bourne derivatives this can be accomplished with:

alias rm='/usr/bin/rm -i '

Test:

# alias rm='/usr/bin/rm -i'
# rm test
# /usr/bin/rm: remove regular empty file ‘test’? y

Of course, this won't help you if you write a script that doesn't first load that alias.

Another failure mode I've seen with new admins is misusing output redirection and overwriting a file. In Bourne derivatives, you can set the 'NOCLOBBER' option in shell scripts to prevent this:

# set NOCLOBBER

This is not 100% portable so ensure this is supported before relying on it.

答案 3 :(得分:0)

好的,所以你正在使用数据库和应用程序。

写一个脚本。除非您使用该脚本,否则请勿触摸文件。

只需将您需要执行的每项操作都放入脚本中。

./my-script clean
./my-script install
./my-script run
etc...

这样文件将被干净,安装删除,每次都以相同的方式重启应用程序,没有删除错误的危险。

此外,作为开发人员,您需要一致性。不再想知道更新失败的原因,因为您忘记将正确的文件复制到位或忘记删除旧的二进制文件。

相关问题