قالب وردپرس درنا توس
Home / Tips and Tricks / How to move your Linux home directory to another drive

How to move your Linux home directory to another drive



  Linux terminal on stylized laptop
Fatmawati Achmad Zaenuri / Shutterstock

Do you want to move your Linux home folder to another drive? Here is a simple and step-by-step guide that should work on every distribution. By moving your home folder, you can reinstall Linux and you do not have to worry about your personal files.

Why should you keep your home folder separate?

When you set up a new computer or add an existing hard disk You may want to have your home directory on a different drive than the default directory.

An increasingly popular configuration for modern PCs is a mid-sized Solid State Drive (SSD) that stores your operating system and a larger solid state-state drive (SSHD) or traditional hard drive (HD) as main storage for data. Or you have a single conventional hard drive in your system and have added a new hard drive to increase storage. Whatever your reasons, here's a quick and easy way to move your home directory. By the way, if you reinstall a Linux system from scratch, you will probably see an option to create a separate home directory in the installer of your Linux distribution. In general, all you have to do is invoke the partition options, create a separate partition, and deploy them under / home. However, if you already have a Linux distribution installed, these instructions will help you move your current home directory to a new location without losing anything or reinstalling the operating system.

Before we begin, make a backup.

RELATED: How to Back Up Your Linux System

Identify the Drive

If you have just connected a drive to a Linux computer or installed Linux on one of the drives in a new computer with multiple drives and a reboot, there is little evidence that the new drive even exists.

The fdisk command lists the drives and their partitions for us.

  sudo fdisk -l 

  sudo fdisk -l in a terminal window

Scroll down the output until you have identified the new drive. The first drive is named / dev / sda the second is / dev / sdb and so on, with the last letter increasing each time. / dev / sde would be the fifth hard drive in the system.

In this example, the new drive is the second drive to be built into the system. So we need to search for an entry for / dev / sdb .

 The output of fdisk in a terminal window is marked with / dev / sdb.

/ dev / sdb is highlighted above. You will notice that there is no line describing a partition. It's a brand new drive, so it does not exist yet. We have to create the partition. We can do this with fdisk . If your disk is not / dev / sdb make sure that you replace / dev / sdb with the actual drive ID for your new disk in the command

  sudo fdisk / dev / sdb 

  sudo fdisk / dev / sdb in a terminal window

If fdisk prompts you to enter a command, press the letter p . This prints the partition table for the hard disk. We know that there are none, but we get useful information about the drive. It's a great way to make sure the drive we're creating a partition for is the drive we want to work with.

It indicates that it is a 1 TB drive that meets our expectations. If you are using this test machine, continue.

Create partition

Press the letter n for a new partition and then p for a primary partition. When asked for the partition number, press the number 1 .

We will create a single partition for the entire disk. So, if you are asked for the first sector, you can press Enter to accept the default value. You will then be asked to enter the last sector and the Enter key will default to.

 Creating a partition with fdisk in a terminal window

Although fdisk confirms this has created a 1 TB Linux partition that is partition number 1. Nothing has changed yet on the hard disk. Until you issue fdisk the command to write the changes to the drive, the drive remains untouched. If you are sure that you are satisfied with our selection, press the letter w to write the changes to the drive.

 Write the fdisk changes to the drive in a terminal window

The partition was written in / dev / sdb . Let's check what has just happened. We will use fdisk once more in / dev / sdb .

  sudo fdisk / dev / sdb 

  sudo fdisk / dev / sdb in a terminal window

Press the letter p to print this partition table, and you will see that for the drive now a partition is listed. Since it was the first partition on this drive, it is named / dev / sdb1 . A second partition would be named / dev / sdb2 and so on.

We do not want to make any changes to the partition, so press q to exit.

Creating a file system on the partition

A file system must be created on the partition. This can easily be achieved with the command mkfs . Note that you must insert the partition number in the command. Enter / dev / sdb1 (the partition) and not / dev / sdb (the drive).

  sudo mkfs -t ext4 / dev / sdb1 

  sudo mkfs -t ext4 / dev / sdb1 in a terminal window

The file system will be created for you and you will be returned to the command prompt.

 Output of the mkfs command in a terminal window

Mounting the new drive

To use the new drive, you must mount the partition to a mount point in the file system. In fact, we do not mount the drive or the partition. We mount the file system on the partition by inserting it into the file system tree of your system . [19659006] The point / mnt is as good as any other. It's just a temporary mount point where we can copy data to the new drive. We will use the command mount to mount the file system on the first partition of / dev / sdb under / mnt in sudo mount

  / dev / sdb1 / mnt 

  sudo mount / dev / sdb1 / mnt in a terminal window

If everything is OK, return to the command line without error messages. Let's see if we can change the directory to our newly mounted filesystem.

  cd / mnt 

  cd / mnt in a terminal window

Yes, we can. Let's see what's here.

  ls -ahl 

  ls -ahl in a terminal window

We are in our new file system. The default directory "lost + found" is not required for us to remove it.

  sudo rm -rf lost + found 

  sudo rm -rf lost + found in a terminal window

Copying your home folder

We need to copy everything from the old home directory to the newly mounted file system. Using the options r (recursive) and p (preserve) ensures that all subdirectories are copied and that the file owner, permissions, and other attributes are preserved.

  sudo cp -rp / home / * / mnt 

  sudo cp -rp / home / * / mnt in a terminal window

When the copy process is complete, you can use ls and check that your data is where you expect it in the new file system. In other words, if / mnt was your home directory, is everything available and correct?

  Is 
  Dave 

  Is he in a terminal window

? I probably want to be a little more thorough than we on the test machine that researched this article. As a safety net, we will rename and keep your old / home directory until you are satisfied that it can be safely deleted.

  sudo mv /home/home.orig[19659065] sudo mv / home /home.orig in a terminal window " width="646" height="57" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

And we create a new, empty home directory.

  sudo mkdir / home 

  sudo mkdir / home in a terminal window

This new empty home directory is used as the mount point for our file system on the new disk. We have to unmount it from / mnt and reinsert it on / home . Note that the command umount has no "n" after the "m".

  sudo umount / dev / sdb1 
  sudo mount / dev / sdb1 / home / 

  sudo umount / dev / sdb1 in a terminal window

RELATED: The explained Linux Directory Structure

Testing the New Home Directory

Let's look at the attributes of . / dev / sdb1 Partition is now:

  df / dev / sdb1 

  df / dev / sdb1 in a terminal window

We can see the name of the file system, the size of the partition, and the used and available space on it and especially the place where it is provided. It is now our / home directory . That is, we should be able to reference it as well as the old / home directory.

If we switch to any point in the file system, we should be able to switch back to ] / home Use the tilde shortcut ~ .

  cd / 
  cd ~ 
  pwd 
  ls 

  cd / and other commands in a terminal window to test the home directory

  cd / home 
  ls [19659060] cd dave 
  ls 
  ls -a 

  cd / home and other commands to test the / home firebox in a terminal window

With explicit commands and the shortcut ~ we can switch to the file system after / home . The folders, files and point files we expect are all there. It looks good.

If something was missing, we could copy it from the /home.orig directory, which we can still access at the root of the file system. But everything looks good.

Now we need to automatically enable / dev / sdb1 every time you restart your computer.

Editing fstab

The fstab file contains descriptions of the file systems mounted at system startup. Before we make any changes, we make a backup of it, which we can refer back to in case of problems.

  sudo cp / etc / fstab /etc/fstab.orig[19659098[sudocp/etc/fstab/etc/fstaborigineinclient_window" width="646" height="57" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

Now we can edit the fstab file.

  sudo gedit / etc / fstab 

  sudo gedit .etc.fstab in a terminal window

You must add a line to the end of the file to mount our new directory / home , If your drive and partition names are different from those used in this example, replace / dev / sdb1 shown here.

  • Enter the name of the partition at the beginning of the line, and then press the Tab key
  • . Enter the mount point / home and press the tab key
  • . Enter the file system description ext4 and press the tab key
  • ] defaults for the mount options and press the tab key.

      Enter the digit  0  for the file system dump option and press the Tab key. 
  • Enter the digit 0 for the file system check option.

  Editing the fstab file with gedit.

Save the fstab file.

Restart your system.

We need to reboot to make sure that everything went to plan and that you have a seamless connection to your new / home directory.

If this is not the case, you still have the safety net of your original / home and your fstab file, which can be restored if necessary. Because of the precautionary measures we took - copying the / home directory and the fstab files - you can easily restore your system to the state it was in before booting up Now in a terminal window " width="646" height="57" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>

RELATED: Restart or shut down Linux from the command line

Final checks

When your system restarts, we simply check to see if your The Directory] / Home is really on your new hard drive, and your system has not miraculously returned to using the old directory / home .

  df / dev / sdb1 [19659121] df / dev / sdb1 in a terminal window " width="646" height="122" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

Great, it's mounted on / home . Mission accomplished.

If you are absolutely certain that you no longer need the backup copy of your old / home directory, you can delete it:

  cd / 
  sudo rm -rf home.orig / 

  sudo rm -rf home.orig / in a terminal window

And of course, if you remember something that has not copied from the old / home to yours new / home you can retrieve it from the backup that you created before starting.

Home Sweet Home

After You Disconnect Your / home directory on the rest of the operating system partition allows you to reinstall your operating system, leaving your data untouched. All you need to do is edit the fstab file to mount your second drive in / home .

And because all of your point files are in your / home directory when you fire When you launch your various applications, they find all your settings, settings, and data.

New installations are effortless and upgrades are risky.




Source link