Linux tune2fs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the tune2fs command, a powerful tool for managing ext2, ext3, and ext4 file systems. The tune2fs command allows you to modify various file system parameters without the need to unmount the file system. We will first understand the tune2fs command, then learn how to modify the file system behavior using tune2fs, and finally, we will cover how to backup and restore file system metadata with tune2fs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/df -.-> lab-422968{{"`Linux tune2fs Command with Practical Examples`"}} linux/mount -.-> lab-422968{{"`Linux tune2fs Command with Practical Examples`"}} end

Understand the tune2fs Command

In this step, we will explore the tune2fs command, which is a powerful tool for managing ext2, ext3, and ext4 file systems. The tune2fs command allows you to modify various file system parameters without the need to unmount the file system.

First, let's check the current file system information using the tune2fs command:

sudo tune2fs -l /dev/sda1

Example output:

tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name:   <none>
Filesystem UUID:          a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              2621440
Block count:              10485760
Reserved block count:     524288
Free blocks:              8175533
Free inodes:              2612172
First block:              0
Block size:               4096
Fragment size:            4096
Group descriptor size:    64
Reserved GDT blocks:      1022
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Filesystem created:       Fri May 12 11:21:21 2023
Last mount time:          Fri May 12 11:21:21 2023
Last write time:          Fri May 12 11:21:21 2023
Mount count:              1
Maximum mount count:      -1
Last checked:             Fri May 12 11:21:21 2023
Check interval:           0 (<none>)
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:               256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6
Journal backup:           inode blocks

This output provides detailed information about the file system, including its UUID, features, flags, and various other parameters.

Now, let's explore some of the common options that can be used with the tune2fs command:

  • -l: Display the file system information.
  • -c: Set the maximum number of mounts before a file system check is forced.
  • -i: Set the interval between file system checks.
  • -m: Set the percentage of the file system that is reserved for the super-user.
  • -o: Set the default mount options for the file system.
  • -L: Set the volume label of the file system.
  • -U: Set the UUID of the file system.

We will explore some of these options in the next steps.

Modify the Filesystem Behavior Using tune2fs

In this step, we will learn how to modify the behavior of the file system using the tune2fs command.

First, let's set the maximum number of mounts before a file system check is forced. By default, this value is set to -1, which means the file system will never be checked automatically. Let's change it to 30 mounts:

sudo tune2fs -c 30 /dev/sda1

Example output:

tune2fs 1.46.5 (30-Dec-2021)
Setting maximum mount count to 30

Now, let's set the interval between file system checks to 30 days:

sudo tune2fs -i 30d /dev/sda1

Example output:

tune2fs 1.46.5 (30-Dec-2021)
Setting interval between checks to 30 days

Next, let's set the reserved blocks percentage to 2%:

sudo tune2fs -m 2 /dev/sda1

Example output:

tune2fs 1.46.5 (30-Dec-2021)
Setting reserved blocks percentage to 2% (209715)

Finally, let's set the volume label to "my_filesystem":

sudo tune2fs -L my_filesystem /dev/sda1

Example output:

tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name changed to "my_filesystem"

You can verify the changes made to the file system using the tune2fs -l /dev/sda1 command.

Backup and Restore Filesystem Metadata with tune2fs

In this step, we will learn how to backup and restore file system metadata using the tune2fs command.

First, let's create a backup of the file system metadata:

sudo tune2fs -f -j /dev/sda1 backup.txt

This command will create a file named backup.txt in the current directory, containing the backup of the file system metadata.

Now, let's simulate a scenario where we need to restore the file system metadata. For this, we will first corrupt the file system by modifying some of the metadata parameters:

sudo tune2fs -c 10 -i 7d -m 5 /dev/sda1

This command sets the maximum mount count to 10, the file system check interval to 7 days, and the reserved blocks percentage to 5%.

To restore the file system metadata, we can use the backup file we created earlier:

sudo tune2fs -l backup.txt /dev/sda1

This command will restore the file system metadata from the backup.txt file.

You can verify the restored metadata by running the tune2fs -l /dev/sda1 command and comparing the output with the original file system information.

Summary

In this lab, we explored the tune2fs command, a powerful tool for managing ext2, ext3, and ext4 file systems. We learned how to use tune2fs to modify various file system parameters without the need to unmount the file system. We also discovered how to backup and restore file system metadata using tune2fs. By understanding the capabilities of tune2fs, we can effectively manage and maintain the health of our file systems.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like