Login failed for user 'NT AUTHORITY\ANONYMOUS LOGIN'

I'm facing a very specific issue on my SQL server instance. Recently I had to create a linked server (referencing server B) in my server A, to connect to a database in B through A. Everything worked and works well when I'm in the server A (accessing via remote desktop).

I need to query the server A through an Apache application, it always worked. But when I need to query the server B through the A using Apache (not in remote desktop, where it works), the query fails to complete, so I open SSMS, execute the same query and it returns the following error:

"Login failed for user 'NT AUTHORITY\ANONYMOUS LOGIN'"

What really concerns me is that it's not an authentication error (even though the message says it), because when I access the server A via RD, execute the query and come back to the Apache application, then I am able to query, which makes no sense at all.

It looks like it's an intermittent issue, and sometimes I just can't force it.

This is known as a double-hop issue. You need to make sure you have Kerberos setup correctly and that your connection to SQL Server is using that instead of NTLM.

Or - you can modify the linked server to use a SQL account either by mapping the windows account to a remote SQL account or using a single account for all connections.