Linux check root filesystem

Check and Repair Your Filesystem With fsck [Linux]

No matter how well you take care of your computer, sometimes, it will just crash and you often have to pull off the plug to restart the computer. When such crashes happen, there is a chance that your filesystem will get corrupted or damage. It is advisable to do a filesystem check regularly to make sure that it is running properly and free of error.

In Linux (and Mac), there is this powerful command “ fsck ” that you can use to check and repair your filesystem. “Fsck” stands for “File System Consistency checK”.

The usage is very easy. Open a terminal and type:

This will check the sda1 partition.

Note: fsck cannot be used on a mounted partition. If you do so, there is a high chance that it will damage the filesystem.

To check your Home folder that resides on another partition, say sda2, use the following commands:

Note: you will need root/superuser permission to run the “fsck” command.

You can also use “fsck” to check external drive, such as your thumb drive or SD card. For example:

If you are not sure of the partition number, you can use the command

to list out all the partitions in the system.

Advanced Usage

There are a few parameters that you can add to “fsck” to make it more powerful.

Auto repair filesystem when errors are detected

During the filesystem check, if errors are detected, you can get “fsck” to auto repair the filesystem with the -a flag. For example:

Similarly, using the -y flag can get the job done as well:

Check all filesystems in one run

If there are several filesystems in your computer, you can get fsck to check all of them at the same time with the -A flag.

What it will do is to grab all the filesystem entries from /etc/fstab and scan them for errors. You can use it together with the -R and -y flag to prevent it from scanning the root filesystem and fix all errors, if there is any.

Exclude check on mounted filesystem

As mentioned earlier, fsck cannot be run on a mounted filesystem. If you are using the -A flag to scan all the filesystems, and some of them are mounted, you might damage those filesystems. A way to overcome this is to use the -M flag to prevent it from checking mounted system.

For example, running the command

returns nothing and a return code 0 (which means “no error”). No scan was done at all since all the filesystems are mounted.

However, if I unmount the external thumb drive and run the command again, it will then perform the scan and return the output:

Читайте также:  Windows phone от toshiba

Specifying the filesystem type

There are times when you just want to check filesystems of a specific type, say ext2. You can make use of the -t flag to specify the filesystem type to check. For example, the command

will scan the external drive only if it is in the ext4 format. Alternatively, you can combine with the -A flag to scan all filesystems of the specific type:

Force fsck to run everytime during bootup

By default, Ubuntu will run fsck after every 30 bootups, but if you want the system to to do a “fsck” check everytime it boots up, all you need to do is to create an empty file call “forcefsck” and place it in the root folder. This will notify the system to do a “fsck” check everytime during boot up.

To change the checking frequency, you can make use of the command “ tune2fs “.

The following command instructs the system to run “fsck” after every 30 boot up.

You can also specify number of days instead of boot up:

Using fsck with a GUI

As you can see, fsck is mainly a command line tool. If you need a GUI for that, GParted comes with a feature that allows you to check your filesystem.

First, install GParted:

Next, open GParted and select the filesystem you want to check. Right click on it and select “Check”.

Conclusion

fsck is a useful command that can check your filesystem for errors and fix them automatically. Most of the time, you won’t have to bother with it as your system will usually run it regularly during boot up to make sure everything is working fine. However, when your filesystem get corrupted, this is the easiest way to find out what happen and fix it.

Note: fsck is also available in Mac.

Join the elite circle of Linux Power Users with this bundle

Do you have a burning desire to learn how to use Linux? If so, you have to check out the Linux Power User Bundle. Don’t worry about prior experience because these courses will show you how to install Linux servers and utilize proper command line technique like a pro. Open up a world of opportunity by learning Linux in this bundle, available now for just $19.

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.

5 comments

A couple of notes:
To check a root fs that can not be unmounted “online” one can use LVM snapshot of it to check for errors while the system is running and without unmounting.

man fsck does not recommend using -a option but -p instead:
-a Automatically repair the file system without any questions (use this option with caution). Note that e2fsck(8) supports -a for backwards compatibility only. This option is mapped to
e2fsck’s -p option which is safe to use, unlike the -a option that some file system checkers support.

Kind of lame that fsck will allow you to run a command that could damage the filesystem it’s supposed to be diagnosing!

Anybody know if the Mac’s Disk Utility uses fsck for its disk repair operations?

Читайте также:  Windows run dialogue box

Disk Utility works almost the same way as fsck except that it comes with a GUI that makes it easier to work with.

Ok…i think I`ve just kind of destroyed my external HDD.
I was pretty sure that it wasn`t mounted…but it seems that i was wrong. Now I`m stuck with an external HDD which is not accessible neither from Ubuntu nor from Windows…!
Well is there anything that i can do in order to save it or it is to late?

To make sure you have no problems with using fsck with mounted partitions, run it only after rebooting from a LiveCD.

Comments are closed.

RedMagic 6S Pro Review: Gaming Is Serious Business.

How to Boot to Recovery Mode (Safe Mode) in Ubuntu

Ubuntu Software Center Not Working? Here Are the Fixes

How to Stress Test a Graphics Card on Linux

How to Mount a Windows Share Folder on Linux

How to Mount Your iPhone as an External Drive in Ubuntu

How to Fix Ubuntu Freezing in VirtualBox

How to Fix «Repository Does Not Have Release File» Error

How to Reset the Root Password in Linux

How to Combine PDF Files on Windows and Linux

8 Reasons to Switch from Windows to Linux

Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.

Источник

How to Use ‘fsck’ to Repair File System Errors in Linux

Filesystems are responsible for organizing how data is stored and recovered. One way or another, with time, filesystem may become corrupted and certain parts of it may not be accessible. If your filesystem develops such inconsistency it is recommend to verify its integrity.

This can be completed via system utility called fsck (file system consistency check). This check can be done automatically during boot time or ran manually.

In this article, we are going to review the fsck utility and its usage to help you repair disk errors.

When to Use fsck in Linux

There are different scenarios when you will want to run fsck. Here are few examples:

  • The system fails to boot.
  • Files on the system become corrupt (often you may see input/output error).
  • Attached drive (including flash drives/SD cards) is not working as expected.

fsck Available options

Fsck command needs to be run with superuser privileges or root. You can use it with different arguments. Their usage depend on your specific case. Below you will see some of the more important options:

    • -A – Used for checking all filesystems. The list is taken from /etc/fstab .
    • -C – Show progress bar.
    • -l – Locks the device to guarantee no other program will try to use the partition during the check.
    • -M – Do not check mounted filesystems.
    • -N – Only show what would be done – no actual changes are made.
    • -P – If you want to check filesystems in parallel, including root.
    • -R – Do not check root filesystem. This is useful only with ‘ -A ‘.
    • -r – Provide statistics for each device that is being checked.
    • -T – Does not show the title.
    • -t – Exclusively specify the filesystem types to be checked. Types can be comma separated list.
    • -V – Provide description what is being done.

How to Run fsck to Repair Linux File System Errors

In order to run fsck, you will need to ensure that the partition you are going to check is not mounted. For the purpose of this article, I will use my second drive /dev/sdb mounted in /mnt .

Читайте также:  Брандмауэр windows создание правил

Here is what happens if I try to run fsck when the partition is mounted.

Run fsck on Mounted Partition

To avoid this unmount the partition using.

Then fsck can be safely ran with.

Run fsck on Linux Partition

Understanding fsck exit codes

After running fsck, it will return an exit code. These cods can be seen in fsck’s manual by running:

Repair Linux Filesystem Errors

Sometimes more than one error can be found on a filesystem. In such cases you may want fsck to automatically attempt to correct the errors. This can be done with:

The -y flag, automatically “yes” to any prompts from fsck to correct an error.

Similarly, you can ran the same on all filesystems (without root):

How to Run fsck on Linux Root Partition

In some cases, you may need to run fsck on the root partition of your system. Since you cannot run fsck while the partition is mounted, you can try one of these options:

  • Force fsck upon system boot
  • Run fsck in rescue mode

We will review both situations.

Force fsck Upon System Boot

This is relatively easy to complete, the only thing you need to do is create a file called forcefsck in the root partition of your system. Use the following command:

Then you can simply force or schedule a reboot of your system. During the next bootup, the fsck will be performed. If downtime is critical, it is recommended to plan this carefully, since if there are many used inodes on your system, fsck may take some extra time.

After your system boots, check if the file still exists:

If it does, you may want to remove it in order to avoid fsck on every system boot.

Run fsck in Rescue Mode

Running fsck in rescue mode requires few more steps. First prepare your system for reboot. Stop any critical services like MySQL/MariaDB etc and then type.

During the boot, hold down the shift key so that the grub menu is shown. Select the “Advanced options”.

Grub Advance Options

Then choose “Recovery mode”.

Select Linux Recovery Mode

In the next menu select “fsck”.

Select fsck Utility

You will be asked if you wish to have your / filesystem remounted. Select “yes” .

Confirm Root Filesystem

You should see something similar to this.

Running fsck Filesystem Check

You can then resume to normal boot, by selecting “Resume”.

Select Normal Boot

Conclusion

In this tutorial you learned how to use fsck and run consistency checks on different Linux filesystem. If you have any questions about fsck, please do not hesitate to submit them in the comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Оцените статью