[Linux] Detecting and Repairing Password File Inconsistencies with the pwck Command

目次

Overview

The pwck command is an administrative tool designed to verify the integrity of the system account files, /etc/passwd and /etc/shadow. It validates various aspects of these files, such as ensuring the correct number of fields per line, checking for duplicate usernames, and verifying the existence of home directories. If inconsistencies are detected, the command allows for interactive repairs (such as deleting invalid lines), making it essential for preventing and resolving system account issues.

Specifications (Arguments and Options)

Syntax

BASH

pwck [options] [passwd_file] [shadow_file]

Note: If file names are omitted, the command defaults to checking /etc/passwd and /etc/shadow.

Main Options

OptionDescription
-r, –read-onlyExecutes in read-only mode. Displays check results without performing any modifications.
-s, –sortSorts the entries in the file by UID (User ID) and saves the result.
-q, –quietDisplays only critical errors and suppresses warning-level messages (such as missing home directories).

Basic Usage

It is recommended to first execute the command in read-only mode to identify existing issues without modifying the files.

BASH

# Verify password file integrity (No changes made)
sudo pwck -r

Example Output

user 'mori': directory '/home/mori' does not exist
pwck: no changes

In this example, the system warns that the home directory for the user ‘mori’ does not exist.

Practical Commands

Detecting and Fixing Inconsistencies Interactively

This workflow allows for the actual repair of file inconsistencies. When the command is executed, the system prompts for confirmation before correcting any detected issues.

BASH

# 1. Check the end of the current file (verify if any erroneous lines exist)
tail -n 4 /etc/passwd

# 2. Execute check and repair
sudo pwck

Example Output

user 'testuser': directory '/home/testuser' does not exist
delete user 'testuser'? y
pwck: the files have been updated

Checking Integrity with Explicit Shadow File Specification

While /etc/shadow is usually checked automatically, files can be explicitly specified to ensure integrity verification across both databases.

BASH

# Explicitly check the password and shadow files
sudo pwck /etc/passwd /etc/shadow

Important Notes

Importance of Backups

When y is selected in interactive mode, pwck performs destructive changes, such as deleting corresponding lines from the files. Incorrect operations may result in the inability to log in. Creating backups of /etc/passwd and /etc/shadow before execution is mandatory.

File Locking

The pwck command locks the target files during execution. Errors may occur if other user management commands are running simultaneously.

Summary

The pwck command serves as an “inspection tool” for repairing account information damaged by manual editing or system failures. While not intended for daily use, it proves useful when diagnosing login failures or performing server health checks. The recommended operational procedure is to verify safety using the -r option first, and then proceed with the interactive mode only when modifications are strictly necessary.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次