0

I am facing a problem when trying to execute a command using another user.

If I do this

sudo su - user
direct

Works perfectly

Now if I do this

sudo su - user -c direct

I face this error

-bash: direct: command not found

Does someone know what's happening? Thank you for your attention.

15

2 Answers 2

2

You don't actually need su in the command at all (what your sudo su - user does is to use sudo to jump to root with full privileges, and then use su to downgrade to the account called user. You might as well just jump straight to user with sudo).

One of these lines will work, depending on how /connect/ndm/bin has been added to your $PATH. Use the first one that works:

sudo -u user direct
sudo -iu user direct
sudo -Hiu user direct

The first variant runs direct as user user. The second variant also tells the target shell to run in "interactive" mode, which means for bash it would execute .bash_profile or .profile, setting up the environment ($PATH, etc.). The final version also forces the $HOME variable to be set for the target user account too. With some configurations this is done automatically by sudo but I included it here anyway "just in case".

2
  • Thank you so much! The second and third options worked for me, but, may you explain to me what they are really doing? Sep 30, 2022 at 4:09
  • 1
    @AntonioCosta I've added a paragraph of explanation. I hope it's sufficiently understandable Sep 30, 2022 at 12:23
1

sudo su - user will load the profile startup files, which probably set the PATH to the direct command.

When you use -c, the direct command can't be found.

One solutions would be to specify the full path to the binary:

sudo su - user -c /full/path/to/direct

To find this path, use command -v or which from the shell able to find it:

sudo su - user
command -v direct
1
  • Got it! In my case, I have tried using the full path of direct but I faced another error from direct application. Sep 30, 2022 at 4:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .