Linux误删用户家(home)目录的恢复方法

在生产中可能会由于误操作而删除了某一用户的家目录,这时就需要把用户的家目录安装创建新用户时的模板文件,再恢复并且要跟之前的权限相同。

1. 创建一个新的用户用于测试

[root@localhost mobile4.0-8.0]# useradd test

2. 新用户创建完成时会在/home下面自动创建同名的文件夹

[root@localhost home]# ls
hello.sh  rmk  test

3. 首先查看一下用户家目录中的文件以及权限,用于验证我们恢复后权限没有偏差

[root@localhost home]# ls /home/test/ -al
total 12
drwx------. 3 test test  78 Sep 25 15:23 .
drwxr-xr-x. 4 root root  45 Sep 25 15:23 ..
-rw-r--r--. 1 test test  18 Apr 11 08:53 .bash_logout
-rw-r--r--. 1 test test 193 Apr 11 08:53 .bash_profile
-rw-r--r--. 1 test test 231 Apr 11 08:53 .bashrc
drwxr-xr-x. 4 test test  39 Sep 14 18:18 .mozilla

4. 这里模拟生产中误操作而删除了家目录

[root@localhost home]# rm -rf test/
[root@localhost home]# ls
hello.sh  rmk    #删除test目录

5. 复制skel中的所有文件到home下的test。skel目录中的文件都是创建新用户时家目录中的文件。换句话来说skel中的文件是创建用户家目录的模板。

[root@localhost home]# cp /etc/skel/ /home/test -a

6. 查看复制来的test文件夹,注意一下所有者与所属组都是root,所以需要改成test

[root@localhost home]# ls -al
total 8
drwxr-xr-x.  4 root root   45 Sep 25 16:39 .
dr-xr-xr-x. 17 root root  224 Sep 14 18:38 ..
-rwxr-xr-x.  1 root root  120 Sep 19 14:00 hello.sh
drwx------. 20 rmk  rmk  4096 Sep 19 15:49 rmk
drwxr-xr-x.  3 root root   78 Apr 11 12:59 test

drwxr-xr-x.  3 root root   78 Apr 11 12:59 test

7. 首先把wnagcai目录的权限修改为700

[root@localhost home]# chmod 700 test/

8. 最后把wangcai目录下的所有文件所属组、所有者都改成用户自己。

-R选项是递归,将目录下的所有文件都更改设置。

[root@localhost home]# chown test:test test/ -R
[root@localhost home]# ls -al test/
total 12
drwx------. 3 test test  78 Apr 11 12:59 .
drwxr-xr-x. 4 root root  45 Sep 25 16:39 ..
-rw-r--r--. 1 test test  18 Apr 11 08:53 .bash_logout
-rw-r--r--. 1 test test 193 Apr 11 08:53 .bash_profile
-rw-r--r--. 1 test test 231 Apr 11 08:53 .bashrc
drwxr-xr-x. 4 test test  39 Sep 14 18:18 .mozilla

至此用户的家目录就恢复完成了,并且经过对比跟之前的目录权限完全相同。

 

猜你喜欢

转载自blog.csdn.net/qq_37811638/article/details/82841551