已设置FSCKFIX=yes,Ubuntu仍未自动执行fsck,如何实现遇错自动强制fsck -fy?
fsck -fy on Ubuntu When Filesystem Errors Occur Got it, let's work through getting Ubuntu to automatically run fsck -fy whenever it hits that "UNEXPECTED INCONSISTENCY" error—even when your existing FSCKFIX=yes setting didn't kick in.
First, let's clarify why your initial FSCKFIX=yes in /etc/default/rcS might not have handled the issue: that setting only triggers automatic repairs for minor filesystem inconsistencies. When damage is more severe, the system defaults to pausing for manual intervention. Since your server is physically hard to access and you've got full cloud backups, we can tweak the config to skip that pause and force automatic fixes.
Step 1: Adjust SysVinit Config (For Older Ubuntu Versions)
If your system uses the traditional SysVinit system (you're editing /etc/default/rcS, so this applies to you), update the file with these critical settings:
sudo nano /etc/default/rcS
Make sure these lines are set correctly:
FSCKFIX=yes SULOGIN=no
SULOGIN=nois the key addition here: by default, even withFSCKFIX=yes, the system will drop into a single-user login prompt for severe errors. Disabling this tells it to skip that prompt and proceed with automatic repairs.
Save the file (Ctrl+O, press Enter, then Ctrl+X to exit nano).
Step 2: Tweak systemd Settings (For Ubuntu 16.04+ Using Systemd)
If your server runs a newer Ubuntu version with systemd (most modern installs), you'll need to override the default fsck service behavior:
Create an override directory for the fsck service if it doesn't exist:
sudo mkdir -p /etc/systemd/system/fsck@.service.dCreate an override config file:
sudo nano /etc/systemd/system/fsck@.service.d/override.confAdd these lines to force
fsck -fyand disable timeouts:[Service] ExecStart= ExecStart=/sbin/fsck -fy %I TimeoutSec=0- The empty
ExecStart=clears the default command, then we replace it withfsck -fy %I(where%Itargets the specific filesystem being checked). TimeoutSec=0prevents systemd from killing the fsck process if it takes longer than the default timeout.
- The empty
Reload systemd to apply the changes:
sudo systemctl daemon-reload
Step 3: Verify /etc/fstab Settings
Double-check your /etc/fstab to ensure the filesystem in question ( /dev/sda1 in your case) is configured to be checked on boot:
- Look for the line for
/dev/sda1; the 6th column (fs_passno) should be set to1(for root filesystems) or2(for non-root filesystems). This tells the system to include it in the boot-time fsck sequence. - Avoid
ro(read-only) ornoautooptions unless you specifically need them, as they can block automatic checks and repairs.
Important Reminder
You already know this, but it bears repeating: using fsck -fy can lead to data loss if the filesystem is severely corrupted. Since you've got full cloud backups, this risk is mitigated, but it's still good practice to monitor your filesystem health periodically to catch issues before they require forced repairs.
内容的提问来源于stack exchange,提问作者Roman Gaufman

