Software & AppsOperating SystemLinux

How To Force Automatic Fsck on Bootup in Ubuntu 20.04

Ubuntu 7

In this article, we will delve deep into the process of forcing an automatic file system check (fsck) on bootup in Ubuntu 20.04. This can be a crucial step in system maintenance and troubleshooting, helping to identify and rectify file system issues that may be affecting your Ubuntu machine.

Quick Answer

To force an automatic fsck on bootup in Ubuntu 20.04, you can either change the "Maximum number of mounts" for ext file systems or modify the kernel parameter in the Grub configuration. These methods allow you to initiate a file system check during the boot process, helping to identify and fix any issues that may be affecting your Ubuntu machine.

Understanding Fsck

Fsck, short for file system consistency check, is a utility on Unix and Unix-like operating systems such as Ubuntu. It’s used to check and potentially repair file systems. Running fsck can help you to identify and fix issues related to improper shutdowns, power failures, or system crashes.

Precautions

Before we proceed, it’s important to note that fsck should not be run on a mounted file system to avoid data corruption. It’s also advisable to backup any important data before running fsck, as it might change the file system’s structure.

Method 1: Change “Maximum number of mounts” (only ext file systems)

This method is specific to ext file systems. Here’s how to go about it:

  1. Check the current setting: Use the command sudo tune2fs -l /dev/nvme0n1p2 | grep 'Maximum mount'. This command will display the current maximum mount count. Replace /dev/nvme0n1p2 with your partition.
  2. Change the maximum mount count: To force fsck on next reboot, we can temporarily set the maximum mount count to 1 using the command sudo tune2fs -c 1 /dev/nvme0n1p2. This command tells the system to initiate fsck after every 1 mount.
  3. Reboot the system: Use the command sudo reboot. The file system will be checked during the boot process.
  4. Reset the value: After the reboot and fsck, remember to reset the maximum mount count to its original value to avoid repeated checks. You can do this with the command sudo tune2fs -c X /dev/nvme0n1p2, replacing X with the original value.

Method 2: Change kernel parameter

This method can be used for any file system. It involves modifying the Grub configuration:

  1. Access the Grub menu during boot: When your system is booting, you will need to access the Grub menu. This is usually done by pressing the Shift or Esc key during bootup.
  2. Edit the desired entry: Highlight the entry you want to edit (usually the first one) and press ‘e’ to edit it.
  3. Add the kernel parameter: Move to the line starting with ‘linux’, press ‘End’ to move to the end of the line, add a space, and then add fsck.mode=force. This parameter forces the fsck check during boot.
  4. Close and continue booting: Press ‘Ctrl’ + ‘x’ to close the editor and continue with the booting process. The file system will be checked during boot.

Please note that the old method of creating a file /forcefsck does not work anymore since systemd took over initialization in newer versions of Ubuntu.

Conclusion

Forcing an automatic fsck on bootup in Ubuntu 20.04 can be achieved through the two methods discussed above. It’s an important step in system maintenance that can help resolve file system issues. However, remember to take precautions such as backing up data and not running fsck on mounted file systems to avoid data loss or corruption.

What is the purpose of running fsck on bootup in Ubuntu 20.04?

The purpose of running fsck on bootup is to check and potentially repair file system issues that may be affecting your Ubuntu machine. It helps to identify and fix issues related to improper shutdowns, power failures, or system crashes.

Can fsck be run on a mounted file system?

No, it is not recommended to run fsck on a mounted file system as it can lead to data corruption. It’s important to unmount the file system before running fsck.

How can I backup my important data before running fsck?

To backup your important data, you can copy it to an external storage device or create a backup on a separate partition or drive. It’s always a good practice to have a backup of your data before performing any system maintenance tasks.

How can I check the current maximum mount count for my file system?

You can use the command sudo tune2fs -l /dev/partition | grep 'Maximum mount' to check the current maximum mount count for your file system. Replace /dev/partition with the appropriate partition.

Can I change the maximum mount count permanently?

Yes, you can change the maximum mount count permanently by using the command sudo tune2fs -c X /dev/partition, replacing X with the desired value. This will set the new maximum mount count for your file system.

Is it possible to force fsck on any file system using the kernel parameter method?

Yes, the kernel parameter method can be used to force fsck on any file system. It involves modifying the Grub configuration to include the fsck.mode=force parameter, which will initiate the fsck check during boot.

What should I do if the old method of creating a file `/forcefsck` does not work?

In newer versions of Ubuntu, the old method of creating a file /forcefsck does not work. Instead, you can use the kernel parameter method to force fsck on bootup. Follow the steps mentioned in Method 2 to modify the Grub configuration.

Are there any precautions I should take before running fsck?

Yes, it is advisable to backup any important data before running fsck, as it might change the file system’s structure. Additionally, make sure to not run fsck on a mounted file system to avoid data corruption.

How often should I run fsck on my Ubuntu machine?

The frequency of running fsck depends on your specific system usage and any issues you may be experiencing. It’s generally recommended to run fsck if you encounter file system issues or after a system crash. Regular system maintenance can also include running fsck periodically to ensure the integrity of your file system.

Can I cancel the fsck check during boot if I no longer want to proceed with it?

Yes, you can cancel the fsck check during boot by pressing ‘Ctrl’ + ‘C’. However, it’s important to note that canceling the check without allowing it to complete may leave file system issues unresolved. It’s generally recommended to let the fsck check run to completion for proper maintenance and troubleshooting.

Leave a Comment

Your email address will not be published. Required fields are marked *