A corrupted master database is fixed by rebuilding it or restoring a backup, not by running repair commands against the live copy. Microsoft's documented recovery path is to start SQL Server with the undocumented -m (single-user) switch, restore master from a valid backup, and fall back to Setup's REBUILDMASTER or INSTALLSQLDATA option only when no usable backup exists. Before any of that, you need to confirm master is actually the damaged file rather than a suspect page in a user database or a drive that failed underneath both.
The master database holds server-level metadata: logins, linked servers, endpoint and certificate definitions, server configuration values, and the records that point at every other database file. SQL Server can start without model, msdb, or tempdb and rebuild some of them, but it cannot run without a readable master. That makes "fixing" master less about patching pages and more about restoring known-good metadata and then reconciling what changed since the backup was taken. The rest of this guide walks through triage, prerequisites, three recovery routes in the order you should try them, and the checks that tell you whether you're actually done.
Confirm master is the problem, not a passenger
The first mistake is treating every startup failure as master corruption. Master is damaged when SQL Server refuses to start and the error log names master or model, when a DBCC CHECKDB against master returns consistency errors, or when startup fails with messages about the resource database (mssqlsystemresource.mdf) being inaccessible. Common decoys look similar:
Error 9002 ("log for database is full") on master during startup usually points at disk space or an oversized transaction log, not damaged pages. Error 5123 or 823 (operating system error) on a data file often means a failing disk or permissions problem. Rebuilding master on a dying volume just relocates the outage. A server that starts fine but reports suspect user databases has a different problem; master is healthy enough to serve metadata.
Treat storage-level failures as the parent issue. Check the Windows Event Log for disk, controller, or timeout errors, confirm free space on the drive holding master, and verify that the SQL Server service account still has read access to the data directory. If the hardware or volume is failing, restore the whole instance to new storage instead of patching master in place. Rebuild routes also assume the SQL Server setup binaries and your installation media match the installed build; keep both handy because a mismatched or slipstreamed media set is the most common reason a rebuild fails halfway.
Know what you're protecting before you touch anything. Copy the current master.mdf and mastlog.ldf to a separate location even if they look ruined. A copy preserves evidence for later forensics, and occasionally a copy is recoverable with third-party tools even when the instance can't attach it. Note the exact error text, the SQL Server version and build (for example, 16.0.1000.6 for SQL Server 2022 RTM), and the last known-good backup timestamp. You'll need all three to choose the right route and to report accurately if you escalate.
Prerequisites and backups that matter
Restoring master is not like restoring a user database, and the prerequisites differ by route.
For the restore route, you need a backup of master taken with BACKUP DATABASE master, ideally from the same SQL Server major version. A master backup from the same build is safest: server configuration and some system objects can differ across service packs, and a restore from a mismatched build may fail or leave the instance on inconsistent metadata. You also need a current backup of every user database, because restoring master rewinds logins and database registrations to the backup date. Any user database created after that backup exists as a file but is invisible to the restored instance until you attach or restore it.
For the rebuild routes, you need the SQL Server installation media, local administrator rights on the server, and a maintenance window. Command-line setup requires the instance to be stopped. Also confirm which directory holds the system databases and that it's writable by the service account; Setup creates new files for master, model, msdb, and tempdb as part of the operation.
One conditional value worth knowing: SQL Server 2005 and later keep the Resource database separate from master. If you restore master from an older backup onto the same build, the Resource database is untouched, which is usually fine. A rebuild, however, replaces the system databases entirely, so plan to reapply anything you had configured after the backup: SQL Agent jobs in msdb are gone, and logins created after the backup must be recreated manually.
Finally, plan the login gap. Record the logins that must survive; if the instance hosts hundreds of accounts, export a login list before changes and reconcile afterward. Windows-authenticated logins in Active Directory survive a rebuild because Windows validates them, but their server-level role memberships and stored SIDs must be recreated, which is the most overlooked part of recovery.
Step 1: Try to start in single-user mode and inspect the damage
Before restoring or rebuilding, get SQL Server into a minimal running state if possible. This confirms whether master is readable and lets you capture what still works.
1: Stop the SQL Server service (SQL Server (MSSQLSERVER) or the named instance service) and any dependent services such as SQL Server Agent. 2: Start the instance from the command line with the single-user switch: sqlservr.exe -c -m For a named instance, run the command from that instance's Binn folder, because the default path points at a different install. 3: Watch the console and the error log for startup failures. If the instance starts, connect with sqlcmd -S . -E and run DBCC CHECKDB ('master') WITH NO_INFOMSGS; to see the errors directly. 4: Note the object IDs and messages rather than guessing at page damage; if CHECKDB reports errors in master, Microsoft's guidance is to restore from backup, and in-place repairs to master are not in the standard toolbox.
If the instance will not start even in single-user mode, don't spend hours trying switches. That outcome is the signal to move to restore or rebuild. Expect sqlservr to print messages to console; if it fails before that, the cause usually lies below SQL Server (file access, service account, or Windows-level problems) and needs storage or permission work first. Stop condition: if the error log shows recurring hardware I/O errors or bad pages on the disk rather than just in the file, resolve the storage issue or restore to a new server before continuing. Working around a failing disk on the same machine is how one incident becomes two.
Step 2: Restore master from a backup
The restore route preserves configuration and is the one Microsoft documents for corruption, so try it first if a backup exists. Logins and server settings rewind to the backup point; nothing else changes.
1: Keep the instance in single-user mode (sqlservr -m). A second connection will be rejected, so don't hold one open. 2: Connect with sqlcmd as a member of the sysadmin role, typically the Windows administrator who launched the server. 3: Restore with the path that replaces master and leaves the instance stopped afterward: A plain WITH REPLACE restore of master is understood to leave SQL Server in single-user mode and shut down after completion. If you need the instance to come back online immediately, use WITH REPLACE, NORECOVERY? no; that is for chain restores. The practical check is the RESTORE output: master appears in "The backup set holds a backup of database 'master'" and the restore reports success without a follow-on recovery error. 4: Stop the command-line server by pressing Ctrl+C or closing the console, then start the SQL Server service normally. 5: Reconnect and verify: query the error log, check that all databases appear in sys.databases, and run DBCC CHECKDB ('master') WITH NO_INFOMSGS again.
Common failures after this step are useful diagnostics. If the restore reports the backup was taken on a different version or cannot be applied because master needs to be restored with particular options, check the media version and consider that the backup may be unusable on this build. If startup then logs "log for database master is not available" or a related log file problem, the log file was left behind and older than the restored data; remove or move the stray mastlog.ldf and let SQL Server recreate it. Expect one or two iterations of this; the first restart after a master restore is where most surprises appear.
If no backup exists, or the newest backup is too old to be acceptable, go to Step 3. Do not try to string together partway recoveries; a restore that succeeds but leaves the instance unable to log in is worse than a clean rebuild.
Step 3: Rebuild the system databases with Setup
When no valid master backup exists, Setup's command-line repair option rebuilds master, model, msdb, and Resource for the instance. This is the documented fallback, and it resets those databases to their initial state rather than recovering their contents.
1: Back up any surviving user database files and configuration you can still read from the broken instance, including the values of server configuration options if the error log or an archived output recorded them. 2: Stop the instance and any related services. 3: Run setup from a command prompt using the setup.exe in the SQL Server installation media or the instance's setup folder. The supported option is REBUILDMASTER: Without the quiet flag, Setup prompts for the service account and system database location. Use a full path in the output so you can identify the new files later. If you must restore a master backup instead, the same command accepts SQLCOLLATION; check the Setup help for your version before running it, since the accepted option names have shifted across releases. 4: Confirm success from the Setup summary or the summary log path printed at the end. A successful run creates new system database files and leaves master repaired but otherwise empty of user logins, linked servers, jobs, and other customizations. 5: Start the instance and log in with the account you specified as a sysadmin. Then attach or restore every user database in the order you care about; the instance knows nothing about them until you do.
A second route exists for installations where the binaries themselves are compromised: run Repair from the SQL Server Installation Center, which offers Rebuild Database and Repair Instance options, and follow the wizard to the system database rebuild. That is the GUI mirror of the command line above and is fine when a console isn't convenient; the outcome and the login gap are identical.
What you lose, and how to put it back
Every rebuild resets server-level metadata, and the gaps depend on where master came from. After a restore, everything matches the backup time, so reconstruct anything changed since: logins, permissions, server roles, linked servers, credentials, agent jobs, mail settings, and any custom error messages. After a rebuild, the starting point is a fresh system set, so expect to re-create all of it from scratch or from scripted output.
The practical approach is to build back in this order: run sp_addlogin for SQL-authenticated logins and rebuild Windows logins where they exist in Active Directory, then assign server roles and permissions, then recreate linked servers and credentials, then re-enable protocols or endpoint settings the application depends on, and finally restore Agent jobs if you have them scripted. Capture the state of a healthy instance in advance with sp_helplogins, a SELECT * FROM sys.server_principals, and a script of your server configuration; if you don't have that script, the restore route is dramatically cheaper than the rebuild route. After re-creating the first set of logins, immediately make a fresh master backup; that backup is your next safety net and also fixes the oldest-backup problem on future incidents.
One nuance: master's transaction log cannot be truncated in some configurations because it holds recent server-wide transactions, and a full or oversized mastlog is a recurring but different failure. If errors point to the master log being full rather than damaged pages, check the size limit on mastlog.ldf and the availability of disk space before assuming corruption. If you cannot expand the file because master itself is unreadable, you are back to the restore or rebuild step.
Verify the fix rather than assuming it
Success is not the service starting; it's the instance behaving like a healthy one. Run these checks in order:
| Check | Command or lookup | Pass condition |
|---|---|---|
| System databases healthy | DBCC CHECKDB on master, msdb, model | No errors; run offline-safe checks with WITH NO_INFOMSGS |
| Expected databases present | SELECT name, state_desc FROM sys.databases | Every user database appears, ONLINE |
| Logins restored | sys.server_principals count against your inventory | Every required login and role membership is back |
| Configuration set | sp_configure with show advanced | Documented settings match intended values |
| Agent and service | Start SQL Server Agent | Agent starts; expected jobs exist |
| Application login | Log in with a real application account | Connects through its normal endpoint |
If DBCC CHECKDB on master still reports errors after all this, stop repairing and escalate to Microsoft support or restore to a new host; a master that keeps returning corruption has an underlying storage or media cause that repair cannot address.
A worked estimate of the decision
Suppose the last usable master backup is from 40 days ago and the server has 3,000 SQL logins, 150 Agent jobs, and 20 linked servers. Restoring takes minutes, and the work moves to reconciling those changes: a few hours of login re-creation plus job scripts, if an inventory exists. A rebuild avoids accepting an old server configuration, but it means re-creating the full 3,000 logins, every job, and all links against a blank slate, which puts a day's work into the same scenario. The math favors restore whenever the backup is recent enough that the change gap is manageable; the arithmetic is easier when the required recovery is complete rather than patched, which is the only criterion the SQL Server setup options actually promise. Phase the two paths accordingly: restore first, and rehearse the rebuild on a nonproduction instance once, so that the day it's needed it's a procedure rather than an experiment.
If recovery involves a production instance with customers on it, do the dry run on a restored copy or lab machine and time it. Instances with complex security or numerous linked servers are exactly where rebuild time overruns, and knowing that before you start the maintenance window is worth more than any amount of scratch planning.
FAQ
Can I fix master with DBCC CHECKDB's repair options like other databases? No. Master backup corruption is handled by restore or rebuild; page-level repair against the live master is not the documented path and can't run with the instance offline in the way a user database repair does.
Does a single-user-mode restore also fix msdb and model? No, it restores only master. If msdb is also damaged, restore it from its own backup afterward; otherwise the server can start with context but Agent jobs and related settings depend on msdb's contents. model and tempdb are recreated as needed.
Will rebuilding master recover my user databases? The files survive, but the instance loses track of them. Attach each with CREATE DATABASE ... FOR ATTACH or restore from backup afterward, and be ready for a log file mismatch on databases that weren't cleanly shut down.
What if SQL Server won't start even after a successful rebuild? Check the service account permissions on the new system database folder, confirm the setup summary shows success for all four system databases, and read the error log's first failure line rather than the last. That line names the file or resource that is blocking startup.