Skip to main content
Engineering LibreTexts

04-A.7.2: Disk Setup Process - mkfs & mount

  • Page ID
    26840
  • The mkfs Command

    The mkfs command is used to make a file system on a device, usually a partition on a hard disk drive (HDD), or it can also be a USB drive, etc.

    Syntax:

    mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]

    Command Options:

    Options Option Meaning
    -t fstype Specifies the type of file system to be built. If not specified, the default file system type (currently ext2) is used.
    fs-options File system-specific options to be passed to the real file system builder. Although not guaranteed, the following options are supported by most file system builders.
    -c Check the device for bad blocks before building the file system.
    -l filename Read the bad blocks list from filename.

    Command Line Formatting

    The mkfs utility is used to create filesystem (ext2, ext3, ext4, etc) on your Linux system. You should specify the device name to mkfs on which the filesystem is to be created. WARNING: Executing these commands will destroy all the data on your filesystem.

    To format the new partition as ext4 file system you need to use the file system specific mkfs - where you can use ext3, ext4, etc.

    pbmac@pbmac-server $ sudo mkfs.ext4 /dev/sda1

    As always, substitute "/dev/sda1" with your own partition's path.

    Create A Mount Point

    Now that the drive is partitioned and formatted, you need to choose a mount point. This will be the location from which you will access the drive in the future. I would recommend using a mount point with "/media", as it is the default used by Ubuntu. For this example, we'll use the path "/media/mynewdrive"

    pbmac@pbmac-server $ sudo mkdir /home/Project2020

    Now we are ready to mount the drive to the mount point.

    Mount the Drive

    You can choose to have the drive mounted automatically each time you boot the computer, or manually only when you need to use it.

    Automatic Mount at Boot

    You'll need to edit /etc/fstab: (gedit is a simple text editor, you can use nano or vim).

    pbmac@pbmac-server $ sudo gedit /etc/fstab

    Once in editor you should add the line that begins with "/dev/sda6" to the end of the file. The following example to add a partition we have created and it will reside on the /dev/sda6 and is labeled /home/Project2020. The green text is an explanation of the various fields in this file.

    # <file system> <mount point>      <type>  <options>            <dump>  <pass>
    proc            /proc               proc    nodev,noexec,nosuid   0       0
    /dev/sda6       /home/Project2020   ext4    defaults              0        2
    [--------]      [----------------]  [--]    [------------------------------]
    Linux Device      Mount point       File            Mount Options
                                      Sytem Type
    

    Note: some Linux distros now recommend to use UUID in the /etc/fstab file. Check with your distro documentation to find out how to discover the UUID value for your partitions. Here is some help from Ubuntu.

    If you are on a distribution of Linux that requires the use of the UUID mentioned above then you have a few more steps. You may have to consult your distros man pages for how to find the UUID for your storage devices. The example below uses commands on an Ubuntu distro.

    pbmac@pbmac-server $ ls -l /dev/disk/by-uuid/
    total 0
    lrwxrwxrwx 1 root root 10 Jun 15 11:22 3709f1b7-6417-413e-a4cd-2bec3002ba25 -> ../../sda6

    We know the partition will be mounted on /dev/sda6, so we now have the UUID value. Edit the /etc/fstab file and enter:

    # <device>                                 <dir>              <type>   <options>           <dump>   <fsck>
    UUID=fcdc8d9a-73f9-40a2-8a66-3d6f18f16ba3 /home/Project2020    ext4     defaults              0       2
    

    You can now run sudo mount -a (which will mount all of the file system specified in the /etc/fstab file) or reboot the computer to have the changes take effect.

    If you want to allow a normal user to create files on this drive, you can either give this user ownership of the top directory of the drive filesystem: (replace USERNAME with the actual username).

    pbmac@pbmac-server $ sudo chown -R pbmac:pbmac /home/Project2020

    Or, in a more flexible way and practical if you have several users, allow for instance the users in the proj2020 group (usually those who are meant to be able to mount removable disks, desktop users) to create files and sub-directories on the disk:

    pbmac@pbmac-server $ sudo chgrp proj2020 /home/Project2020
    pbmac@pbmac-server $ sudo chmod g+w /home/Project2020e
    pbmac@pbmac-server $ sudo chmod +t /home/Project2020

    The last chmod +t adds the sticky bit, so that people can only delete their own files and sub-directories in a directory, even if they have write permissions to it (see man chmod).

    Manually Mount

    Alternatively, you may want to manually mount the drive every time you need it.

    For manual mounting, use the following command:

    pbmac@pbmac-server $ sudo mount /dev/sda6 /home/Project2020

    When you are finished with the drive, you can unmount it using:

    pbmac@pbmac-server $ sudo umount /home/Project2020

    Adapted from:
    "InstallingANewHardDrive" by Robert Unverzagt, The Community Help Wiki is licensed under CC BY-SA 4.0
    "mkfs Command in Linux with Examples" by shivaysabharwal, Geeks for Geeks is licensed under CC BY-SA 4.0

    • Was this article helpful?