1

tl;dr I am trying to run a command with sudo, but it only works without using sudo. Read below for more details.


Hoping a linux expert might be able to spot the problem here. I installed ef core, per the microsoft instructions with this command:

sudo dotnet tool install --global dotnet-ef

Looking at the tools directory, I see it's installed:

ls ~/.dotnet/tools
dotnet-ef

I made sure my path variables include the tools directory:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/mrhoden/.dotnet/tools:/home/mrhoden/.dotnet/tools/

As you can see I tried adding the trailing slash just in case.

EF is accessible, just not with sudo and I'm dealing with a file access permission issue when trying to run a full command. Any ideas on how I can get these two commands to return the same successful result?

with sudo

sudo dotnet ef -h
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

without sudo

dotnet ef -h
Entity Framework Core .NET Command-line Tools 3.1.0

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.

Additional context, this is the command I am trying to run and it's subsequent result.

mrhoden@devbox:~/Projects/TestEfProject/Test.Web$ dotnet ef database update
System.UnauthorizedAccessException: Access to the path '/home/mrhoden/Projects/TestEfProject/Test.Web/obj/Test.Web.csproj.EntityFrameworkCore.targets' is denied.
 ---> System.IO.IOException: Permission denied
   --- End of inner exception stack trace ---
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
   at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.OpenWrite(String path)
   at Microsoft.EntityFrameworkCore.Tools.Project.FromFile(String file, String buildExtensionsDir, String framework, String configuration, String runtime)
   at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute()
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
Access to the path '/home/mrhoden/Projects/TestEfProject/Test.Web/obj/Test.Web.csproj.EntityFrameworkCore.targets' is denied.
2
  • The programs seems to be installed in user's home folder. So, sudo is not required to install the software. Delete the folder as root user (as you've installed it with sudo) then reinstall it without using sudo. Also the MS link does not mention to use sudo.
    – Biswapriyo
    Jan 12, 2020 at 19:38
  • You're right, they do not require sudo for install. I uninstalled and reinstalled without sudo. I tried it out and the result is the same. I will also update this question with the original command I am trying to solve for to help clarify the context. It might not even be necessary to use sudo. Jan 12, 2020 at 19:50

2 Answers 2

1

Keep in mind that sudo runs the command as root, or another user, and with that user's, or a minimal, environment depending on your security policy settings (/etc/sudoers). If you check that file and it says Defaults env_reset then sudo probably won't see your $PATH environment variable. Try with sudo -E, which may or may not be allowed.

4
  • Ok, using sudo visudo I see that Defaults env_reset is present. I'll admit I'm not very fluent with Linux, how would I update root to know the path to this tool? I think updating the secure_path line would work, but is that the appropriate place? Jan 12, 2020 at 20:37
  • Honestly, reading the docs I'm really confused what MS was thinking. If --global installs into ~/.dotnet, that doesn't seem very global to me. You have a couple of options - add the path to root's environment, try sudo -E or just try to feed the PATH variable to sudo: sudo env "PATH=$PATH:/home/USER/.dotnet/tools" dotnet ef -h
    – Kevin
    Jan 12, 2020 at 20:46
  • Hm, I tried updating the path. Still not working for some reason. I ran sudo env "PATH=$PATH:/home/mrhoden/.dotnet/tools" then tried sudo dotnet ef -h then tried sudo dotnet ef -h -E. Same issue "Could not execute because the specified command or file was not found." Jan 12, 2020 at 21:10
  • Don't split it up: sudo env "PATH=$PATH:/home/USER/.dotnet/tools" dotnet ef -h
    – Kevin
    Jan 12, 2020 at 23:34
0

I change the permits of the root folder to have access from your user something there:

sudo chown -R [user] [path your proyect]
sudo chown -R :[user] [path your proyect]

and execute the comand unused sudo \

dotnet ef -h

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .