Starting with SQL Server 2008, the local Administrators group is no longer added by default during SQL Server setup; you even have to use a manual step to add the current user as a local administrator. This means that it is possible, especially if you don't use mixed authentication (or have forgotten the sa password), that you can be completely locked out of your own SQL Server instance. I've seen cases where an employee has moved on, but their Windows account, being the only one with Administrator privileges for SQL Server, had been completely obliterated from the system. Of course that person was the only one who knew the sa password as well, and being a local admin or even a domain admin might not help you.
The typical workaround I have seen employed is to restart SQL Server in single user mode. However, this approach requires at least some downtime; in some systems, this would be unacceptable. And depending on what needs to be managed on the server, it might not be feasible to wait for a scheduled maintenance window.
A more extreme workaround is to shut down SQL Server, copy all of the MDF/LDF files, install a new instance of SQL Server, and attach all of the user databases. In addition to downtime, the problem here is that you must also re-create all of the logins, linked servers, jobs, and other elements outside of the database in order to have a fully functioning system again. Plus, unless you uninstall the old instance first, the new instance will have a new instance name, and therefore all client applications will need to be updated.
Thanks to Mark Russinovich of Sysinternals fame, there is a very painless way to solve this problem without any downtime: PsExec. While it wasn't one of its primary design goals, PsExec allows you to run programs as the NT AUTHORITY\SYSTEM account, which - unlike "regular" Administrator accounts - has inherent access to SQL Server.
The process to get back up and running is quite simple. Log in to the server where SQL Server is running, as an account with local Administrator privileges. Download and extract PsExec.exe. Start an elevated command prompt (Shift + Right-click, "Run as Administrator"). Run the following command, adjusting for your actual path to Management Studio, which may be different:
This command tells PsExec to run SSMS interactively (-i) and as the system account (-s).
You will get an error message if you are not an Administrator. You will need to agree to the license terms in order to proceed. When Management Studio launches, it will prompt you to connect to a server. You will notice that the authentication mode is Windows Authentication, and the username is hard-coded as NT AUTHORITY\SYSTEM:
Once you connect, in Object Explorer, you will see that you are connected to the server as NT AUTHORITY\SYSTEM:
Now, you can go in to Security > Logins and add your account as a sysadmin, add other admin accounts, update the sa password, and do anything else you need to do to make your instance manageable. As you can see, I was able to connect in this way to both SQL Server 2008 and SQL Server 2012 instances from an instance of Management Studio 2012. I also confirmed that this process works when connecting to a SQL Server 2008 instance using the 2008 version of SSMS. In both cases, I was logged in to Windows as a local administrator, but the account had no explicit access to either SQL Server instance.
You can connect to additional instances on the local server using the Connect > Database Engine dropdown in Object Explorer, or by right-clicking a query window and choosing Connection > Change Connection.
You must be a local Administrator to masquerade as NT AUTHORITY\SYSTEM.
You may need to disable UAC.
You may also be able to do this remotely, but since I work primarily in virtual machines, I did not test this.
Leave the command prompt running in the background until you're done with all of your changes - if you inadvertently Ctrl+C from within the command prompt, SSMS will vanish.
It is quite common to get locked out of a SQL Server instance, and having been there, I know it is quite frustrating. PsExec can get you into your systems without a lot of the headache that can be caused by more brute force methods. You should download PsExec and keep it handy; it can prevent you from going to extreme measures, and having it already available can help in cases where the SQL Server machine you're trying to recover does not have ready access to the web.
Download PsExec and make sure it is readily available to all of your SQL Server instances.
Test the method above and confirm that you are able to use the tool to elevate privileges and gain access to SQL Server even if your Windows account has no such privileges.
You're right, I could use the domain service account although I just looked and that isn't provisioned as a sysadmin either (I just remembered that we had tried this approach but it had slipped my mind when I commented here). This is SQL2012 SP2 but I don't believe that applying the SP has changed any of this configuration.
Luckily in my case this is a new instance that had been setup by someone who is now on leave and hadn't provisioned any other accounts into the sysadmin role. I used the alternative approach to stop and start SQL using my local administrator account which allowed me to connect and create my own sysadmin login.
It would be interesting to know under what conditions the NT AUTHORITY\SYSTEM is and isn't added as a sysadmin. Something for me to investigate.
Wednesday, March 11, 2015 - 9:39:57 AM - Aaron Bertrand
Hi Jason, yes, that's possible, I haven't tested explicitly (though I typically do use my own domain account for the service on VMs, so it should have failed in at least one test).
If the service is running as a domain account then surely you can get that user to help; if that user has moved on (and IT can't fix the issue by resetting the AD password, and another domain admin account proves unsuccessful), you may have to resort to installing a new instance of SQL Server and moving your stuff (and learning to use a non-domain account for the service in the future, or at least a generic and dedicated account which can have its password changed easily).
Wednesday, March 11, 2015 - 7:45:21 AM - Jason Coombes
Thank you for this very useful tip and I'll certainly book mark this for future reference.
I have one question which is to ask what account is running the instance of SQL 2012 for this to work? I suspect that it's running under the NT AUTHORITY\SYSTEM which is why it is provisioned as a sysadmin by default. I expect those posting saying it doesn't work have installed SQL 2012 as a specific service account which means NT AUTHORITY\SYSTEM isn't provisioned as a sysadmin.
I'm basing this on a SQL2012 installation that uses a domain account for the SQL Server service account and NT AUTHORITY\SYSTEM isn't provisioned as a sysadmin.
Either way I'd be interested in a response just to inform myself and others for future reference.
Sorry, I had a bitter part of the solution when I implemented it with PSExec with Administrator rights.
Once I break into MSSQL with "NT AUTHORITY\SYSTEM", I can see the instance as a normal user only and not with admin rights (But before without your idea, I had no clue what's running in side) at instance level.
Once I try to change sa password or provide sa rights to "NT AUTHORITY\SYSTEM" account it failed with "Msg 15151, Level 16, State 1, Line 2 Cannot alter the login ‘sa’, because it does not exist or you do not have permission".
nothing much was possible for me when I did with the actual default Administrator account of windows server 2008R2 running with MSSQL 2012 enterprise edition
Thursday, January 15, 2015 - 10:18:26 AM - dale withroder
Dominique, this is not a permissions problem at all, it's certainly one of these things:
1. You've used an instance name that is incorrect, an instance name when you're trying to connect to the default instance, or you left out the instance name when you need it. Also check you've used .\instance and not ./instance.
2. The service is not running.
There are other potential causes (Google your error message for literally millions of hits), but most of these won't be relevant in most cases when you are connecting to the local machine.
Monday, December 22, 2014 - 3:55:59 PM - Dominique
I tried the psexec command and it will open the SSMS screen but when trying to connect to the instance with NT AUTHORITY\SYSTEm Windows Authentication it is giving me an error:
TITLE: Connect to Server
Cannot connect to VOPVMREPORT1\VEEAMSQL2008R2.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (Microsoft SQL Server, Error: -1)
The database is 2008 R2...
Thursday, December 11, 2014 - 12:49:27 PM - Aaron Bertrand
I don't have this problem....yet. When I run Brent Ozar's sp_Blitz, it notes that NT AUTHORITY\SYSTEM and NT AUTHORITY\NETWORK SERVICE are SA users. Is it ok that they are? I and sa are the only other SA users. I could see removing NT AUTHORITY\SYSTEM and NT AUTHORITY\NETWORK SERVICE from the list of sa's, but then I might run into the problem above. If I should remove them from sa privileges how would I do that?
just a tip that worked for me on Server 2008 R2 and should work for anyone else too, holding shift and right clicking the SSMS.exe program should give you the option to 'run as another user'. Simply put in the local admin account and password and there is no need for extra steps involving PSEXEC.exe.
Thursday, September 18, 2014 - 10:45:18 AM - Theron Knapp
*** NOTE *** - If you want to include code from SQL Server Management Studio (SSMS) in your post, please copy the code from SSMS and paste the code into a text editor like NotePad before copying the code below to remove the SSMS formatt
no se ingles, me funciono correctamente, muchisimas gracias!!
What an AWESOME Christmas present. I had emabrrasingly disabled the group that I connected with and as it was the ONLY sysadmin group oops..
I tried and tried to log in as single user but this was impossible in a cluster with a plethora of monitoring gear and I was running out of time.
Please take to your Christmas the knowledge that you made at least one DBA VERY HAPPY indeed. I will donate $20 to the nearest poor person I see - there's a few around.
Whilst there may be security issues - if run from the server where one has adminsitrator permission there is lttle risk or you wouldn't have such permissions (generally). Again, All the best to you AAron.
Wednesday, October 23, 2013 - 10:40:10 AM - Steven
Before finding this trick, I have resetted my forgotten sa password using the utility - SQL Server Password Changer. Your trick seems to be more awful and I'll also give it a try with my database later.
Friday, October 18, 2013 - 7:15:14 AM - Randy Ansems
Prashant, it doesn't really matter. What it means is if NT AUTHORITY\SYSTEM is not on your system, or is not in the sysadmin role, then yes, you are absolutely correct, this method won't work. If you want it to work, then add NT AUTHORITY\SYSTEM. If you don't want it to work anyway, there's nothing to do.
Tuesday, April 30, 2013 - 9:08:23 AM - Prashant Thakwani
Yes, I checked with all of my SQL Server 2012 instances (around 8) and none of them is provisioned using NT AUTHORITY\SYSTEM, which is inline with what the Microsoft documentation Says. I am still wondering, how it comes for you and Greg (though with different roles, Sysadmin for you and Public for Greg).
I will try to Provision SQL Server 2012 on one for the fresh Windows 2008R2 server and will update here if getting the different result than earlier.
Wednesday, April 24, 2013 - 3:03:40 PM - Aaron Bertrand
Greg ok, in any case, like I said, you can either add them to the sysadmin role if they're not already and in the future you may want to use this technique, or know that it won't work if you leave it as is.
Wednesday, April 24, 2013 - 2:50:33 PM - Greg Robidoux
Hi Aaron, I just checked a couple of my servers and the NT AUTHORITY\SYSTEM is in the PUBLIC server role. There are a bunch of NT SERVICE \ logins that have the sysadmin role. Not sure if this a change or not. Both instances I looked at are running 11.0.2100.
Wednesday, April 24, 2013 - 2:34:10 PM - Aaron Bertrand
That said, if you have an installation where NT AUTHORITY\SYSTEM is not in the sysadmin role (whether it has been removed or SQL Server didn't put it there in the first place), then yes, you are out of luck. You'll be able to connect (assuming the login hasn't been removed altogether), but you won't be able to set the password for existing logins, create new logins, etc.
Wednesday, April 24, 2013 - 2:25:46 PM - Aaron Bertrand
Prashant that may be what the documentation states, but every single SQL Server 2012 instance I can currently access has NT AUTHORITY\SYSTEM in the sysadmin role. I did not modify this and these are not upgrades; it is how SQL Server 2012 was installed. Do you have any instances where you have not made a change and this is not the case?
Wednesday, April 24, 2013 - 1:25:29 PM - Prashant Thakwani
Prashant, this doesn't happen by default (unless you're confusing NT AUTHORITY\SYSTEM with Builtin\Administrators).
If you *manually* delete NT AUTHORITY\SYSTEM, *and* haven't added yourself and/or a domain admin as an administrator account, *and* lost the sa password, that seems like a perfect storm to me, and I can't say I have a whole lot of sympathy in that case. Shut down the service, install a new instance somewhere, and restore your latest backups. If you don't have recent backups available, then I have even less sympathy, as even more preventable things have gone horribly wrong. Grab the mdf/ldf files and try to attach them to the new instance.
Tuesday, April 23, 2013 - 10:01:57 PM - Prashant Thakwani
I was able to use this process on a Windows 2008 R2 server running SQL Server 2008 R2 where the SQL Server services were running as Local System. When I tried this on another server running SQL Server 2012 where the services were running as a local user, I was not able to add a user. After it failed, I checked and found that the SQL Server installation had only granted the public server role to the NT Authority/SYSTEM login. I don't have a SQL Server 2008 R2 system handy where the services are not running as Local System.
Tuesday, September 04, 2012 - 1:58:15 PM - TimothyAWiseman
@K Brian Kelly You have a good point; I can see how this could be considered a security issue. With that said, I don't really expect locking my front door to keep someone out if I gave them the key to my garage door.
As Aaron said, Admin on the machine is essentially the keys to the castle. You can put some limitations on an admin, especially through use of cryptography, but they can legitimately override most restrictions and can circumvent many other restrictions if they are willing to be more devious (an admin can likely put in place a key logger to eventually get those encryption keys for instance).
Friday, August 31, 2012 - 4:21:07 PM - Peter Szegedi
Peter, note Argenis' caveat: you can only do this if there's a currently established session with the sa login. No session, no password. Basically, the password for an SQL Server-based login is visible in memory on the system if the session is established.
Friday, August 31, 2012 - 3:57:21 PM - Aaron Bertrand
If your client SQL Server is under SQL 2012 you can also (proc)dump the sqlservr.exe process. Then run strings.exe to filter it. Then search in the filtered file for the following string: <instancename>sa (for example if you instance name is TestSQL then you searching for TestSQLsa) Right next to this string you will see the sa password. Watch out the procdump will cause some hanging on the instance while it dumps everything from the memory to the disk.
it is a security issue because it is a bypass of a security control. Of course, you expect such controls to be a deterrent, not 100% effective. From an audit perspective, if the data is that sensitive, you need to impose other controls, like event auditing to see who is logging on and reporting that accordingly.
Friday, August 31, 2012 - 1:09:06 PM - TimothyAWiseman
I am not certain you could call this a security issue. A local admin probably should be able to get into SQL Server, and even if you truly structured it so they couldn't, then that still does not protect the data from them since they can access the files.
You could limit their access to the data through encryption, and there are types of logging that can make it very difficult to make changes without leaving tracks, but a local admin will always be able to cause a vast amount of mischief if they were so inclined and you should never give anyone admin access unless you can trust them.
Thursday, August 30, 2012 - 4:41:18 PM - Aaron Bertrand
Gene, yes, if you give someone local administrator rights, there are many ways they can break into SQL Server, not just the one I showed here. This is just the one that has least impact on a running system. SQL Server tried to make it harder for local IT administrators (but non-SQL administrators) to gain access to SQL Server, but basically a local administrator owns the machine and can take ownership of anything running on it.
The security problem this highlights is a different issue altogether. Keys to the castle, and all that.
Thursday, August 30, 2012 - 12:56:51 PM - Gene Wirchenko
Aaron, thank you for posting this. I have had to do this the hard way in other instances and this may save me quite a bit of time when I run into this again.
I would reiterate the comment about not using the SA account for other programs though. Generally, a policy of least priveleges is good for security, and it is fairly rare that a program needs admin level priveleges to the SQL Server (though it does come up). Even when a program really must connect to the server with its own admin credentials, I would give the program its own account if only to avoid problems when I change the SA password and to make tracking the source of activity a little bit easier.
The AT command is a command-line hold-over from the days before Task Scheduler. It allowed you to schedule processes to run on Windows systems. When Task Scheduler was added to the OS, AT wasn't eliminated. Instead, using AT creates a Task Scheduler task, but running with the security account specified for any tasks coming from the AT command.
If you're in an environment where you can't get psexec onto the system, you can use the AT command and schedule it to start a minute or two after you execute the command. By default, task scheduler tasks that are scheduled using the AT command run as System, though that should normally be changed to prevent the AT command from being exploited in this manner.