xp_cmdshell(服务器配置选项)

适用于:SQL Server

本文介绍如何启用 xp_cmdshell SQL Server 配置选项。 此选项使系统管理员能够控制是否可以在系统上执行 xp_cmdshell 扩展存储过程。 新安装中默认禁用 xp_cmdshell 选项。

启用此选项前,请务必考虑潜在的安全隐患。

  • 新开发的代码不应使用 xp_cmdshell 存储过程,通常应将它保留为禁用状态。
  • 某些旧版应用程序要求启用 xp_cmdshell。 如果无法将这些应用程序修改为避免使用此存储过程,则可以按下面所述启用它。

注意

如果必须使用 xp_cmdshell,最佳安全做法建议只在需要它的实际任务的持续时间内启用。 使用 xp_cmdshell 可以触发安全审核工具。

如果需要启用 xp_cmdshell,可以使用基于策略的管理或运行 sp_configure 系统存储过程,如以下代码示例所示:

-- To allow advanced options to be changed.
EXECUTE sp_configure 'show advanced options', 1;
GO

-- To update the currently configured value for advanced options.
RECONFIGURE;
GO

-- To enable the feature.
EXECUTE sp_configure 'xp_cmdshell', 1;
GO

-- To update the currently configured value for this feature.
RECONFIGURE;
GO

-- To set "show advanced options" back to false
EXECUTE sp_configure 'show advanced options', 0;
GO

-- To update the currently configured value for advanced options.
RECONFIGURE;
GO

后续步骤