banner



how to create a new home directory bash

Linux terminal on stylized laptop
Fatmawati Achmad Zaenuri/Shutterstock

Want to move your Linux home folder to another drive? Here's a straightforward and step by step way to do it that should work on any distribution. Moving your home folder means you can reinstall Linux and not have to worry about your personal files.

Why Keep Your home Folder Separate?

If you're setting up a new machine or adding a hard drive to an existing one, you may want to have your home directory on a different drive than the default location.

An increasingly popular configuration for modern personal computers is to have a medium-sized Solid State Drive (SSD) holding your operating system and a larger Solid State Hybrid Drive (SSHD) or traditional hard drive (HD) as your the main storage for data. Or you may have a single traditional hard drive in your system, and you've added a new HD for increased storage. Whatever your reasons, here is a simple and blow by blow run-through of moving your home directory.

By the way, if you're installing a Linux system from scratch, you'll probably see an option to create a separate home directory in your Linux distribution's installer. Generally, you'll just need to go into the partitioning options, create a separate partition, and mount it at "/home". But, if you've already installed a Linux distribution, you can use these instructions to move your current home directory to a new location without losing anything or reinstalling your operating system.

Now, before we start, go and make a backup.

RELATED: How to Back Up Your Linux System With rsync

Identify the Drive

If you've just fitted a drive to a Linux computer, or installed Linux to one of the drives in a new multi-drive computer, and rebooted, there's little evidence that the new drive is even present.

The fdisk command will list the drives and their partitions for us.

sudo fdisk -l

Scroll through 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. So /dev/sde would be the fifth hard drive in the system.

in this example, the new drive is the second drive to be fitted to the system. So we need to look for an entry for/dev/sdb.

/dev/sdb is highlighted above. You'll notice that it doesn't have a line describing a partition on it. It's a brand new drive so it won't have one yet. We need to create the partition. We can do so using fdisk. If your hard drive is not /dev/sdb, make sure you substitute /dev/sdb with the actual drive identifier for your new hard drive in the command.

sudo fdisk /dev/sdb

When fdisk prompts you for a command, press the letter p. This prints the partition table for the hard drive. We know it won't have one, but we get some useful information about the drive. It gives us a good chance to make sure that the drive we're going to create a partition for is the drive we intended to work with.

It tells us that the drive is a 1TB drive, which matches what we expect in this test machine, so we'll proceed.

Create a Partition

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

We're going to create a single partition for the whole disk, so when prompted for the first sector we can press Enter to accept the default value. You will then be prompted for the last sector, and Enter will accept the default value.

Althoughfdisk confirms that it has created a 1TB Linux partition, which is partition number 1, nothing has changed on the hard drive yet. Until you give fdisk the command to write the changes to the drive, the drive is untouched. Once you are certain you're happy with our choices, press the letter w to write the changes to the drive.

The partition has been written to /dev/sdb . Let's check what just happened. We'll use fdisk once more on /dev/sdb.

sudo fdisk /dev/sdb

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

We don't want to make any changes to the partition, so press the letter q to quit.

Create a File System on the Partition

We need to create a filesystem on the partition. This is easily achieved with the mkfs command. Note that you must include the partition number in the command. Be careful to type /dev/sdb1 (the partition) and not /dev/sdb (the drive).

sudo mkfs -t ext4 /dev/sdb1

The filesystem will be created for you, and you'll be returned to the command prompt.

Mounting the New Drive

To use the new drive, we must mount the partition on it to a mount point in the filesystem. Actually, to be perfectly accurate, we're neither mounting the drive nor the partition, we're mounting the filesystem on the partition, by grafting it onto your system's filesystem tree.

The /mnt point is as good a place as any. It is only a temporary mount point to allow us to copy data to the new drive. We're going to use the mount command to mount the filesystem on the first partition on /dev/sdb, at /mnt .

sudo mount /dev/sdb1 /mnt

If all goes well, you'll be returned to the command line with no error messages. Let's see if we can change directory to our newly mounted filesystem.

cd /mnt

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

ls -ahl

We're in our new file system. The default "lost+found" directory is not required so we can remove it.

sudo rm -rf lost+found

Copying Your Home Folder

We need to copy everything from the old home directory to the newly mounted filesystem. Using the r (recursive) and p (preserve) options will ensure all subdirectories are copied and that file ownerships, permissions, and other attributes are retained.

sudo cp -rp /home/* /mnt

When the copy has completed, use ls to have a look around and verify that your data is where you expect it to be in the new filesystem. In other words, if /mnt was your home directory, is everything present and correct?

ls
ls dave

You'll probably want to be a bit more thorough than we were on the test machine this article was researched on. As a safety net, we're going to rename and keep your old /home directory until you're satisfied that it is safe to delete it.

sudo mv /home /home.orig

And we'll create a new, empty home directory.

sudo mkdir /home

We'll use that new empty home directory as the mount point for our filesystem on the new hard drive. We need to unmount it from /mnt and remount it on /home. Note that the command umount doesn't have an "n" after the "u."

But first, we'll change into the root directory (with cd / ) to make sure we're not in a directory that is going to be included in the mount or unmount locations.

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

RELATED: The Linux Directory Structure, Explained

Testing Your New home Directory

Let's see what the attributes of the /dev/sdb1 partition are now:

df /dev/sdb1

We're shown the name of the filesystem, the size of the partition and the used and available space on it, and importantly, where it is mounted. It is now our /home directory.  That means we should be able to reference it exactly as we could the old /home directory.

If we move to some arbitrary point in the filesystem, we ought to be able to change back to /home using the ~ tilde shortcut.

cd /
cd ~
pwd
ls

cd /home
ls
cd dave
ls
ls -a

We can move through the filesystem back and forth to /home using explicit commands and using the ~ shortcut. The folders, files, and dotfiles we'd expect are all present. It's all looking good.

If anything was missing, we could copy it out of the /home.orig directory, which we still have access to in the root of the filesystem. But it all looks fine.

Now we need to have /dev/sdb1 mounted automatically every time your computer is started.

Editing fstab

The "fstab" file contains descriptions of the filesystems that are going to be mounted when the system boots. Before we make any changes to it, we'll make a backup copy of it that we can return to in the event of problems.

sudo cp /etc/fstab /etc/fstab.orig

Now we can edit the fstab file. Use your favorite editor, we're using gedit. Any text editor willdo.

sudo gedit /etc/fstab

You must add a line at the bottom of the file to mount our new /home directory. If your drive and partition identifiers are different than the ones used in this example, substitute those for the /dev/sdb1 shown here.

  • Type the name of the partition at the start of the line, and then press Tab.
  • Type the mount point, /home,  and press Tab.
  • Type the filesystem descriptionext4, and press Tab.
  • Type defaults for the mount options, and press Tab.
  • Type the digit 0 for the filesystem dump option, and press Tab.
  • Type the digit 0 for the filesystem check option.

Using gedit to edit the fstab file

Save the fstab file.

Reboot Your System

We need to reboot to verify that everything has gone according to plan and that you have a seamless connection to your new /home directory.

If it doesn't, you've still got the safety net of your original /home directory and fstab file that could be restored if required. Because of the precautions we've taken—copying the /home directory and fstab files—you could easily return your system to the state it was in before you started.

sudo reboot now

RELATED: How to Reboot or Shut Down Linux Using the Command Line

Final Checks

When your system restarts let's just check that your /home directory is really on your new hard drive, and your system hasn't somehow (miraculously) reverted to using the old /home directory.

df /dev/sdb1

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

Once you're perfectly sure that you no longer need the safety copy of your old /home directory, you can delete it:

cd /
sudo rm -rf home.orig/

And of course, if you do realize something didn't copy over from the old /home to your new /home, you'll be able to retrieve it from the backup you made before we started.

Home Sweet Home

Now that you've separated your /home directory from the rest of the operating system's partition, you can re-install your operating system, and your data will be untouched. All you have to do is edit the fstab file to mount your second drive on /home.

And because all of your dotfiles are in your /home directory, when you fire up your various applications, they'll find all of your settings, preferences, and data.

It takes the pain out of reinstalls and takes the risk out of upgrades.

how to create a new home directory bash

Source: https://www.howtogeek.com/442101/how-to-move-your-linux-home-directory-to-another-hard-drive/

Posted by: grahamladmoultan.blogspot.com

0 Response to "how to create a new home directory bash"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel