How to create and mount filesystems in Linux – The Geek Diary (2023)

File System Types

Creating a file system writes information to the device and creates order of the empty space. This file system–related data consumes a small percentage of the space. The remaining space on the disk drive is split into small, consistently sized segments called blocks. Linux supports a number of file system types, some of which are described as follows.

ext2High performance for fixed disk and removable media
ext3Journaling version of ext2
ext4Supports larger files and file system sizes
vfatMS-DOS file system useful when sharing files between Windows and Linux
XFSHigh-performance journaling file system
BtrfsAddresses scalability requirements of large storage systems

Creating Filesystems

The command to build a Linux file system on a device, or hard disk partition, is mkfs. The syntax for the command is:

# mkfs [options] device

The mkfs command is actually a front end for the different file system builder utilities such as mkfs.ext2 and mkfs.ext4. These utilities are executable directly from the command line. When using the mkfs wrapper, include the -t fstype option to specify the type of file system to be built. If not specified, the default file system type, ext2, is created.

To see which supported file system types are installed, use the ls /sbin/mkfs* command:

# ls -lrt /sbin/mkfs*-rwxr-xr-x. 1 root root 28624 Jun 10 2014 /sbin/mkfs.fatlrwxrwxrwx. 1 root root 8 Jan 7 2015 /sbin/mkfs.msdos -> mkfs.fatlrwxrwxrwx. 1 root root 8 Jan 7 2015 /sbin/mkfs.vfat -> mkfs.fat-rwxr-xr-x. 4 root root 96296 Aug 3 2017 /sbin/mkfs.ext4-rwxr-xr-x. 4 root root 96296 Aug 3 2017 /sbin/mkfs.ext3-rwxr-xr-x. 4 root root 96296 Aug 3 2017 /sbin/mkfs.ext2-rwxr-xr-x. 1 root root 368464 Aug 4 2017 /sbin/mkfs.xfs-rwxr-xr-x. 1 root root 375240 Aug 6 2017 /sbin/mkfs.btrfs-rwxr-xr-x. 1 root root 37104 Dec 1 23:28 /sbin/mkfs.minix-rwxr-xr-x. 1 root root 36984 Dec 1 23:28 /sbin/mkfs.cramfs-rwxr-xr-x. 1 root root 11520 Dec 1 23:28 /sbin/mkfs

The mkdosfs, mkfs.msdos, and mkfs.vfat files are symbolic links to mkfs.fat.

Using mkfs

The default file system type created when using the mkfs command is ext2. As previously mentioned, mkfs is a wrapper that calls other file system build utilities. Therefore, any of the following commands create an ext2 file system on the specified device:

# mkfs /dev/xvdd1# mke2fs /dev/xvdd1# mkfs.ext2 /dev/xvdd1

To create an ext3 file system, use any of the following commands:

# mkfs –t ext3 /dev/xvdd1 # mke2fs –t ext3 /dev/xvdd1 # mkfs.ext3 /dev/xvdd1

To create an ext4 file system, use any of the following commands:

# mkfs –t ext4 /dev/xvdd1 # mke2fs –t ext4 /dev/xvdd1 # mkfs.ext4 /dev/xvdd1

Configuration File

A number of options are available to customize block size, fragment size, blocks per group, journal options, number of inodes, and other parameters. Without including any options, the defaults that are specified in the /etc/mke2fs.conf configuration file are used.

Sample /etc/mke2fs.conf file

File System Labels

A useful option for the file system build utilities is the -L name option. This assigns a label to the partition; this label can be used instead of the device name when mounting the file system. Labels are limited to a maximum size of 16 characters. For existing file systems, the e2label command is used to display or set a label.

File systems are automatically assigned a universally unique identifier (UUID). UUIDs can be used when mounting the file system. To display the UUID, the label, and the file system type, use the blkid command. The following examples illustrate creating different file systems, with and without a label, and displaying the information with the blkid command. To create an ext2 file system and display information, enter:

# mkfs /dev/xvdf1# blkid /dev/xvdf1/dev/xvdf1: UUID="41bae7c3-396a-436d-9764-d27d4ca3f17b" TYPE="ext2"

To create an ext3 file system and display information, enter:

# mkfs -t ext3 /dev/xvdf1# blkid /dev/xvdf1/dev/xvdf1: UUID="c8e958f4-e5d0-4404-8a9a-2cea24675fcd" SEC_TYPE="ext2" TYPE="ext3"

To create an ext4 file system, assign a label name, and display information, enter:

# mkfs -t ext4 -L "Test Label" /dev/xvdf1 # blkid /dev/xvdf1/dev/xvdf1: LABEL="Test Label" UUID="687eb83f-c16f-4fa9-bb49-1621eed3a35d" TYPE="ext4"

Mounting File Systems

File systems on different partitions and removable devices, such as CDs, DVDs, or USB flash drives, must be attached to the directory hierarchy to be accessed. To attach a partition or device, a mount point must be created. A mount point is simply a directory created with the mkdir command. After a directory, or mount point, is created, attach the partition by using the mount command. Syntax for the mount command is:

# mount [options] device_file mount_point

The following example creates a mount point (/test) and attaches the partition:

# mkdir /test# mount /dev/xvdf1 /test

Alternatively, mount the partition or device by referencing the UUID or label. The following example displays the UUID and label, using the blkid command, and mounts the partition by referencing each:

# blkid /dev/xvdf1/dev/xvdf1: LABEL="Test Label" UUID="687eb83f-c16f-4fa9-bb49-1621eed3a35d" TYPE="ext4"
# mount LABEL="Test Label" /test# mount UUID="687eb83f-c16f-4fa9-bb49-1621eed3a35d" /test

The mount command without any options displays all currently attached file systems:

# mount | grep test/dev/xvdf1 on /test type ext4 (rw,relatime,seclabel,data=ordered)

In this example, the /dev/xvdf1 partition is mounted on /test. The file system type is ext4 and is mounted for both reading and writing. The df command also displays mounted file systems. Example:

# df -hP /testFilesystem Size Used Avail Use% Mounted on/dev/xvdf1 923M 2.4M 857M 1% /test

The information in the proc file system displays mounted file systems. Example:

# cat /proc/mounts | grep test/dev/xvdf1 /test ext4 rw,seclabel,relatime,data=ordered 0 0

Mount Options

To specify mount options, use the –o flag followed by a comma-separated string of options. The following are some of the available options for the mount command:

  • auto: Allows the file system to be mounted automatically by using the mount –a command
  • loop: Mounts the image as a loop device
  • noauto: Disallows the automatic mount of the file system by using the mount –a command
  • noexec: Disallows the execution of binary files on the file system
  • nouser: Disallows an ordinary user (other than root) to mount and unmount the file system
  • remount: Remounts the file system in case it is already mounted
  • ro: Mounts the file system for reading only
  • rw: Mounts the file system for both reading and writing
  • user: Allows an ordinary user (other than root) to mount and unmount the file system

For example, to mount the /dev/xvdf1 partition on the /test mount point as read-only with only the root user able to mount and unmount the file system, enter:

# mount –o nouser,ro /dev/xvdf1 /test

To mount an ISO image by using the loop device (assuming that the ISO image is present in the current directory and the mount point exist), enter:

# mount -o ro,loop rhel7-x86_64-dvd.iso /media/cdrom

Journaling Mount Options

The ext3 and ext4 file systems have three journaling levels that can be set with the -o option in the mount command or in the options section of /etc/fstab:

  • data=journal: The highest level. The one that does the most journaling. This writes the journal entries for all the data and metadata changes. All data is committed into the journal before being written into the main file system.
  • data=ordered: The default mode. All data is forced directly out to the main file system before its metadata is committed to the journal.
  • data=writeback: The lowest level. Data ordering is not preserved. Data can be written into the main file system after its metadata has been committed to the journal.

Unmounting File Systems

To unmount a file system, use the umount command. The partition name, the device name, or the mount point is used as an argument. Example:

# umount /dev/xvdd1 # umount /test

/etc/fstab File

The /etc/fstab file is called the file system mount table and contains all the information that the mount command needs to mount devices. When adding a new file system, create the appropriate entry in /etc/fstab to ensure that the file system is mounted at boot time. The following is an example of entries in the /etc/fstab file:

# cat /etc/fstab LABEL=centos_root/ ext4 defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0tmpfs /dev/shm tmpfs defaults 0 0proc /proc proc defaults 0 0sysfs /sys sysfs defaults 0 0UUID=687eb83f-c16f-4fa9-bb49-1621eed3a35d /test ext4 defaults 0 0

The first column is the device to mount. The UUID or the label name should be used in place of the device name, because device names could change. The second column is the mount point, except the swap partition entry. The third column is the file system type. The fourth column specifies mount options. The fifth column is used by the dump command. The number 1 means to dump the file system and 0 means the file system does not need to be dumped. The last column is used by the fsck program to determine the order in which file system checks are done at reboot time. The root file system should be specified with a value of 1 and the other file systems should have a value of 2. A value of 0 does not check the file system.


How do I create a filesystem and mount it in Linux? ›

To create a filesystem, there are three steps:
  1. Create partitions using fdisk or Disk Utility. ...
  2. Format the partitions using mkfs or Disk Utility.
  3. Mount the partitions using the mount command or automate it using the /etc/fstab file.
Jan 24, 2022

How to create file system in Linux command? ›

A Linux File system is an integrated aggregation of files on a disk drive or partition. The command you should use to create Linux file systems on a particular location, that is, hard-disk or a device is mkfs.

How do I create a mount folder in Linux? ›

A mount point is "just" a directory. So all you need to do is to create a directory with "mkdir /mymount", or use an existing one (but be aware that if you do mount over an existing directory, anything "below" that directory will be hidden -- although that may well be the effect you're trying to achieve).

How to create filesystem in RHEL 8? ›

  1. Verify Linux configuration.
  2. Configure IP addresses using DHCP.
  3. Install SANtricity Storage Manager for SMcli.
  4. Access SANtricity System Manager and use the Setup wizard.
  5. Determine host port GUIDs and make the recommended settings.
  6. Configure subnet manager.
  7. Set up NVMe over InfiniBand on the host side.
Sep 26, 2022

How do I format and mount a partition in Linux? ›

How To Partition and Format Storage Devices in Linux
  1. Introduction.
  2. Install the Tools.
  3. Identify the New Disk on the System.
  4. Partition the New Drive.
  5. Create a Filesystem on the New Partition.
  6. Mount the New Filesystem.
  7. Conclusion.

What is the mount command in Linux? ›

The mount command instructs the operating system to make a file system available for use at a specified location (the mount point). The mount command mounts a file system that is expressed as a directory by using the Node:Directory parameter on the directory specified by the Directory parameter.

How to create file in Linux with example? ›

Open your terminal window and run the following command:
  1. Command: $ file abc.
  2. Command: $ touch abc.txt.
  3. Command: $ cat > abcFile.txt.
  4. Command: $ > abcFile.txt.
  5. Command: $ echo "This is the File name file1"
  6. Command: $ echo "This is the File name file3" > file3.txt.
Feb 16, 2023

Which command is used to create filesystem? ›

The mkfs command makes a new file system on a specified device. The mkfs command initializes the volume label, file system label, and startup block.

How to mount filesystem on Linux? ›

Mounting a file system on Linux is generally a straightforward two-step process: create a mount point directory, and use the mount command to mount the device at the mount point. Unless the file system is in use, unmounting is even simpler, requiring only the umount command.

What is mount in Linux with example? ›

mount allows the use of --all to remount all already mounted filesystems which match a specified filter (-O and -t). For example: mount --all -o remount,ro -t vfat remounts all already mounted vfat filesystems in read-only mode. Each of the filesystems is remounted by mount -o remount,ro /dir semantic.

How do I create a mount folder? ›

Mount a drive as a folder with Disk Management

Go to Action > All Tasks > Change Drive Letter and Paths, then choose Add. Select Mount in the following empty NTFS folder option. Select the Browse button to locate the folder. After you select the folder, choose select OK.

How to create filesystem in LVM? ›

How to Create LVM Partition in Linux (Step by Step)
  1. Step 1) Identity Disk and Create Physical Volume (PV)
  2. Step 2) Create Volume Group (VG)
  3. Step 3) Create Logical volume (LV) from Volume Group (VG)
  4. Step 4) Format LVM partition.
Dec 16, 2021

What is filesystem in Linux? ›

A file system is a method for storing and organizing data on a computer. In Linux, the file system is a crucial component of the operating system, responsible for managing data stored on disk and other storage devices.

How to create Ext4 filesystem in Linux step by step? ›

How to Create a New Ext4 File System in Linux?
  1. $ ls -1 /sbin/mkfs*
  2. $ sudo fdisk -l [sudo] password for ubuntu:
  3. $ sudo fdisk /dev/sda Command (m for help): l.
  4. sudo mkfs. ext4 /dev/sda5.
Feb 25, 2020

How do I format a mounted filesystem in Linux? ›

Linux Format Disk ext4 File System
  1. Use the command sudo mkfs -t ext4 /dev/sdb to format your disk partition.
  2. Once done with it, you must verify the file system change. ...
  3. Once complete, the terminal will list blocked devices. ...
  4. Confirm the formatting of the preferred partition.
Jul 20, 2023

How to create filesystem in Linux LVM? ›

How to Create LVM Partition in Linux (Step by Step)
  1. Step 1) Identity Disk and Create Physical Volume (PV)
  2. Step 2) Create Volume Group (VG)
  3. Step 3) Create Logical volume (LV) from Volume Group (VG)
  4. Step 4) Format LVM partition.
Dec 16, 2021

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated: 30/12/2023

Views: 6194

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.