vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Run debug session as sudo

Open GuerrillaCoder opened this issue 8 years ago • 10 comments

Environment data

dotnet --info output: .NET Command Line Tools (2.0.0)

Product Information: Version: 2.0.0 Commit SHA-1 hash: cdcd1928c9

Runtime Environment: OS Name: ubuntu OS Version: 16.04 OS Platform: Linux RID: ubuntu.16.04-x64 Base Path: /usr/share/dotnet/sdk/2.0.0/

Microsoft .NET Core Shared Framework Host

Version : 2.0.0 Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

VS Code version: 1.17.2 b813d12980308015bcd2b3a2f6efa5c810c33ba5

C# Extension version: 1.12.1

Steps to reproduce

  1. Create a console project.
  2. Execute bash command in code that requires sudo permission

Expected behavior

command to be executed with root privilege.

Actual behavior

permission denied error

I have looked in launch settings documentation and also intellisense but I cannot see anywhere to set my debug launch options to use sudo. The work around is to launch VS Code as root but I have seen other languages vs code debuggers like python have ability to set it as a launch parameter for debugging session.

Is there any way to do this?

GuerrillaCoder avatar Oct 27 '17 04:10 GuerrillaCoder

@GuerrillaCoder Do you know how they accomplished a password prompt? You might be able to make this work as a pipeTransport (see docs). But I don't know how to get around the password prompt when trying to do that.

gregg-miskelly avatar Oct 27 '17 04:10 gregg-miskelly

@gregg-miskelly I am not really seeing what I should be doing with the pipeTransport to elevate debugging session. I tried to no success:

"pipeTransport": {
                "pipeProgram": "sudo /bin/bash",
                "pipeArgs": [],
                "debuggerPath": "~/vsdbg/vsdbg",
                "pipeCwd": "${workspaceRoot}",
            }

I haven't used the other debugger just looked at its docs (came up while I was googling the issue). I think a pop-up asking for password at beginning of session (gksudo) or typing it into terminal before session launches is fine. I'm just not exactly sure how to set that up.

GuerrillaCoder avatar Oct 27 '17 05:10 GuerrillaCoder

@GuerrillaCoder from what I can tell, gksudo will not redirect stdin/out across, so it might be possible for the extension to use it internally to add this feature, but it wouldn't work as a pipeTransport. Thinking more, sudo wouldn't work as there would be no way to pass in the password. I think the only thing that could work today would be to SSH into your own computer as root.

I will leave this open as a feature request. I think we could do this without too much effort by running sudo in the VS Code terminal.

gregg-miskelly avatar Oct 27 '17 16:10 gregg-miskelly

@gregg-miskelly Thanks. Makes sense now.

GuerrillaCoder avatar Oct 28 '17 05:10 GuerrillaCoder

same problem when debugging on Ubuntu 16.04. My application needs admin privileges. When debugging with "launch" request, there is no way to use "sudo" to launch .dll. When debugging with "attach" request and picking the running process, error happens:

No process with the specified id is currently running.

kinglionsoft avatar Dec 21 '17 08:12 kinglionsoft

FYI, it worked for me by throwing sudo in front of the debuggerPath, not ideal, bujt allowed me to debug.

E.g.

            "pipeTransport": {
                "pipeCwd": "${workspaceRoot}",
                "pipeProgram": "ssh",
                "pipeArgs": [ "[email protected]", "-p2222", "-i ${workspaceRoot}/.vagrant/machines/default/virtualbox/private_key" ],
                "debuggerPath": "sudo ~/vsdbg/vsdbg",
                "quoteArgs": false
            },

chrisrickard avatar Feb 06 '18 06:02 chrisrickard

Is there a workaround to debug MyApp.dll as sudo?

MyApp.dll needs to be run with sudo.

Ex:

{
	"name": "Local debug",
	"type": "coreclr",
	"request": "launch",
	"program": "${workspaceFolder}/MyApp.dll",
	"cwd": "${workspaceFolder}",
	"stopAtEntry": false,
	"console": "internalConsole",
}

moi15moi avatar Oct 22 '25 19:10 moi15moi

@moi15moi using a program of MyApp.dll should work with the work around that is above (see here). Note that this requires you to install vsdbg. See https://github.com/dotnet/vscode-csharp/blob/main/docs/debugger/Attaching-to-remote-processes.md#installing-vsdbg-on-the-server

gregg-miskelly avatar Oct 22 '25 22:10 gregg-miskelly

@gregg-miskelly I don't know if it make any difference, but I work with in wsl.

I already tried the message you mentionned, but it didn't worked for me. This is what I did:

  1. Install VsDbg in WSL
wget http://aka.ms/getvsdbgsh
chmod a+x getvsdbgsh
./getvsdbgsh  -v latest -l ~/vsdbg
  1. Have this config in vscode
{
	"name": "Local debug",
	"type": "coreclr",
	"request": "launch",
	"program": "/usr/bin/dotnet",
	"args": [
		"${workspaceFolder}/MyApp.dll", # I also tried to replace the "program" with my dll
	],
	"cwd": "${workspaceFolder}",
	"stopAtEntry": false,
	"console": "internalConsole",
	"pipeTransport": {
		"pipeCwd": "${workspaceRoot}",
		"pipeProgram": "ssh",
		"pipeArgs": [
			"[email protected]",
		],
		"debuggerPath": "sudo ~/vsdbg/vsdbg",
                "quoteArgs": false
	},
}
  1. When I launch it, nothing happen. It just load infinitly.

moi15moi avatar Oct 23 '25 13:10 moi15moi

@moi15moi sorry for the delay. There are a couple of problems with what you are trying to do that I can see (not sure if there are others):

  1. You need to use the -T argument to ssh (Disable pseudo-terminal allocation). Not sure why chrisrickard didn't need this.
  2. I think chrisrickard must have configured sudo to not prompt for a password, but obviously that isn't the default. You could configure that, or set things up so you can ssh in as root.
  3. You aren't using a key file to authenticate, which means that ssh is going to attempt to request a password, but there is no facility to do so.

gregg-miskelly avatar Oct 29 '25 22:10 gregg-miskelly