Wednesday, April 1, 2015

Enable and Disable xp_cmdshell using SP_CONFIGURE

You can enable or disable xp_cmdshell by using the Policy-Based Management or by executing sp_configure. This option enables system adms to control wheather the xp_cmdshell extended stored procedure can be executed on a system.

By default this is option is disabled on a new installation.

How to enable:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- Enable xp_cmdshell feature
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO

** User might receive below errow when excuting sp_configure command

EXEC sp_configure ‘show advanced options’,1
GO
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '‘'. 


Note that the syntax is correct, but you might have used wrong character quote.

Run below query to check feature ENABLED or not.
SELECT * FROM SYS.CONFIGURATIONS WHERE Name = 'xp_cmdshell'


How to disable: 

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options',1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- Disable xp_cmdshell feature
EXEC sp_configure 'xp_cmdshell', 0
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

Run below query to check feature DISABLED or not.
SELECT * FROM SYS.CONFIGURATIONS WHERE Name = 'xp_cmdshell'



** Users might receive below error when executing RECONFIGURE command.
Msg 5808, Level 16, State 1, Line 1
Ad hoc update to system catalogs is not supported.


Note that some configuration options require a server stop and restart to update the currently running value." By using “WITH OVERRIDE” you should be able to run successfully.

RECONFIGURE WITH OVERRIDE;
GO
Command(s) completed successfully.

Regards
Satishbabu Gunukula
http://www.sqlserver-expert.com.

1 comment:

  1. Nice Article !

    Really this will help to people of SQL Server Community.
    I have also prepared small note on this, script to enable and disable xp_cmdshell in SQL Server.

    http://www.dbrnd.com/2015/11/sql-server-script-to-enable-and-disable-xp_cmdshell-configuration/

    ReplyDelete