How can I move the home directory to a separate partition?

I would like to install another distribution but keep my home directory. Is there a way to move the home directory to a separate partition? I don’t have an external hard drive available to back up my data. I would like to set up my partitions as suggested here.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your question is distro-neutral, so if I mention anything specific that you don’t have, just use the equivalent on your side.

I really recommend you buy an external for backups, trust me, losing your data is the worst. Proceed at your own risk – But if you can’t get one, here’s what you can do.

What you need

  • the size of your /home directory
  • free space, more than the size of your /home directory
  • disk partitioning tool, I recommend gparted

What to do

  • Check the size of your /home directory (the last result will be home total):

    du -h /home

  • Check if you have enough free space for the new partition:

    df -h

  • Install gparted

    sudo apt-get install gparted

You need more free space than the size of your /home directory. If you don’t have the free space, then you won’t be able to create that new partition, and need to move your data onto an external anyway.

If you have the space, use gparted to shrink your existing partition, and then create a new partition with the freed unallocated space. Once your new partition is ready, note it’s /dev/sdax (use sudo fdisk -l to see this), and copy your /home files to it.

Using the partition in a new distro

You mentioned installing another distro, if you plan to override your current distro, then during installation you should be asked to setup partitions. At that point you can specify this partition as /home, choose not to format it, and all will be well, you can skip this next section.

If however you want your current distro to work with the new /home partition, follow this section:

Mount the partition in an existing distro

We have to tell your OS to use the partition as your new /home, we do this in fstab, but first let us find the UUID of this new partition:

ls -l /dev/disk/by-uuid

Cross reference your new partition’s /sdax and copy the UUID of it, mine looks like 3d866059-4b4c-4c71-a69c-213f0e4fbf32.

Backup fstab: sudo cp /etc/fstab /etc/fstab.bak
Edit fstab: sudoedit /etc/fstab

The idea is to add a new line that mounts the partition at /home. Use your own UUID, not the one I post here 😉

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
UUID=3d866059.. /home           auto    default         0       1

Save and restart, and test if the new partition mounts to /home. Run df -h to list all mounted partitions, /home should now be in that list.


Notes

  • It might be a good idea to familiarize yourself with fstab if you don’t know it well. Just take your time and think about each step.
  • If you install a new distro, and use the same login name, your old /home files will automatically fall under your ownership.
  • This is not a trivial topic to cover in one post, but I think I got most of it. 🙂

Method 2

The title of the post and your question caused some confusion to me. Do you want to separate your home into a partition, or do you just want to reinstall and keep the same home?

If all that you want is to reinstall the whole OS while keeping your home then you can backup your home into a place that will not be affected by the install, then restore it after that, together with a permission fix (use chown -R username:groupname to make sure ownership is correct).

If you want to follow some best practices and separate your home then here is the guide you need. It’s written for Ubuntu, but I think the same thing goes for other distros.

Method 3

Assuming you have a separate partition already (and if you don’t, you probably want to use gparted or something similar, to make one), you simply do the following (preferably as the root user):

 cp -R /home/whatever /path/to/destination
 mv /home/whatever /home/whatever.orig
 ln -s /path/to/destionation /home/whatever

And then assuming that everything works the way it should (log in as the user and test)

 rm -rf /home/whatever.orig

This copies the entire directory (and all the contents) to the destination mountpoint, renames the original home directory (in case something is screwed up in the process, then we can recover), creates a symbolic link from the new location to the original home directory (which everything (like /etc/passwd) is still pointing to), then assuming it worked, removes the backup copy we made, leaving the copy we put at the destination filesystem.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x