Command running as root but not with sudo

1

I'm currently configuring a Samba 4 DC on CentOS 7.

As myself, samba-tool runs fine but when I run sudo samba-tool I get command not found.

samba-tool is located in /usr/local/samba/bin. I've added the directory to the PATH for both myself and root.

As myself, env | ^PATH= correctly includes the above directory. However, sudo env | ^PATH= shows only PATH=/sbin:/bin:/usr/sbin:/usr/bin.

Switching now to the root user. env | ^PATH= returns PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/samba/bin:/usr/local/samba/sbin and samba-tool runs just fine.

It was my intuition that sudo perfectly mirrored the root user, which I guess is not the case. A fix and hopefully an explanation is highly appreciated.

Oliver Benning

Posted 2017-06-27T12:35:29.357

Reputation: 113

Try using 'su', then execute the command without 'sudo'. – TooMuchRAM – 2017-06-27T12:41:04.977

@Thijs365 I don't understand, do you mean switch into root and run the command? I'm already aware that that works. – Oliver Benning – 2017-06-27T12:45:27.067

Okay. Are you sure you have sudo installed? – TooMuchRAM – 2017-06-27T12:46:29.367

@Thijs365 Yes. Sudo works as expected for its env path above. – Oliver Benning – 2017-06-27T12:49:59.200

Okay. I won't say anything anymore. – TooMuchRAM – 2017-06-27T12:50:44.943

Answers

1

It may be your sudo uses PATH defined in /etc/sudoers file, not the root's PATH. Check man 5 sudoers, look for the secure_path option.

You may also read
How to make sudo preserve $PATH?

Hint: use visudo to edit /etc/sudoers. The tool does some sanity check and may save you from the situation when you lose sudo access due to invalid edit.

Kamil Maciorowski

Posted 2017-06-27T12:35:29.357

Reputation: 38 429