关于/dev/中的文件误删恢复

2019-07-17 2006点热度 0人点赞

不小心把/dev/目录删掉了,又不想通过重启恢复,mknod 可以帮你重新创建那些字符文件。


首先我们从两个随机数缓冲文件说起,当你误删了 /dev/random 和 /dev/urandom 后,你发现你的很多需要随机数的业务全都不可用了。

而在正常的业务系统中,你使用 ls -al /dev/random /dev/urandom 的时候可以查看到一串特殊的信息

crw-rw-rw- 1 root root 1, 8 Jun 15 09:51 random
crw-rw-rw- 1 root root 1, 9 Jun 15 09:51 urandom

而我们使用 mknod 的时候,就需要这些信息来创建这个字符缓冲特殊文件。当然,大多数系统的这个文件都一样

random 文件的类型为 c (字符缓冲特殊文件) 权限为 666 所有者为 root 主要编号 MAJOT 为 1 然后 次要编号 MINOR 为 8
urandom 文件的类型为 c (字符缓冲特殊文件) 权限为 666 所有者为 root 主要编号 MAJOR 为 1 然后 次要编号 MINOR 为 9

所以我们根据这些信息,可以直接创建这个文件了

mknod -m 666 /dev/random c 1 8
chown root:root /dev/random
mknod -m 666 /dev/urandom c 1 9
chown root:root /dev/urandom

这样我们创建好后,这个文件就可以正常用了。唯一麻烦的是系统原先写入 /dev/random 的随机数都没有了。


而当你隐约明白了一点后,我们可以继续用 mknod 重建更多的文件

crw-r--r-- 1 root root    10, 235 Jun 15 09:51 autofs
crw------- 1 root root     5,   1 Jun 15 09:51 console
crw-rw---- 1 root disk    10, 237 Jun 15 09:51 loop-control
crw-rw-rw- 1 root root     1,   3 Jun 15 09:51 null
crw-rw-rw- 1 root tty      5,   0 Jul 17 14:41 tty
crw--w---- 1 root tty      4,   0 Jun 15 09:51 tty0
crw--w---- 1 root tty      4,   1 Jun 15 09:53 tty1
crw-rw---- 1 root dialout  4,  64 Jun 15 09:51 ttyS0
crw-rw---- 1 root dialout  4,  65 Jun 15 09:51 ttyS1
crw-rw---- 1 root tty      7,   0 Jun 15 09:51 vcs
crw-rw---- 1 root tty      7,   1 Jun 15 09:51 vcs1
crw-rw---- 1 root tty      7, 128 Jun 15 09:51 vcsa
crw-rw---- 1 root tty      7, 129 Jun 15 09:51 vcsa1
crw-rw-rw- 1 root root     1,   5 Jun 15 09:51 zero

上面的就是我们常用的一些东西,比如我们想修复 loop 的挂载,就要重新创建 loop-control 这个文件。

mknod -m 660 /dev/loop-control c 10 237
chown :disk /dev/loop-control

又或者你把 tty0 删除了,也可以恢复。

mknod -m 600 /dev/tty0 c 10 237
chown :tty /dev/tty0
chmod g+w /dev/tty0

但是 /dev/ 里面其实还有磁盘。如果我们需要重新恢复磁盘的 块缓冲特殊文件 怎么办?

其实使用的方式和之前差不多,不过我们要定位出磁盘都用了哪些文件。我们要使用 lsblk 的命令 去查看。

[root@hostname ~]# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    252:0    0  25G  0 disk
└─vda1 252:1    0  25G  0 part /

在这里我们能看到,我们磁盘有两个文件,一个是 vda 一个是 vda1,两个的主要编号都为 252 ,次要编号一个是 0 一个是 1

然后我们使用命令重新恢复 ,注意磁盘文件属于 块缓存特殊文件 ,所以文件的类型是 b (block)

mknod -m 660 /dev/vda b 252 0
chown :disk /dev/vda
mknod -m 660 /dev/vda1 b 252 1
chown :disk /dev/vda1

创建好后我们可以查看两个文件的信息如下。

[root@hostname ~]# ls -al /dev/vda /dev/vda1
brw-rw---- 1 root disk 252, 0 Jun 15 09:51 vda
brw-rw---- 1 root disk 252, 1 Jun 15 09:51 vda1

同时使用 blkid 的时候可以查看到对应磁盘分区的 UUID 则为成功。


最后在这里粘贴一份 CentOS 7 的 /dev/ 里面文件信息

drwxr-xr-x  20 root root        3140 Jun 15 09:51 .
dr-xr-xr-x. 19 root root        4096 May  3 14:18 ..
crw-r--r--   1 root root     10, 235 Jun 15 09:51 autofs
drwxr-xr-x   2 root root          80 Jun 15 09:51 block
crw-------   1 root root     10, 234 Jun 15 09:51 btrfs-control
drwxr-xr-x   3 root root          60 Jun 15 09:52 bus
drwxr-xr-x   2 root root        2760 Jun 15 09:51 char
crw-------   1 root root      5,   1 Jun 15 09:51 console
lrwxrwxrwx   1 root root          11 Jun 15 09:51 core -> /proc/kcore
drwxr-xr-x   3 root root          60 Jun 15 09:52 cpu
crw-------   1 root root     10,  61 Jun 15 09:51 cpu_dma_latency
crw-------   1 root root     10, 203 Jun 15 09:51 cuse
drwxr-xr-x   4 root root          80 Jun 15 09:51 disk
drwxr-xr-x   2 root root          60 Jun 15 09:51 dri
crw-rw----   1 root video    29,   0 Jun 15 09:51 fb0
lrwxrwxrwx   1 root root          13 Jun 15 09:51 fd -> /proc/self/fd
crw-rw-rw-   1 root root      1,   7 Jun 15 09:51 full
crw-rw-rw-   1 root root     10, 229 Jun 15 09:51 fuse
crw-------   1 root root    244,   0 Jun 15 09:51 hidraw0
crw-------   1 root root     10, 228 Jun 15 09:51 hpet
drwxr-xr-x   2 root root           0 Jun 15 09:51 hugepages
crw-------   1 root root     10, 183 Jun 15 09:51 hwrng
lrwxrwxrwx   1 root root          25 Jun 15 09:51 initctl -> /run/systemd/initctl/fifo
drwxr-xr-x   4 root root         300 Jun 15 09:51 input
crw-r--r--   1 root root      1,  11 Jun 15 09:51 kmsg
drwxr-xr-x   2 root root          60 Jun 15 09:52 lightnvm
srw-rw-rw-   1 root root           0 Jun 15 09:51 log
crw-rw----   1 root disk     10, 237 Jun 15 09:51 loop-control
drwxr-xr-x   2 root root          60 Jun 15 09:51 mapper
crw-------   1 root root     10, 227 Jun 15 09:51 mcelog
crw-r-----   1 root kmem      1,   1 Jun 15 09:51 mem
crw-------   1 root root     10,  58 Jun 15 09:51 memory_bandwidth
drwxrwxrwt   2 root root          40 Jun 15 09:52 mqueue
drwxr-xr-x   2 root root          60 Jun 15 09:51 net
crw-------   1 root root     10,  60 Jun 15 09:51 network_latency
crw-------   1 root root     10,  59 Jun 15 09:51 network_throughput
crw-rw-rw-   1 root root      1,   3 Jun 15 09:51 null
crw-------   1 root root     10, 144 Jun 15 09:51 nvram
crw-r-----   1 root kmem      1,   4 Jun 15 09:51 port
crw-------   1 root root    108,   0 Jun 15 09:51 ppp
crw-rw-rw-   1 root tty       5,   2 Jul 17  2019 ptmx
drwxr-xr-x   2 root root           0 Jun 15 09:51 pts
crw-rw-rw-   1 root root      1,   8 Jun 15 09:51 random
drwxr-xr-x   2 root root          60 Jun 15 09:52 raw
lrwxrwxrwx   1 root root           4 Jun 15 09:51 rtc -> rtc0
crw-------   1 root root    249,   0 Jun 15 09:51 rtc0
drwxrwxrwt   2 root root          40 Jun 15 09:51 shm
crw-------   1 root root     10, 231 Jun 15 09:51 snapshot
drwxr-xr-x   2 root root          80 Jun 15 09:51 snd
lrwxrwxrwx   1 root root          15 Jun 15 09:51 stderr -> /proc/self/fd/2
lrwxrwxrwx   1 root root          15 Jun 15 09:51 stdin -> /proc/self/fd/0
lrwxrwxrwx   1 root root          15 Jun 15 09:51 stdout -> /proc/self/fd/1
crw-rw-rw-   1 root tty       5,   0 Jul 17 14:41 tty
crw--w----   1 root tty       4,   0 Jun 15 09:51 tty0
crw--w----   1 root tty       4,   1 Jun 15 09:53 tty1
crw--w----   1 root tty       4,  10 Jun 15 09:51 tty10
crw--w----   1 root tty       4,  11 Jun 15 09:51 tty11
crw--w----   1 root tty       4,  12 Jun 15 09:51 tty12
crw--w----   1 root tty       4,  13 Jun 15 09:51 tty13
crw--w----   1 root tty       4,  14 Jun 15 09:51 tty14
crw--w----   1 root tty       4,  15 Jun 15 09:51 tty15
crw--w----   1 root tty       4,  16 Jun 15 09:51 tty16
crw--w----   1 root tty       4,  17 Jun 15 09:51 tty17
crw--w----   1 root tty       4,  18 Jun 15 09:51 tty18
crw--w----   1 root tty       4,  19 Jun 15 09:51 tty19
crw--w----   1 root tty       4,   2 Jun 15 09:51 tty2
crw--w----   1 root tty       4,  20 Jun 15 09:51 tty20
crw--w----   1 root tty       4,  21 Jun 15 09:51 tty21
crw--w----   1 root tty       4,  22 Jun 15 09:51 tty22
crw--w----   1 root tty       4,  23 Jun 15 09:51 tty23
crw--w----   1 root tty       4,  24 Jun 15 09:51 tty24
crw--w----   1 root tty       4,  25 Jun 15 09:51 tty25
crw--w----   1 root tty       4,  26 Jun 15 09:51 tty26
crw--w----   1 root tty       4,  27 Jun 15 09:51 tty27
crw--w----   1 root tty       4,  28 Jun 15 09:51 tty28
crw--w----   1 root tty       4,  29 Jun 15 09:51 tty29
crw--w----   1 root tty       4,   3 Jun 15 09:51 tty3
crw--w----   1 root tty       4,  30 Jun 15 09:51 tty30
crw--w----   1 root tty       4,  31 Jun 15 09:51 tty31
crw--w----   1 root tty       4,  32 Jun 15 09:51 tty32
crw--w----   1 root tty       4,  33 Jun 15 09:51 tty33
crw--w----   1 root tty       4,  34 Jun 15 09:51 tty34
crw--w----   1 root tty       4,  35 Jun 15 09:51 tty35
crw--w----   1 root tty       4,  36 Jun 15 09:51 tty36
crw--w----   1 root tty       4,  37 Jun 15 09:51 tty37
crw--w----   1 root tty       4,  38 Jun 15 09:51 tty38
crw--w----   1 root tty       4,  39 Jun 15 09:51 tty39
crw--w----   1 root tty       4,   4 Jun 15 09:51 tty4
crw--w----   1 root tty       4,  40 Jun 15 09:51 tty40
crw--w----   1 root tty       4,  41 Jun 15 09:51 tty41
crw--w----   1 root tty       4,  42 Jun 15 09:51 tty42
crw--w----   1 root tty       4,  43 Jun 15 09:51 tty43
crw--w----   1 root tty       4,  44 Jun 15 09:51 tty44
crw--w----   1 root tty       4,  45 Jun 15 09:51 tty45
crw--w----   1 root tty       4,  46 Jun 15 09:51 tty46
crw--w----   1 root tty       4,  47 Jun 15 09:51 tty47
crw--w----   1 root tty       4,  48 Jun 15 09:51 tty48
crw--w----   1 root tty       4,  49 Jun 15 09:51 tty49
crw--w----   1 root tty       4,   5 Jun 15 09:51 tty5
crw--w----   1 root tty       4,  50 Jun 15 09:51 tty50
crw--w----   1 root tty       4,  51 Jun 15 09:51 tty51
crw--w----   1 root tty       4,  52 Jun 15 09:51 tty52
crw--w----   1 root tty       4,  53 Jun 15 09:51 tty53
crw--w----   1 root tty       4,  54 Jun 15 09:51 tty54
crw--w----   1 root tty       4,  55 Jun 15 09:51 tty55
crw--w----   1 root tty       4,  56 Jun 15 09:51 tty56
crw--w----   1 root tty       4,  57 Jun 15 09:51 tty57
crw--w----   1 root tty       4,  58 Jun 15 09:51 tty58
crw--w----   1 root tty       4,  59 Jun 15 09:51 tty59
crw--w----   1 root tty       4,   6 Jun 15 09:51 tty6
crw--w----   1 root tty       4,  60 Jun 15 09:51 tty60
crw--w----   1 root tty       4,  61 Jun 15 09:51 tty61
crw--w----   1 root tty       4,  62 Jun 15 09:51 tty62
crw--w----   1 root tty       4,  63 Jun 15 09:51 tty63
crw--w----   1 root tty       4,   7 Jun 15 09:51 tty7
crw--w----   1 root tty       4,   8 Jun 15 09:51 tty8
crw--w----   1 root tty       4,   9 Jun 15 09:51 tty9
crw-rw----   1 root dialout   4,  64 Jun 15 09:51 ttyS0
crw-rw----   1 root dialout   4,  65 Jun 15 09:51 ttyS1
crw-rw----   1 root dialout   4,  66 Jun 15 09:51 ttyS2
crw-rw----   1 root dialout   4,  67 Jun 15 09:51 ttyS3
crw-------   1 root root     10, 239 Jun 15 09:51 uhid
crw-------   1 root root     10, 223 Jun 15 09:51 uinput
crw-rw-rw-   1 root root      1,   9 Jun 15 09:51 urandom
crw-------   1 root root    245,   0 Jun 15 09:51 usbmon0
crw-------   1 root root    245,   1 Jun 15 09:51 usbmon1
crw-rw----   1 root tty       7,   0 Jun 15 09:51 vcs
crw-rw----   1 root tty       7,   1 Jun 15 09:51 vcs1
crw-rw----   1 root tty       7,   2 Jun 15 09:51 vcs2
crw-rw----   1 root tty       7,   3 Jun 15 09:51 vcs3
crw-rw----   1 root tty       7,   4 Jun 15 09:51 vcs4
crw-rw----   1 root tty       7,   5 Jun 15 09:51 vcs5
crw-rw----   1 root tty       7,   6 Jun 15 09:51 vcs6
crw-rw----   1 root tty       7, 128 Jun 15 09:51 vcsa
crw-rw----   1 root tty       7, 129 Jun 15 09:51 vcsa1
crw-rw----   1 root tty       7, 130 Jun 15 09:51 vcsa2
crw-rw----   1 root tty       7, 131 Jun 15 09:51 vcsa3
crw-rw----   1 root tty       7, 132 Jun 15 09:51 vcsa4
crw-rw----   1 root tty       7, 133 Jun 15 09:51 vcsa5
crw-rw----   1 root tty       7, 134 Jun 15 09:51 vcsa6
crw-rw----   1 root tty       7,  64 Jun 15 09:51 vcsu
crw-rw----   1 root tty       7,  65 Jun 15 09:51 vcsu1
crw-rw----   1 root tty       7,  66 Jun 15 09:51 vcsu2
crw-rw----   1 root tty       7,  67 Jun 15 09:51 vcsu3
crw-rw----   1 root tty       7,  68 Jun 15 09:51 vcsu4
crw-rw----   1 root tty       7,  69 Jun 15 09:51 vcsu5
crw-rw----   1 root tty       7,  70 Jun 15 09:51 vcsu6
brw-rw----   1 root disk    252,   0 Jun 15 09:51 vda
brw-rw----   1 root disk    252,   1 Jun 15 09:51 vda1
drwxr-xr-x   2 root root          60 Jun 15 09:51 vfio
crw-------   1 root root     10,  63 Jun 15 09:51 vga_arbiter
crw-------   1 root root     10, 137 Jun 15 09:51 vhci
crw-------   1 root root     10, 238 Jun 15 09:51 vhost-net
crw-------   1 root root     10, 241 Jun 15 09:51 vhost-vsock
drwxr-xr-x   2 root root          60 Jun 15 09:51 virtio-ports
crw-------   1 root root    242,   1 Jun 15 09:51 vport1p1
crw-rw-rw-   1 root root      1,   5 Jun 15 09:51 zero

StarryVoid

Have a good time