0

I am logged in as root user and need to execute a single command from redbox user without switching. For this, I am using following command:

# su - redbox -c 'CMD'.

When I try to execute "ifconfig" (su - redbox -c 'ifconfig'), I get following error:

-bash: ifconfig: command not found

But it works if I switch to redbox first, and then execute "ifconfig" as shown below:

# su - redbox $ ifconfig

Same problem for many more commands. I don't understand what is causing this problem. Please help me figure it out.

I am using Alpine Linux v3.9.4

UPDATE:

Thanks to link shared by Jeff, I see the problem now. When I use su - redbox - c 'ifconfig', the value of $PATH is different than when I use su - redbox followed by ifconfig.

Can someone explain why $PATH is different in both cases? Note that in both cases I hove used - which uses login shell.

  • This is almost https://unix.stackexchange.com/q/29791/5132 . – JdeBP Dec 18 '19 at 13:58
  • @JdeBP Thats not true. If you observe, I have used '-' to specify that bash needs to start as login shell. But it still creates problem. – Niraj Joshi Dec 18 '19 at 14:13
  • Ahem! I distinctly wrote _almost_. – JdeBP Dec 18 '19 at 14:15
  • @JdeBP Point taken. But you got to agree that solution does not help me a least bit. – Niraj Joshi Dec 18 '19 at 14:18
  • 3
    You are not the only person that this WWW site targets. This is a public Q&A WWW site, not a personal helpdesk. Sometimes we point to other related stuff so that _other people_ can find it. – JdeBP Dec 18 '19 at 14:26
  • 1
    @NirajJoshi are you adverse to just running `/usr/sbin/ifconfig` instead of `ifconfig`? – Jeff Schaller Dec 18 '19 at 14:28
  • 1
    Related: https://unix.stackexchange.com/q/356105/117549 and https://unix.stackexchange.com/q/232782/117549 – Jeff Schaller Dec 18 '19 at 14:31
  • Thanks @Jeff. Linked questions helped a bit in understanding the problem. And ifconfig is just an example. I want to know why there is difference in those two ways. – Niraj Joshi Dec 19 '19 at 08:19

1 Answers1

0

Read man sudoers. sudo forces its idea of $PATH on the commands it executes.

sudo -u redbox -c $( type -p ifconfig) 

to use your $PATH or

sudo -u redbox /usr/sbin/ifconfig
waltinator
  • 4,439
  • 1
  • 16
  • 21