Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Scripts          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Enabling xp_cmdshell in SQL Server


By:   |   Last Updated: 2018-06-05   |   Comments (4)   |   Related Tips: More > Security

Problem

Sometimes there may be a need to run some external processing from within SQL Server.  So to make this task easy for both DBAs and developers Microsoft has a built-in extended stored procedure called xp_cmdshell.  With this extended stored procedure you have the ability to run any command line process, so you can embed this within your stored procedures, jobs or batch processing.  This option is disabled by default.  Also, to limit access to using xp_cmdshell only members of the sysadmin server role have default rights.

Solution

In this tip we will look at how to enable xp_cmshell and some of the errors you may see.

SQL Server Errors for xp_cmdshell

Here are some errors you may encounter with xp_cmdshell.

If you have sysadmin privileges and don't enable xp_cmdshell and you issue a command such as the following to get a directory listing of the C: drive:

xp_cmdshell 'dir c:\'

you get the following error message:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1 [Batch Start Line 0]
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', search for 'xp_cmdshell' in SQL Server Books Online.

If you don't have sysadmin privileges and try to run xp_cmdshell whether it is enable or not you get this error message:

Msg 229, Level 14, State 5, Procedure xp_cmdshell, Line 1 [Batch Start Line 0]
The EXECUTE permission was denied on the object 'xp_cmdshell', database 'mssqlsystemresource', schema 'sys'.

Another error you may get if you try to enable xp_cmdshell using sp_configure when advanced options is not set is the following error:

Msg 15123, Level 16, State 1, Procedure sp_configure, Line 62 [Batch Start Line 2]
The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.

So in order to use xp_cmdshell whether you are a sysadmin or a regular user you need to first enable the use of xp_cmdshell.

Enable xp_cmdshell with sp_configure

The following code with enable xp_cmdshell using sp_configure. You need to issue the RECONFIGURE command after each of these settings for it to take effect.

-- this turns on advanced options and is needed to configure xp_cmdshell
sp_configure 'show advanced options', '1'
RECONFIGURE
-- this enables xp_cmdshell
sp_configure 'xp_cmdshell', '1' 
RECONFIGURE

Disable xp_cmdshell with sp_configure

The following code with disable xp_cmdshell using sp_configure:

-- this turns on advanced options and is needed to configure xp_cmdshell
sp_configure 'show advanced options', '1'
RECONFIGURE
-- this disables xp_cmdshell
sp_configure 'xp_cmdshell', '0' 
RECONFIGURE

Enable or Disable xp_cmdshell with SSMS Facets

From within SSMS, right click on the instance name and select Facets.

The in the Facet drowdown, change to Server Security as shown below. 

You can then change the setting for XpCmdShellEnabled as needed to either True or False. After changing the value, click OK to save the setting and the change will take effect immediately.  There is not a need to enable show advanced options or use reconfigure, the GUI takes care of this automatically.

sql server facets

Granting Access to xp_cmdshell

Let's say we have a user that is not a sysadmin, but is a user of the master database and we want to grant access to run xp_cmdshell.

-- add user test to the master database
USE [master]
GO
CREATE USER [test] FOR LOGIN [test]
GO

-- grant execute access to xp_cmdshell
GRANT EXEC ON xp_cmdshell TO [test]

We get this error message:

Msg 15153, Level 16, State 1, Procedure xp_cmdshell, Line 1 [Batch Start Line 0]
The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the '##xp_cmdshell_proxy_account##' credential exists and contains valid information.

There is not a need to give a user sysadmin permissions or elevated permissions to run xp_cmdshell.  To do so you can create a proxy account as shown in this tip Creating a SQL Server proxy account to run xp_cmdshell.

Next Steps
  • Check your SQL Server instances and enable or disable xp_cmdshell as needed.


Last Updated: 2018-06-05


get scripts

next tip button



About the author
MSSQLTips author Greg Robidoux Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.

View all my tips
Related Resources




Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Thursday, July 21, 2016 - 4:43:13 AM - Nandakumar Back To Top

I do not know how to  enable the xp_cmdshell in SQL Server 2005 . and then i did see your website and  i enabled it in my client database.

thanks a lot . No words to say .

 

 


Monday, September 30, 2013 - 4:19:47 PM - Adelson Back To Top

Thanks !!!!


Tuesday, September 24, 2013 - 4:26:47 AM - stroct1 Back To Top

Thanks for helping


Wednesday, November 14, 2012 - 3:37:37 AM - narasimha Back To Top

thanks a lot , it is very helpful to us keep it uptopdate more and more 

 

best regards,

Narasimha 


Learn more about SQL Server tools