experchange > shell

Hongyi Zhao (11-28-19, 12:21 AM)
Hi,

I want to pick out the dnsmasq binary available on my os, but failed with
the following:

$ command -v dnsmasq
$

But see the following result of which:

$ sudo which dnsmasq
/usr/local/sbin/dnsmasq
$

So, I tried with the following:

$ sudo command -v dnsmasq
sudo: command: command not found
$

Any hints for this issue?

Regards
John McCue (11-28-19, 12:44 AM)
Hongyi Zhao <hongyi.zhao> wrote:
> Hi,
> I want to pick out the dnsmasq binary available on my os, but failed with
> the following:
> $ command -v dnsmasq
> $
> But see the following result of which:
> $ sudo which dnsmasq
> /usr/local/sbin/dnsmasq
> $


What does your PATH say ?

> So, I tried with the following:
> $ sudo command -v dnsmasq
> sudo: command: command not found
> $


command is a builtin on my system, so
sudo will not find it.

> Any hints for this issue?
> Regards


John
David W. Hodgins (11-28-19, 02:35 AM)
On Wed, 27 Nov 2019 17:21:44 -0500, Hongyi Zhao <hongyi.zhao> wrote:

> I want to pick out the dnsmasq binary available on my os, but failed with
> the following:
> $ command -v dnsmasq
> $


> But see the following result of which:
> $ sudo which dnsmasq
> /usr/local/sbin/dnsmasq
> $


> So, I tried with the following:
> $ sudo command -v dnsmasq
> sudo: command: command not found
> $
> Any hints for this issue?


See the section "Preventing shell escapes" in man sudoers, which is there to
prevent users who are only allowed some sudo permissions from getting a full
root shell.

Regards, Dave Hodgins
David W. Hodgins (11-28-19, 02:46 AM)
On Wed, 27 Nov 2019 19:35:47 -0500, David W. Hodgins <dwhodgins> wrote:

> See the section "Preventing shell escapes" in man sudoers, which is there to
> prevent users who are only allowed some sudo permissions from getting a full
> root shell.


Should have added, if you actually want to run a root shell use "su --login",
which can be abbreviated as "su -".

Regards, Dave Hodgins
Keith Thompson (11-28-19, 12:08 PM)
Hongyi Zhao <hongyi.zhao> writes:
> I want to pick out the dnsmasq binary available on my os, but failed with
> the following:
> $ command -v dnsmasq
> $
> But see the following result of which:
> $ sudo which dnsmasq
> /usr/local/sbin/dnsmasq
> $


What made you decide to use sudo on the "which" command?

Obviously /usr/local/sbin is not in your $PATH, but it is in the $PATH
of the root process invoked by sudo. (It's not unusual for "sbin"
directories to be in root's $PATH but not in ordinary users' $PATH.)

BTW, if you're using bash, "type" is probably better than "which".
"type" is a shell built-in, so it can track aliases, functions, other
built-in commands, and so forth. "which" is usually an external command.

> So, I tried with the following:
> $ sudo command -v dnsmasq
> sudo: command: command not found
> $


"command" is a shell built-in, so there's no way sudo can invoke it.

> Any hints for this issue?


If you want to find out whether the dnsmasq command would be available
under sudo:

sudo sh -c 'type dnsmasq'
or
sudo sh -c 'command -v dnsmasq'

This invokes a root shell process, and that process is able to invoke
the built-in "type" and "command" commands.
Helmut Waitzmann (11-28-19, 12:12 PM)
Hongyi Zhao <hongyi.zhao>:

>I want to pick out the dnsmasq binary available on my os, but
>failed with the following:
>$ command -v dnsmasq
>$
>But see the following result of which:
>$ sudo which dnsmasq
>/usr/local/sbin/dnsmasq
>$


There are two differences between your two commands:  First, the former
one uses “command”, while the latter uses “which”.  Second, the
former one does not make use of “sudo”, while the latter does.

So, which one of the two differences would you like to discuss?

>So, I tried with the following:
>$ sudo command -v dnsmasq
>sudo: command: command not found
>$
>Any hints for this issue?


As John already pointed out, “command” is a shell builtin utility,
not an executable.

As “sudo” executes the given command using one of the exec family
of functions, it cannot invoke a shell builtin.

You might let “sudo” invoke a shell, giving it a command line to
invoke its builtin utility “command”, though:

sudo sh -c -- '"$@"' sh command -v -- dnsmasq

Or just use the “-s” or “-i” options with “sudo”:

sudo -s command -v -- dnsmasq

sudo -i command -v -- dnsmasq
Joerg.Schilling (11-28-19, 03:13 PM)
In article <qrmstn$phr$1>,
Hongyi Zhao <hongyi.zhao> wrote:
[..]
>$ sudo command -v dnsmasq
>sudo: command: command not found
>$


The reason here is that your OS is not POSIX compliant.

POSIX requires the shell builtin "command" to be available as executable binary
command in the filesystem as well, but this is missing on your installation.
Geoff Clare (11-28-19, 03:22 PM)
Keith Thompson wrote:

> Hongyi Zhao <hongyi.zhao> writes:
>> $ sudo command -v dnsmasq
>> sudo: command: command not found

> "command" is a shell built-in, so there's no way sudo can invoke it.


Unless you use a POSIX conforming system, in which case "command" also
exists as an executable.

Solaris:

$ file /usr/xpg4/bin/command
/usr/xpg4/bin/command: executable /usr/xpg4/bin/sh script

HP-UX:

$ file /usr/bin/command
/usr/bin/command: commands text

macOS:

$ file /usr/bin/command
/usr/bin/command: POSIX shell script text executable
Keith Thompson (11-28-19, 11:17 PM)
Joerg.Schilling writes:
> In article <qrmstn$phr>,
> Hongyi Zhao <hongyi.zhao> wrote: [...]
> The reason here is that your OS is not POSIX compliant.
> POSIX requires the shell builtin "command" to be available as
> executable binary command in the filesystem as well, but this is
> missing on your installation.


Where does POSIX require that?

Ralf Damaschke (11-29-19, 03:09 AM)
Keith Thompson wrote:
> Joerg.Schilling writes: [...]
> Where does POSIX require that?
>


Look at
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_06>

| However, all of the standard utilities, including the regular
| built-ins in the table, but not the special built-ins described in
| Special Built-In Utilities, shall be implemented in a manner so that
| they can be accessed via the exec family of functions as defined in
| the System Interfaces volume of POSIX.1-2008 and can be invoked
| directly by those standard utilities that require it (env, find,
| nice, nohup, time, xargs).

The command utility is a regular built-in.
Kenny McCormack (11-29-19, 03:29 AM)
In article <pan.2019.11.29.01.09.13>,
Ralf Damaschke <rwspam> wrote:
[..]
>| directly by those standard utilities that require it (env, find,
>| nice, nohup, time, xargs).
>The command utility is a regular built-in.


Yeah, we get it. But it is a ridiculous requirement that everybody
recognizes as ridiculous and few systems actually do it.
Similar Threads