sudo 无权访问全局安装的软件

sudo 无权访问全局安装的软件

总结我正在尝试使用 sudo 运行命令,但只有在不使用 sudo 的情况下它才有效。请阅读下文了解更多详细信息。


希望 Linux 专家能够发现这里的问题。我按照以下说明安装了 ef core微软说明使用以下命令:

sudo dotnet tool install --global dotnet-ef

查看工具目录,我发现它已经安装:

ls ~/.dotnet/tools
dotnet-ef

我确保我的路径变量包含工具目录:

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/

正如您所看到的,我尝试添加尾随斜线以防万一。

EF 可以访问,但不能使用 sudo,而且我在尝试运行完整命令时遇到了文件访问权限问题。关于如何让这两个命令返回相同的成功结果,您有什么想法吗?

使用 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.

没有 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.

附加上下文,这是我尝试运行的命令及其后续结果。

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.

答案1

请记住,sudo 以 root 或其他用户的身份运行命令,并使用该用户的环境或最小环境,具体取决于您的安全策略设置 ( /etc/sudoers)。如果您检查该文件并显示,Defaults env_reset则 sudo 可能看不到您的$PATH环境变量。尝试使用sudo -E,这可能会或可能不会被允许。

答案2

我更改了根文件夹的许可证,以便您的用户可以访问那里的一些内容:

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

并执行未使用的命令 sudo \

dotnet ef -h

相关内容