sudo problems

Latest response

I am running RedHat server with .NET Core 2.1.
I can happily run dotnet commands such as dotnet build, dotnet run etc.
However, if I try and sudo dotnet as in : sudo dotnet run I get the error sudo: dotnet: command not found.
Can anyone suggest why this is happening.

Andy

Responses

Hi Andrew,

What does "sudo -l" shows? This would show which commands user could run using sudo. Also, check if you could run commands using sudo with complete path to the binaries, something like "sudo /usr/bin/myprogram" and check if that works, if so, then it could be due to path parameter not properly for that user.

Thanks for your interest :-)

The output from sudo -l is:

Matching Defaults entries for a on localhost: !visiblepw, always_set_home, match_group_by_gid, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR
USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER
LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin:/bin:/usr/sbin:/usr/bin

User a may run the following commands on localhost: (ALL) ALL

If I use the path directly then it works fine. If the path is wrong how does it work when I do not use sudo ? Is there a way to fix the path issue ?

Best Regards

Andrew

One thing that I don't understand here is that when you could run command without using "sudo" then why do you use sudo?

If there is path problem then you may set/correct the PATH variable defined in the file ".bash_profile" under user home directory for any local user. For example, if i wish to include "mybin" directory under path so that I can run those commands from any where within my user account, then I'd add "$HOME/mybin" to the PATH variable and then either logout-login or source this file. After which "echo $PATH" would show this "/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/redhat/.local/bin:/home/redhat/bin:/home/redhat/mybin". Now, I could run binaries or executable defined under "mybin" directory without specifying path.

I hope this helps.

I need to use sudo because the .NET Core application I am trying to run accesses a serial port which can only be done under root.

Andy

Hi Andrew,

Just an idea ... what happens when you change to the directory where the dotnet binary resides and run it with sudo from there ? :)

Regards,
Christian

Hi Christian,

Do you mean my c# compiled binary or the dotnet binary ?

Andrew

Hi Andrew, I mean the dotnet binary which you want to run with root privileges as mentioned in your initial post. :)

Regards,
Christian

Christian,

No, if I go into the directory that has dotnet and then type sudo dotnet it gives the same error.

Andrew

Andrew,

Did you try adding it to the PATH variable and check?

I assumed the path must be correct as it finds dotnet when not using sudo. I am quite new to Linux - Is there a separate path sudo uses ?

Andy

Hi Andrew,

I guess I was able to trace it out. It is because of the "secure_path" parameter that has been set in "/etc/sudoers" file which by defaults looks for binaries in "/sbin:/bin:/usr/sbin:/usr/bin" path only, so appending additional paths to this would work. As I've tested this on my vm.

For testing purpose, I have created a file "list-files" (script) to list out files under "mybin" folder in my users home directory (/home/redhat) and path has been set as appropriate:

<AnsibleCoding>id
uid=1000(redhat) gid=1000(redhat) groups=1000(redhat),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

<AnsibleCoding>echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/redhat/.local/bin:/home/redhat/bin:/home/redhat/mybin

The script works when called anywhere inside my home directory, however, doesn't when called with sudo, since 'sudo' was looking out for the binary inside the "secure_path" location:

<AnsibleCoding>list-files
total 64
drwx------.  6 redhat redhat    205 Oct 10 21:09 .
drwxr-xr-x.  5 root   root       44 Oct 10 15:25 ..
drwxrwxr-x. 12 redhat redhat   4096 Sep 25 06:46 ansible-project
-rw-------.  1 redhat redhat  23110 Oct 10 21:11 .bash_history
-rw-r--r--.  1 redhat redhat     18 Mar  7  2017 .bash_logout
-rw-r--r--.  1 redhat redhat    234 Oct 10 12:30 .bash_profile
-rw-r--r--.  1 redhat redhat    231 Mar  7  2017 .bashrc
drwxrwxr-x.  2 redhat redhat     24 Oct 10 21:09 mybin
drwx------.  2 redhat redhat     57 Sep 12 07:40 .ssh
-rw-------.  1 redhat redhat   9004 Oct 10 21:09 .viminfo
-rw-r--r--.  1 root   root       47 Sep 11 13:44 .vimrc
Calling user : redhat
<AnsibleCoding>sudo list-files
sudo: list-files: command not found

<AnsibleCoding>logout
[root@localhost ~]# grep secure_path /etc/sudoers
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

So appending the required path would work. However, please be careful, as this would get executed as root user.

After modifications of "secure_path" parameter in '/etc/sudoers' file:

[root@localhost ~]# grep secure_path /etc/sudoers
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/home/redhat/mybin
[root@localhost ~]# su - redhat
Last login: Wed Oct 10 21:12:02 EDT 2018 on pts/1
<AnsibleCoding>sudo list-files
total 64
drwx------.  6 redhat redhat    205 Oct 10 21:09 .
drwxr-xr-x.  5 root   root       44 Oct 10 15:25 ..
drwxrwxr-x. 12 redhat redhat   4096 Sep 25 06:46 ansible-project
-rw-------.  1 redhat redhat  22896 Oct 10 21:16 .bash_history
-rw-r--r--.  1 redhat redhat     18 Mar  7  2017 .bash_logout
-rw-r--r--.  1 redhat redhat    234 Oct 10 12:30 .bash_profile
-rw-r--r--.  1 redhat redhat    231 Mar  7  2017 .bashrc
drwxrwxr-x.  2 redhat redhat     24 Oct 10 21:09 mybin
drwx------.  2 redhat redhat     57 Sep 12 07:40 .ssh
-rw-------.  1 redhat redhat   9004 Oct 10 21:09 .viminfo
-rw-r--r--.  1 root   root       47 Sep 11 13:44 .vimrc
Calling user : root

I hope this helps you.

Thank you vey much for taking the time to look into this for me :-) Modifying the sudoers file did the trick.

Many Thanks

Andew

Thank you Andrew. I'm glad that I was able to help you.. :)

Well done, Sadashiva - once again ... good job ! :)

Thanks Christian.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.