Home / Databases / SQL Server

DatabasesRunbookAdvanced

SQL Server 'Cannot generate SSPI context' and linked servers failing as ANONYMOUS LOGON: a Kerberos SPN and delegation runbook

Windows logins fail with "Cannot generate SSPI context" or linked servers fail as NT AUTHORITY\ANONYMOUS LOGON. Check the auth scheme, fix SPNs, then configure delegation.

Deepak Bhardwaj8 min read

Applies toSQL Server 2016–2025 on Windows in an Active Directory domain; Windows Server 2016 and later domain controllers

Symptoms

You will see one of these, often right after a service account change, a server rename, an AG listener build or a new linked server:

The target principal name is incorrect. Cannot generate SSPI context. (Microsoft SQL Server, Error: 0)
Msg 18456, Level 14, State 1
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

The first means the client asked Active Directory for a Kerberos ticket for the SQL Server service, got one, and the server could not decrypt it. That almost always means the Service Principal Name (SPN) is registered on the wrong account or registered twice.

The second is the double-hop problem. A user connects to server A, a linked server query on A connects to server B as that user, and A has no way to pass the user's identity on. Without Kerberos delegation, B sees an anonymous login.

Step 1: find out which protocol the connection is using

Connect any way you can (SQL login, the server's console, or the DAC) and check your own session and the other Windows sessions:

-- Your session
SELECT auth_scheme, net_transport, local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = @@SPID;

-- All Windows-authenticated sessions
SELECT s.login_name, s.host_name, c.auth_scheme, c.net_transport
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions    AS s ON s.session_id = c.session_id
WHERE c.auth_scheme <> 'SQL'
ORDER BY c.auth_scheme, s.login_name;
auth_scheme Meaning
KERBEROS SPN is correct for this connection path; delegation is possible
NTLM Client fell back to NTLM; logins work but delegation (double hop) will not
SQL SQL authentication; not relevant to this runbook

Local connections over shared memory normally show NTLM. Test from a remote client over TCP before drawing conclusions.

Step 2: check what SQL Server tried to register at startup

The SQL Server service tries to register its own SPNs when it starts and logs the result:

EXEC sys.xp_readerrorlog 0, 1, N'Service Principal Name';

You will see either "successfully registered the Service Principal Name" or "could not register the Service Principal Name (SPN) [ MSSQLSvc/... ]" with a Windows return code. A failure means the service account lacks permission to write its own servicePrincipalName attribute. That is common and fine if SPNs are managed by hand, but then someone has to create them.

Note the exact SPN strings in the message. They show the names and ports SQL Server expects.

Step 3: list and check SPNs

Run these from a domain-joined machine with the AD tools (setspn ships with Windows Server and RSAT). Replace the names with yours.

# Which account runs SQL Server?
Get-CimInstance Win32_Service -Filter "Name LIKE 'MSSQL%'" |
    Select-Object Name, StartName, State

# SPNs on the service account
setspn -L CONTOSO\svc_sqlprod

# Who owns a specific SPN? (searches the forest)
setspn -Q MSSQLSvc/sql01.contoso.com:1433

# Duplicate SPNs anywhere in the forest
setspn -X -F

For a TCP connection, the client builds the SPN from the server name it resolved and the port:

Connection SPN required
Default instance, port 1433 MSSQLSvc/sql01.contoso.com:1433 and MSSQLSvc/sql01.contoso.com
Named instance on a static port MSSQLSvc/sql01.contoso.com:50123
Named pipes to a named instance MSSQLSvc/sql01.contoso.com:INSTANCE01
Availability Group listener MSSQLSvc/aglistener.contoso.com:1433 on the service account used by every replica
Failover Cluster Instance MSSQLSvc/<virtual network name FQDN>:<port>

Tip

Give named instances a static TCP port. With dynamic ports the port can change on restart, and a hand-registered SPN with the old port silently drops clients back to NTLM.

Step 4: fix the SPNs

Common causes

Finding Cause Fix
SPN registered on the old service account or the computer account Service account changed; old SPN left behind Delete it from the old account, add it to the new one
setspn -X reports a duplicate Same SPN on two accounts Delete the wrong one; the KDC refuses duplicates or picks the wrong key
No SPN for the listener or alias name AG listener or DNS alias added later Register an SPN for the name clients actually connect with
SPN has the wrong port Named instance moved to another port Register the current port; remove the stale entry
Everything correct, still failing Cached tickets on the client, clock skew above the domain's tolerance (5 minutes by default), or a broken machine trust klist purge, check time sync, Test-ComputerSecureChannel

Move or create an SPN

# Remove from the wrong account
setspn -D MSSQLSvc/sql01.contoso.com:1433 CONTOSO\SQL01$
setspn -D MSSQLSvc/sql01.contoso.com      CONTOSO\SQL01$

# Add to the service account (-S checks for duplicates first)
setspn -S MSSQLSvc/sql01.contoso.com:1433 CONTOSO\svc_sqlprod
setspn -S MSSQLSvc/sql01.contoso.com      CONTOSO\svc_sqlprod

Changing SPNs requires rights to write servicePrincipalName on the target account, normally Domain Admins or a delegated AD group. AD replication can take a few minutes before every domain controller sees the change.

Warning

Never delete an SPN until you have confirmed with setspn -Q which account holds it and that the service on that account is not still using it. Removing the SPN of a running instance breaks every Kerberos connection to it and, for delegated linked servers, every double-hop query.

Test from the client

klist purge
klist get MSSQLSvc/sql01.contoso.com:1433

If klist get returns a ticket, reconnect and re-run the auth_scheme query from Step 1. You want KERBEROS. If klist get fails, the SPN does not exist under that exact name; check spelling and port.

Step 5: fix the linked server double hop

Kerberos must work on both hops before delegation can work. Confirm:

  1. The user's session on server A shows auth_scheme = KERBEROS.

  2. Server B has a correct SPN on its service account (Steps 3–4).

  3. The linked server login mapping passes the caller's identity:

    EXEC master.dbo.sp_addlinkedsrvlogin
         @rmtsrvname = N'SQLB',
         @useself    = N'TRUE';
    

Then allow server A's service account to delegate to server B. Pick one model.

Option A: resource-based constrained delegation (set on the back end)

Configured on server B's service account and does not need Domain Admin on server A's account. Requires the ActiveDirectory PowerShell module:

$frontEnd = Get-ADUser svc_sqlA
Set-ADUser -Identity svc_sqlB -PrincipalsAllowedToDelegateToAccount $frontEnd

# Check it
Get-ADUser svc_sqlB -Properties PrincipalsAllowedToDelegateToAccount |
    Select-Object -ExpandProperty PrincipalsAllowedToDelegateToAccount

Use Set-ADServiceAccount instead of Set-ADUser if the account is a gMSA. Note that this replaces the existing list, so include every front-end account that needs access.

Option B: classic constrained delegation (set on the front end)

Set-ADUser svc_sqlA -Add @{
  'msDS-AllowedToDelegateTo' = @(
    'MSSQLSvc/sqlb.contoso.com:1433',
    'MSSQLSvc/sqlb.contoso.com')
}

In Active Directory Users and Computers this is the Delegation tab, "Trust this user for delegation to specified services only". Avoid unconstrained delegation ("Trust this user for delegation to any service"); it lets the account impersonate users to any service in the domain.

Caution

Users who are members of Protected Users, or whose accounts are marked "Account is sensitive and cannot be delegated", will still arrive at server B as ANONYMOUS LOGON. That is by design. Use a mapped remote login (@useself = 'FALSE' with a dedicated low-privilege login) for those cases rather than weakening the account settings.

Apply and test

Restart the SQL Server service on server A so it picks up a new ticket, or wait for its tickets to renew. Then, from a remote client connected with Kerberos:

SELECT * FROM OPENQUERY(SQLB, 'SELECT SUSER_SNAME() AS login_on_b, auth_scheme
                               FROM sys.dm_exec_connections
                               WHERE session_id = @@SPID');

login_on_b should show the user's own Windows login and auth_scheme should show KERBEROS.

Prevent it next time

  • Treat SPNs as part of the build: register them in the same change as the service account, listener or port, and remove them when decommissioning.
  • Either grant the service account permission to manage its own SPNs, or keep a documented list and check it with setspn -L after every service account change.
  • Run setspn -X -F on a schedule and alert on duplicates.
  • Add a monitoring query that counts NTLM connections from remote hosts on each instance; a sudden rise means an SPN broke.
  • Use resource-based constrained delegation for linked servers and record which front-end accounts are allowed on each back end.