Reputation: 4976
I have a project made and tested with Visual Studio. It works.
Then I uploaded it into Ubuntu server.
Then ran it with dotnet run
. Works, remote machines see it (via nginx proxy).
Then I tried dotnet run &
. The process seems like started, but nothing listens on port specified. Then, according to the example, I tried sudo nohup dotnet run kestrel > /dev/null 2>&1 &
. This time it listened for a while, then died with:
Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] An unhandled exception has occurred: Can not find compilation library location for package 'google.protobuf' System.InvalidOperationException: Can not find compilation library location for package 'google.protobuf' at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
(A fragment of output from nohup.out, first linens, I skipped project details as irrelevant and private).
Any clues what's happening? I still get no errors when running it in foreground.
Here's what I found out: I can't run it (it gives same error messages) when I run it as root. On my test server I have a special user account named "dotnet". When I log in as dotnet
I can run the app. As root I can't.
I don't want to run my app with root privileges.
Next try: I run dotnet restore
as root. Then I go with nohup dotnet run kestrel > /dev/null 2>&1 &
and it works.
Nice. Now is there a way to start my app with limited privileges?
Upvotes: 3
Views: 2964
Reputation: 4976
I found the answer myself, so I'll share.
First: do not run .NET core projects on Linux as root if you don't intend to give them full root privileges. I think web applications with root privileges are bad idea and sort of asking for troubles.
But well, when something doesn't work, it's tempting to use sudo
from time to time. And it turns out it was the cause of the problem:
dotnet restore
and dotnet run
must be executed with the same privileges, by the same user. When I issued dotnet restore
as root, it worked. It's even harder the other way, when you want to run the project as user with lower privileges. You have to remove all temporary files before issuing dontet restore
. So, generally nohup dotnet run kestrel > /dev/null 2>&1 &
works as charm, no sudo
is needed here, running this as root can be harmful.
Now I always create special dotnet
user to run dotnet apps on servers. It's safer this way. The user cannot sudo. When I need to perform administrative tasks I just start separate root session. I use the same approach with database access. The app has only rights to execute procedures, not even select allowed.
Upvotes: 5