ubuntu tips: Move /home to it’s own partition(移动"/home"目录到单独的分区)

本文介绍如何将Ubuntu系统中的/home目录迁移至独立分区,包括创建分区、挂载、复制文件、设置fstab等步骤。
  You can move any directory with this tips, and I just use this tip move my home directory to a independent disk.

  Having the “/home” directory tree on it’s own partition has several advantages,the biggest perhaps being that you can reinstall the OS (or even a different distro of Linux) without losing all your data. You can do this by keeping the /home partition unchanged and reinstalling the OS which goes in the “/” (root) directory, which can be on a seperate partition.

  But you, like me, did not know this when you first installed Ubuntu, and have not created a new partition for “/home” when you first installed Ubuntu. Despair not, it is really simple to move “/home” to its own partition.


  First, create a partition of sufficient size for your “/home” directory. You may have to use that new hard drive, or adjust/resize the existing partition on your current hard-drive to do this. Let me skip those details.

  Next, mount the new partition:
$mkdir /mnt/newhome
$sudo mount -t ext3 /dev/hda5 /mnt/newhome
(You have to change the “hda5″ in the above to the correct partition label for the new partition. Also, the above assumes that the new partition you created is formatted as an ext3 partition. Change the “ext3″ to whatever filesystem the drive is formatted to.)

  Now, Copy files over:
Since the “/home” directory will have hardlinks, softlinks, files and nested directories, a regular copy (cp) may not do the job completely. Therefore, we use something we learn from the Debian archiving guide:
$cd /home/
$find . -depth -print0 | cpio --null --sparse -pvd /mnt/newhome/


Make sure everything copied over correctly. You might have to do some tweaking and honing to make sure you get it all right, just in case.

Next, unmount the new partition:
$sudo umount /mnt/newhome

Make way for the new “home”
$sudo mv /home /old_home

Since we moved /home to /old_home, there is no longer a /home directory. So first we should recreate a new /home by:
sudo mkdir /home

Mount the new home:
$sudo mount /dev/hda5 /home
(Again, you have to change “hda5″ to whatever the new partition’s label is.)

Cursorily verify that everything works right.

Now, you have to tell Ubuntu to mount your new home when you boot. Add a line to the “/etc/fstab” file that looks like the following:

/dev/hda5 /home ext3 nodev,nosuid 0 2
(Here, change the partition label “hda5″ to the label of the new partition, and you may have to change “ext3″ to whatever filesystem you chose for your new “home”)

Once all this is done, and everything works fine, you can delete the “/old_home” directory by using:
$sudo rm -r /old_home

Michael, Russ and Magnus posted this solution on the ubuntu-users mailing list a few months ago.
### 将硬盘挂载至Ubuntu系统的/home目录 #### 准备工作 为了确保数据安全,在执行任何磁盘操作之前,建议备份重要文件。接下来的操作涉及分区调整和挂载设置。 #### 查看当前磁盘情况 首先查看系统中的磁盘及其分区状况: ```bash lsblk fdisk -l ``` 这有助于了解哪些设备可以用于挂载以及它们的路径[^1]。 #### 创建或选择目标分区 如果已有未分配的空间或者额外的物理/逻辑卷,则可以直接跳过此步;如果没有合适的分区,可能需要先创建一个新的分区。对于已经存在的大容量NTFS/FAT32格式化过的Windows分区,不推荐直接作为Linux `/home` 使用,因为这些文件系统缺乏权限控制机制,不利于安全性维护。 #### 安装必要的工具并启动GParted 安装图形化的分区管理器 GParted 来更方便地管理和修改分区: ```bash sudo apt update && sudo apt install gparted ``` 运行 `gparted` 并按照提示完成所需分区的创建或调整大小的任务[^3]。 #### 格式化新分区(可选) 假设选择了/dev/sdbX (其中 X 是具体的编号),将其格式化为ext4 文件系统(适用于大多数场景): ```bash sudo mkfs.ext4 /dev/sdbX ``` 注意替换实际使用的设备名前缀(`sda`, `sdb`)分区(X)。 #### 修改fstab配置实现自动挂载 编辑/etc/fstab 文件来定义开机自启时应加载的内容。打开该文件进行编辑: ```bash sudo nano /etc/fstab ``` 向文件末尾追加一行描述要挂载的新分区的信息, 如下所示: ``` UUID=your-partition-uuid /mnt/new_home ext4 defaults 0 2 ``` 这里需要用真实的 UUID 替换 "your-partition-uuid", 可通过命令获取: ```bash sudo blkid | grep sdbX ``` 同时考虑到最终目的是让其成为用户的 home 目录而不是临时位置 "/mnt/new_home"。 #### 移动原有HOME内容到新分区 停止不必要的服务进程以减少活动文件的数量,然后复制现有的 `/home/*` 到新的挂载点上: ```bash rsync -avxHAXW --info=progress2 /home/. /mnt/new_home/ ``` 确认无误后删除旧版 `/home` 下面的所有东西,并更新 fstab 中的目标路径指向真正的 `/home`. 最后重启计算机验证一切正常运作.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值