Wondering how to use fsck command to check and repair filesystem? We can help you.
Generally, we use it to run a filesystem check as preventive maintenance or when there is an issue with the system.
As part of our Server Management Services, we assist our customers with several fsck queries.
Today, let us see how we can use this command effectively.
How to Use fsck Command to Check and Repair Filesystem
fsck can diagnose when the system fails to boot. In addition, it can diagnose when we get an input/output error when the files on the system become corrupt.
In order to begin, our Support Techs recommend having the following:
- Linux or UNIX-like system
- Access to a terminal or command line
- A user with root permissions to run the tool
Now, let us see what we need to perform before we check and repair the file system.
-
View Mounted Disks and Partitions
To do so, we can use one of the available tools in Linux.
To locate the disk we want to scan, we list the filesystem disks with the df command:
df -h
As a result, we will get a print of data usage on the system and filesystems.
Here, we can make note of the disk to check with the fsck command.
Then to view partitions for the first disk, for example, we use:
sudo parted /dev/sda 'print'
Linux refers to the first SCSI disk as sda, the second would be sdb, and so on.
We get the result respective to the number of partitions on the virtual machine.
-
Unmount the Disk
Prior to the disk check with fsck, we unmount the disk or partition.
Failure of which will result in a warning.
sudo umount /dev/sdb
We replace /dev/sdb with the device we want to unmount.
-
Run fsck to Check for Errors
Once done, we can run fsck. To check the second disk, we enter:
sudo fsck /dev/sdb
This will show output for a clean disk. In case of multiple issues on the disk, a prompt appears for each one where we have to confirm the action.
The exit code the fsck utility returns is the sum of these states.
-
Mount the Disk
To use the device again we have to mount the disk once we finish checking and repairing it.
In this case, we remount the sdb disk:
mount /dev/sdb
-
Do a Dry Run with fsck
Prior to a live check, we perform a test run with fsck:
sudo fsck -N /dev/sdb
The output will show what would happen but does not perform any actions.
-
Fix Detected Errors Automatically with fsck
In order to fix potential problems without getting any prompts, we pass the -y option to fsck.
sudo fsck -y /dev/sdb
This means we say “yes, try to fix all detected errors” without a prompt every time.
-
Skip Repair but Print fsck Errors in the Output
With the -n option, we can check potential errors on a file system without repairing them.
Suppose, we have a second drive with some journaling errors. The -n flag will print the error without fixing it:
sudo fsck -n /dev/sdb
-
Force fsck to Do a Filesystem Check
When we perform fsck on a clean device, the tool skips the filesystem check.
To force the filesystem check, we can use the -f option.
For example,
sudo fsck -f /dev/sdb
This will scan all five checks to search for corruption even when it seems there are no issues.
-
Run fsck on All Filesystems at Once
Similarly, to perform a check on all filesystems with fsck in one go, we pass the -A flag. It will go through the etc/fstab file in one run.
Since we can’t unmount the root filesystems on a running machine, we add the -R option to skip them:
fsck -AR
-
Skip fsck on a Specific Filesystem
Then, to skip checking a filesystem, we add -t and “no” before a filesystem.
For example, to skip ext3 filesystem, we run:
sudo fsck -AR -t noext3 -y
Here, we add -y to skip the prompts.
-
Skip Fsck on Mounted Filesystems
We need to ensure that we do not run fsck on a mounted filesystem. To do so, we add the -M option.
Here, we will run fsck on sdb while it is mounted, and then when we unmount it.
sudo fsck -M /dev/sdb
While sdb mounts, the tool exits without running a check.
Then, we unmount sdb and run the same command again. This time, fsck reports it as clean, or with errors.
-
Run fsck on Linux Root Partition
As we know now, fsck cannot check root partitions on a running machine.
However, we can check even Linux root partitions if we boot into recovery mode and run the fsck check.
1. To do so, we power on or reboot the machine through the GUI or via the terminal:
sudo reboot
2. Then we press and hold the shift key during boot-up. We can now see the GNU GRUB menu.
3. Here, we can select the Advanced options for Ubuntu.
4. After that, we select the entry with (recovery mode) at the end.
We need to let the system load into the Recovery Menu.
5. Later, we select fsck from the menu.
6. In the prompt, we confirm by selecting <Yes>.
7. Finally, we select resume at the recovery menu to boot up the machine.
[Need help with any of the above steps? We are here for you]
Conclusion
In short, we saw how our Support Techs use the fsck command to check and repair filesystem.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments