当你安装Linux时,安装者已经默认选择了"受指导的"分区。当你遇到这个选项时,安装者在root(/)分区放置了home目录和所有其他系统目录。
虽然这种设置看起来非常好,但它展现了非常大的风险。如果你的系统崩溃或者有个东西损坏了这个root分区,驻留在home目录中所有你个人的文件丢失了。
处于此种原因,在安装过程中创建一个单独的home分区是重要的。这在操作系统重装或者遇到root分区损坏时确保了你个人文件的安全。
如果你接受了默认选项(所有目录划到root分区下那样地划分你的硬盘),则这是对你的指南。
在本指南中,我们把home目录移动到一个新添加磁盘上的单独分区中。为了演示目的,我们在计算机上装上了第二块磁盘、在现实世界中,这就是你想要称为home分区的SDD磁盘或者另一块HDD。
在接入第二个磁盘前,我们只有一个硬盘驱动(/dev/sda)。
使用df命令看一下你硬盘驱动配置:
- [blctrl@localhost applications]$ df -hT
- Filesystem Type Size Used Avail Use% Mounted on
- devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
- tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
- tmpfs tmpfs 3.7G 9.9M 3.7G 1% /run
- tmpfs tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
- /dev/mapper/rl-root xfs 90G 6.0G 84G 7% /
- /dev/mapper/rl-home xfs 14G 188M 14G 2% /home
- /dev/sda1 xfs 1014M 264M 751M 26% /boot
- tmpfs tmpfs 754M 64K 754M 1% /run/user/1000
-
- [blctrl@localhost applications]$ lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 111.8G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 110.8G 0 part
- ├─rl-root 253:0 0 89.3G 0 lvm /
- ├─rl-swap 253:1 0 7.7G 0 lvm [SWAP]
- └─rl-home 253:2 0 13.8G 0 lvm /home
接下来,我们接入一个250GB的SDD磁盘。这被识别为/dev/sdb。要证实这个,我们将运行lsblk命令。
- [blctrl@localhost applications]$ lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 111.8G 0 disk
- ├─sda1 8:1 0 1G 0 part /boot
- └─sda2 8:2 0 110.8G 0 part
- ├─rl-root 253:0 0 89.3G 0 lvm /
- ├─rl-swap 253:1 0 7.7G 0 lvm [SWAP]
- └─rl-home 253:2 0 13.8G 0 lvm /home
- sdb 8:16 0 238.5G 0 disk
分区会根据你的设备而不同。例如,第三个磁盘将被识别为/dev/sdc,第四个磁盘被识别为/dev/sdd以此类推。
我们已经向我们的系统添加了第二块磁盘,但为了使其用作home目录的单独分区,我们需要在它上创建一个分区。到此,由于他是一个新磁盘,它还没有一个分区。
fdisk命令验证:
- [blctrl@localhost applications]$ sudo fdisk -l
- Disk /dev/sda: 111.8 GiB, 120034123776 bytes, 234441648 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
- Disklabel type: dos
- Disk identifier: 0x000a8d44
-
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 2099199 2097152 1G 83 Linux
- /dev/sda2 2099200 234440703 232341504 110.8G 8e Linux LVM
-
-
- Disk /dev/sdb: 238.5 GiB, 256060514304 bytes, 500118192 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 4096 bytes
- I/O size (minimum/optimal): 4096 bytes / 4096 bytes
-
-
-
-
- Disk /dev/mapper/rl-root: 89.3 GiB, 95860817920 bytes, 187228160 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
-
-
- Disk /dev/mapper/rl-swap: 7.7 GiB, 8241807360 bytes, 16097280 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
-
-
- Disk /dev/mapper/rl-home: 13.8 GiB, 14847836160 bytes, 28999680 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
到此,你看到新磁盘不像第一块有/dev/sda1和/dev/sda2的磁盘,它没有与自身关联的任何分区。
现在,我们将使用以下命令创建一个分区:
$ sudo fdisk /dev/sdb
在提示时,按'n'创建一个新分区。接着按'p'指定一个主分区的创建,并且按'1'指定分区号。对于接下来的两次提示,输入'回车'接受指定第一和末尾 扇区的默认值。要保存所有所作的修改,按'w‘,随后这写了所作的所有修改到了这个分区。
- [blctrl@localhost applications]$ sudo fdisk /dev/sdb
- [sudo] password for blctrl:
-
- Welcome to fdisk (util-linux 2.32.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
-
- The old xfs signature will be removed by a write command.
-
- Device does not contain a recognized partition table.
- Created a new DOS disklabel with disk identifier 0xcbe5ad7a.
-
- Command (m for help): n
- Partition type
- p primary (0 primary, 0 extended, 4 free)
- e extended (container for logical partitions)
- Select (default p): p
- Partition number (1-4, default 1): 1
- First sector (2048-500118191, default 2048):
- Last sector, +sectors or +size{K,M,G,T,P} (2048-500118191, default 500118191):
-
- Created a new partition 1 of type 'Linux' and of size 238.5 GiB.
-
- Command (m for help): w
- The partition table has been altered.
- Calling ioctl() to re-read partition table.
- Syncing disks.
要验证做的更改,再次运行这条命令:
sudo fdisk /dev/sdb
在提示时,按'p'对应输出。这打印出这个分区信息。我们可以从输出看到已经用Linux作为文件系统类型创建了一个新分区/dev/sdb1。我们需要在下一步中格式化成xfs文件系统类型。
-
- [blctrl@localhost applications]$ sudo fdisk /dev/sdb
-
- Welcome to fdisk (util-linux 2.32.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
-
-
- Command (m for help): p
- Disk /dev/sdb: 238.5 GiB, 256060514304 bytes, 500118192 sectors
- Units: sectors of 1 * 512 = 512 bytes
- Sector size (logical/physical): 512 bytes / 4096 bytes
- I/O size (minimum/optimal): 4096 bytes / 4096 bytes
- Disklabel type: dos
- Disk identifier: 0xcbe5ad7a
-
- Device Boot Start End Sectors Size Id Type
- /dev/sdb1 2048 500118191 500116144 238.5G 83 Linux
下一步是使用xfs文件系统格式化/dev/sdb1分区。注意:我们格式化/dev/sdb1(分区),而不是/dev/sdb(磁盘本身)。
- [blctrl@localhost applications]$ sudo mkfs.xfs /dev/sdb1
- [sudo] password for blctrl:
- meta-data=/dev/sdb1 isize=512 agcount=4, agsize=15628630 blks
- = sectsz=4096 attr=2, projid32bit=1
- = crc=1 finobt=1, sparse=1, rmapbt=0
- = reflink=1 bigtime=0 inobtcount=0
- data = bsize=4096 blocks=62514518, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0, ftype=1
- log =internal log bsize=4096 blocks=30524, version=2
- = sectsz=4096 sunit=1 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
- Discarding blocks...Done.
为了磁盘被系统接受,我们需要挂载它到系统的文件系统。但首先,我们将创建一个我们将挂载这个分区的挂载点。
[blctrl@localhost applications]$ sudo mkdir -p /srv/home
下一步,我们将按以下在这个挂载点挂载这个分区。这实际上使得磁盘对系统可用。
[blctrl@localhost applications]$ sudo mount /dev/sdb1 /srv/home/
要验证这个,按如下运行df命令。
- [blctrl@localhost applications]$ sudo df -Th
- Filesystem Type Size Used Avail Use% Mounted on
- devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
- tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
- tmpfs tmpfs 3.7G 9.9M 3.7G 1% /run
- tmpfs tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
- /dev/mapper/rl-root xfs 90G 6.0G 84G 7% /
- /dev/mapper/rl-home xfs 14G 188M 14G 2% /home
- /dev/sda1 xfs 1014M 264M 751M 26% /boot
- tmpfs tmpfs 754M 64K 754M 1% /run/user/1000
- /dev/sdb1 xfs 239G 1.7G 237G 1% /srv/home
第五步: 把Home目录文件复制到新分区
我们现在复制home目录的内容到位于这个磁盘上的挂载点。我们运行命令:
[blctrl@localhost applications]$ sudo cp -aR /home/* /srv/home/
只要确认所有内容都被复制了,我们将检查home目录的内容:
我们可以从输出清楚地看到预计在home目录中地所有默认目录都出现了。
- [blctrl@localhost applications]$ ls -l /srv/home/blctrl/
- total 0
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Desktop
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Documents
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Downloads
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Music
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Pictures
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Public
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Templates
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Videos
我们现在需要创建另一个home目录,在其上我们将挂载我们地home分区。要避免冲突,我们重命名我们的当前home目录为/home.bak。
- [root@localhost /]# fuser -km /home # 查到使用home的进程
- [root@localhost /]# kill 2220 # 杀死以上使用home的进程
- [root@localhost /]# umount /home # 卸载掉home
- [root@localhost /]# mv /home /home.bak # 重命名
接着,我们创建一个新的home目录。
- [blctrl@localhost /]$ mkdir /home
- [blctrl@localhost /]$ ls
- bin boot dev etc home home.bak lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
我们将卸载/dev/sdb1文件系统并且挂载它到新创建的home目录:
- [root@localhost /]# umount /dev/sdb1
- [root@localhost /]# mount /dev/sdb1 /home
要验证/home目录包含默认的目录,我们进入到/home下并且列出其内容:
- [root@localhost /]# cd /home
- [root@localhost home]# ls
- blctrl
- [root@localhost home]# ls -l blctrl/
- total 0
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Desktop
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Documents
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Downloads
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Music
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Pictures
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Public
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Templates
- drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Videos
另外,我们可以使用df命令验证我们的文件系统挂载到了/home挂载点:
- [root@localhost home]# df -Th /dev/sdb1
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/sdb1 xfs 239G 1.8G 237G 1% /home
输出确认在我们磁盘上的dev/sdb1文件系统被挂载到了/home分区。但重启后,这将不存在。要使之永久,需要再补充一步并且那就是用文件系统信息修改/etc/fstab文件。
要自动确认在每次系统启动时挂载文件系统,我们要修改/etc/fstab文件。但首先,按如下获取文件系统的UUID。
- [root@localhost home]# blkid /dev/sdb1
- /dev/sdb1: UUID="23fea399-44c5-4fba-97e1-9b18a70e9efd" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="cbe5ad7a-01"
由于将在下一步中使用这个UUID,在文本编辑器中某处复制并且粘贴这个UUID。
接着,打开/etc/fstab文件。
[root@localhost home]# vim /etc/fstab
在打开的文件末尾添加一行。用/dev/sdb1文件系统的实际UUID替换在方括号中的uid:
UUID=[uid] /home xfs defaults 0 1
- #
- # /etc/fstab
- # Created by anaconda on Fri Jul 1 02:12:26 2022
- #
- # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
- # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
- #
- # After editing this file, run 'systemctl daemon-reload' to update systemd
- # units generated from this file.
- #
- /dev/mapper/rl-root / xfs defaults 0 0
- UUID=aaa8bd0f-cd64-4435-adc5-fc7e7ae6c27d /boot xfs defaults 0 0
- #/dev/mapper/rl-home /home xfs defaults 0 0
- /dev/mapper/rl-swap none swap defaults 0 0
- UUID=23fea399-44c5-4fba-97e1-9b18a70e9efd /home xfs defaults 0 1
保存并且关闭这个文件。接着运行以下命令来挂载所有分区:
[root@localhost home]# mount -a
现在,在系统每次启动时,你的驱动将被挂载到/home分区。
- [root@localhost home]# df -h /dev/sdb1
- Filesystem Size Used Avail Use% Mounted on
- /dev/sdb1 239G 1.8G 237G 1% /home
- [root@localhost home]# df -h
- Filesystem Size Used Avail Use% Mounted on
- devtmpfs 3.7G 0 3.7G 0% /dev
- tmpfs 3.7G 0 3.7G 0% /dev/shm
- tmpfs 3.7G 18M 3.7G 1% /run
- tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
- /dev/mapper/rl-root 90G 6.0G 84G 7% /
- /dev/sda1 1014M 264M 751M 26% /boot
- tmpfs 754M 0 754M 0% /run/user/1000
- tmpfs 754M 0 754M 0% /run/user/0
- /dev/sdb1 239G 1.8G 237G 1% /home
为了避免经历这些步骤,为了把你个人数据从文件系统文件分开,在安装过程中,建议把home分区与余下的系统分区分隔开。这使得在故障时恢复你的数据变得简单。