skip to Main Content

I want to install dotnet to root on a linux vm and be able to run the dotnet with just a

sudo dotnet

call instead of needing to give the full path every time.

I installed dotnet on a linux VM with sudo using dotnet-install.sh. Dotnet is installed in /root/.dotnet/ . I can run the dotnet executable with sudo /root/.dotnet/dotnet –version which prints the version as expected. However, even though I did export PATH=’$PATH:/root/.dotnet" to add the dotnet folder to $PATH, and I can see that it’s been added by doing echo $PATH, trying to execute sudo dotnet –version will result in a Command not found error. How can I add the path properly so the dotnet executable can be ran with just sudo dotnet …?

2

Answers


  1. You can try two options:

    • Move the /root/.dotnet/dotnet binary to /usr/local/bin, and then you should be able to execute it.
    mv /root/.dotnet/dotnet /usr/local/bin
    

    OR

    • Create a symlink so that you can call the .dotnet/dotnet from your bin folder of your Linux VM:
    ln -s /root/.dotnet/dotnet /usr/local/bin
    
    Login or Signup to reply.
  2. sudo doesn’t typically use your personal PATH variable as that would be a security risk. A bad user could run a program with elevated privileges by installing it in a directory they control and putting that directory on their path ahead of a trusted directory.

    To demonstrate, the output of these two commands will likely be different for you:

    /bin/bash -c 'echo "$PATH"'
    sudo /bin/bash -c 'echo "$PATH"'
    

    The intended way to configure this is with the secure_path option in your sudoers file. See the sudoers manpage:

    secure_path
    Path used for every command run from sudo. If you don't trust the people
    running sudo to have a sane PATH environment variable you may want to use
    this. Another use is if you want to have the ''root path'' be separate
    from the ''user path''. Users in the group specified by the exempt_group
    option are not affected by secure_path. This option is not set by default.
    

    If you trust /root/.dotnet and want programs there to be executable with sudo then you can add it to the sudoers file:

    Defaults  secure_path="<current value>:/root/.dotnet"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search