Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remote debug C#/.Net app on Linux when the app is run under a different user?

I'm running a .Net Core microservice on Linux (Ubuntu) and am trying to remote debug with Visual Studio over SSH. But the service is run under the user svcuser and my user is mainuser. Main user is in the same group as the service user.

In visual studio, I can see the process that the service is running under, but when I try to attach I get:

One or more errors occurred. Failed to attach to process. The .Net Debugger has insufficient privileges to debug the process. To debug this process, vsdbg must be running with root permissions.

I checked in MS documentation but for Linux all they have is this: https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging-dotnet-core-linux-with-ssh?view=vs-2019 which has no info on running the service with a different user

And the only info they have on fixing such a problem is for windows only: https://docs.microsoft.com/en-us/visualstudio/debugger/error-the-microsoft-visual-studio-remote-debugging-monitor-on-the-remote-computer-is-running-as-a-different-user?view=vs-2019

like image 872
Don Rhummy Avatar asked Apr 17 '20 22:04

Don Rhummy


1 Answers

If you have sudo privileges then this is relatively easy, and can be kept secured to those users with sudo privs. Avoids needing to reconfigure users/environments, and allows you to debug any process on the machine regardless of which user account it is running as.

If you use Visual Studio to make an initial attempt to debug you will find that a ~/.vs-debugger folder has been created in the home directory of the user account you were attempting to use. This command will help you locate the vsdbg binary which was installed. You can install VsDbg manually but I find leveraging the automated process is easier. If you are using VSCode this becomes a manual process, and an exercize left for the reader, but I would still use VS2019 IDE to prep the target just to keep things consistent between tools.

find ~ | grep vsdbg

For my installation the binary is located at ~/.vs-debugger/vs2019/vsdbg and this path will most likely change over time.

First, rename the binary to something convenient:

mv ~/.vs-debugger/vs2019/vsdbg ~/.vs-debugger/vs2019/vsdbg-bin

Second, create a script to replace the binary:

touch ~/.vs-debugger/vs2019/vsdbg
chmod 770 ~/.vs-debugger/vs2019/vsdbg
nano ~/.vs-debugger/vs2019/vsdbg

The script content might look something like this, note the full path to vsdbg-bin, the use of $@ ensures all command-line args passed to your script are forwarded to VsDbg.

#!/bin/bash
sudo ~/.vs-debugger/vs2019/vsdbg-bin $@

Now retry your debug session from Visual Studio, if you did things correctly you should be able to attach to any remote process on the target machine using SSH->VsDbg. "Works on my machine." ;) This was confirmed with VS2019 16.8.4, .NET 5.0, and VsDbg 16.9.20122.2 debugging an ASP.NET Core application running on Debian 5.4.8 (x64) launched by systemd under a service user account in Azure. "Sweet."

HTH!

like image 135
Shaun Wilson Avatar answered Sep 21 '22 17:09

Shaun Wilson