Linux move home to another partition

How to Move Home Directory to New Partition or Disk in Linux

On any Linux system, one of the directories that will surely grow in size has to be the /home directory. This is because system accounts (users) directories will reside in /home except root account – here users will continuously store documents and other files.

Another important directory with the same behavior is /var , it contains log files whose size will gradually increase as the system continues to run such as log files, web files, print spool files etc.

When these directories fill up, this can cause critical problems on the root file system resulting into system boot failure or some other related issues. However, sometimes you can only notice this after installing your system and configuring all directories on the root file system/partition.

In this guide, we will show how to move the home directory into a dedicated partition possibly on a new storage disk in Linux.

Installing and Partitioning a New Hard Disk in Linux

Before we proceed any further, we’ll briefly explain how to add a new hard disk to an existing Linux server.

Note: If you already have a partition ready for the operation, move to the section which explains the steps for moving /home directory in a partition of its own below.

We’ll assume you have attached the new disk to the system. On a hard disk, the number of partitions to be created as well as the partition table is normally determined by disk label type and the first few bytes of space will define the MBR (Master Boot Record) which stores the partition table as well as the boot loader (for bootable disks).

Although there are many label types, Linux only accepts two: MSDOS MBR (516 bytes in size) or GPT (GUID Partition Table) MBR.

Let’s also assume that the new new hard disk (/dev/sdb of size 270 GB used for the purpose of this guide, you probably need a bigger capacity on a server for large user base.

First you need to set the disk label type using fdisk or parted; we have used GPT label name in this example.

# parted /dev/sdb mklabel gpt

Note: fdisk only supports MSDOS MBR for now and parted supports both labels.

Now create the first partition (/dev/sdb1) with size 106GB. We have reserved 1024MB of space for the MBR.

# parted -a cylinder /dev/sdb mkpart primary 1074MB 107GB

Explaining the command above:

  • a – option to specify the partition alignment.
  • mkpart – sub command to create the partition.
  • primary – sets partition type as primary on the hard disk (other values are logical or extended).
  • 1074MB – beginning of partition.
  • 107GB – end of partition.

Now check the free space on the disk as follows.

We will create another partition (/dev/sdb2) with size 154GB.

# parted -a cylinder /dev/sdb mkpart primary 115GB 268GB

Next, let’s set the filesystem type on each partition.

# mkfs.ext4 /dev/sdb1 # mkfs.xfs /dev/sdb2

To view all storage devices attached on the system, type.

Читайте также:  Docker machine linux install

List New Storage Device

Moving Home Directory into a Dedicated Partition

Now we have added the new disk and created the necessary partition; it’s now time to move the home folder into one of the partitions. To use a fileysystem, it has to be mounted to the root filesystem at a mount point: the target directory such as /home.

First list the filesystem usage using df command on the system.

Linux Filesystem Usage

We will start by creating a new directory /srv/home where we can mount /dev/sdb1 for the time being.

# mkdir -p /srv/home # mount /dev/sdb1 /srv/home

Then move the content of /home into /srv/home (so they will be practically stored in /dev/sdb1) using rsync command or cp command.

# rsync -av /home/* /srv/home/ OR # cp -aR /home/* /srv/home/

After that, we will find the difference between the two directories using the diff tool, if all is well, continue to the next step.

Afterwards, delete all the old content in the /home as follows.

Next unmount /srv/home.

Finally, we have to mount the filesystem /dev/sdb1 to /home for the mean time.

# mount /dev/sdb1 /home # ls -l /home

The above changes will last only for the current boot, add the line below in the /etc/fstab to make the changes permanent.

Use following command to get the partition UUID.

# blkid /dev/sdb1 /dev/sdb1: UUID="e087e709-20f9-42a4-a4dc-d74544c490a6" TYPE="ext4" PARTLABEL="primary" PARTUUID="52d77e5c-0b20-4a68-ada4-881851b2ca99"

Once you know the partition UUID, open /etc/fstab file add following line.

UUID=e087e709-20f9-42a4-a4dc-d74544c490a6 /home ext4 defaults 0 2

Explaining the field in the line above:

  • UUID – specifies the block device, you can alternatively use the device file /dev/sdb1.
  • /home – this is the mount point.
  • etx4 – describes the filesystem type on the device/partition.
  • defaults – mount options, (here this value means rw, suid, dev, exec, auto, nouser, and async).
  • 0 – used by dump tool, 0 meaning don’t dump if filesystem is not present.
  • 2 – used by fsck tool for discovering filesystem check order, this value means check this device after root filesystem.

Save the file and reboot the system.

You can run following command to see that /home directory has been successfully moved into a dedicated partition.

Check Filesystem Usage on Linux

That’s It for now! To understand more about Linux file-system, read through these guides relating to filesystem management on Linux.

In this guide, we explained you how to move the /home directory into a dedicated partition in Linux. You can share any thoughts concerning this article via the comment form below.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Sorry, no posts were found.

68 thoughts on “How to Move Home Directory to New Partition or Disk in Linux”

  1. A 1.8T disk (sda) on which there is boot (sda1) and backup of the old /home I had in fedora 33 (sda2).
  2. A disk of about 30 G with linux (sdb1) swap (sdb2).
Читайте также:  Change machine name linux

This is the situation after I made the upgrade to Fedora35 with the new home on the small disk.

I would like to mount the /home on the big disk following your instructions above, but the big disk already has data on it, the file system of the old /home. How do I proceed not to lose everything?

Thank you for your help in advance

In Windows 7, 8, 10, 11, etc you go into c:\users\username and in that folder, there are folders like Videos, Documents, Pictures, and whatnot, right-click and choose properties, location tab and move that folder to a new drive and everything link’s perfect, its just that simple. Now I use Linux as my only OS, why can’t it be that simple. On Linux, I can’t go into files and click on documents for example as that is not where they are stored. Why isn’t there an easy way to do this? Reply

  1. Desktop (create link)
  2. 120Gb drive (a spare drive I have on the system)
  3. Data composition
  4. Mail recipient

Your system or file manager may not have exactly those options but I am sure that something like them will be available.

If you want to simply link a file then you may use the command from the command line.

Sorry, something went wrong there on two lines: “I went to /home//” should read “I went to /home/my-home-directory/” and, “use the command” should read “use the command ln “ Reply

Nice, but if I click on “send to” it just sends it to my email client, there is no other options, it is fine. All of my pictures, downloads, music, documents, and videos are stored on their own SSD so I thought I could make these the defaults instead of the default ones on my NVME where the operating system is. Reply

The step rm -rf /home is dangerous though, you cannot get it back. It should be mv /home /home-old -> this will backup everything at home, then if everything goes bad you can restore it. Reply

I don’t get it. wouldn’t you mv /home /”home-new”? or more specifically mv /home /dev/sdb/home? This article is all convoluted. It starts with creating an entire disk, which should be the subject of another article entirely. Either just explain moving /home to a new partition, and leave it, maybe then repeat whatever steps needed for the second example of moving /home to a new disk. And creating a second disk should be a separate article entirely. As Open Source idealists start becoming disillusioned, there are more and more crappy articles by people just writing for clickbait, and for deadlines. It makes me sad. Reply

My problem is that I believe in separating code and data. That is why I create two partitions, one for Linux and programs and a separate partition for my data. However, Linux developers insist that the home directory is in the same partition as the Linux code. Is it even possible to get Linux to use a directory on a separate partition as the “home directory”? Reply

No that’s not true at all, about “Linux developers”. In the 90s it was usually suggested every single directory immediately under root have it’s own partition. /boot, /dev, /etc, /home, /lib, and /opt, had it’s own partition, I forget further details of /bin and /sbin. But my point was this is rampant throughout all distros in the 80s and 90s. You just haven’t done your research! Early Linux books all explain this. The fact is that Windows users were content and only understood “C:”. Maybe an extra D; disk, or network disk tat might be Z: or K:. Forget individual partitions. So, after rabidly going after Windows users for 20 years, Most large Linux distros gave up and made it convenient to put everything, or nearly everything in one partition, so users could transition. By the way, Chrome OS typically has eleven partitions, all hidden from the user, and Mac OSX also hides partitions from users. Humans generally are too stupid to keep track of partitions. We are drowning in stupidity every day. I’m thinking of giving myself a lobotomy so I can blend in. I’m almost tempted to give you a pat on the head for wanting a different partition arrangement. But then you’re also too lazy to find this material in texts (online or off). So I guess there’s not much hope at all. Reply

Читайте также:  Router scan linux аналог

Thanks for your clear and useful instructions. I have to move my /home because the HDD is nearly full. I have a 1 Tb disk installed and I am preparing to partition it etc. I am confused about the partitions you create in your examples. sdb1 and sdb2 have different file systems and I wonder why it is necessary to have two partitions. My idea was to create the whole HDD as one partition to mount as /home . Should I have a second partition? I don’t need one for any other purpose. Reply

Replying to my own comment. What a loser 😉 I made a decision and flew with it. I created a partition on the new HDD that used all but the first MB of the disk. Then followed the instructions to ultimately mount it as /home by editing fstab. It worked. So if anyone else wondered about the partitioning you don’t need more than one. Reply

The rsync command with options. rsync -av should be rsync -av -A -X to preserve SELinux contexts – otherwise, some distributions will fail to mount /home properly (or at all) after this. Reply

Also, shopt -s dotglob if using cp. Not sure if rsync does hidden files by default, I have always just enabled dotglob for it if I want it to include hidden files. Reply

is rm -rf home/* not dangerous? At that time, other programs may crash or even create new directories. Should we not first mount the new drive? Reply

@Aaron Yes, a new line was added to /etc/fstab (using the correct UUID of course). I’m no Linux expert and so not being familiar enough to repair a system that I can’t boot into of course I scrapped the installation. It’s only a VM I’m experimenting with and I had a previous snapshot, so no great loss to me. But I still have no idea what I did wrong. I’ll have another go later. Reply

Источник

Оцените статью
Adblock
detector