0

The usermod command successfully through sudo.

sudo usermod -aG some_group some_user

However, if you execute it through su, then there will be an error.

su
...I enter root password...
usermod -aG some_group some_user
bash: usermod: command not found

Why it happens?

How to configure my system?

The fact is that I have a script that I cannot change. This script uses command usermod under su.

0

su loads profile from /etc/profile which may modifies shell $PATH variable.

man su says

-m, -p, --preserve-environment
              Preserve the entire environment, i.e., it does not set HOME,
              SHELL, USER nor LOGNAME.  This option is ignored if the option
              --login is specified.

so, use --preserve-environment flag to load $PATH from existing shell

su --preserve-environment

OR
manually export PATH variable

su
export PATH=$PATH:/path/of/usermod/dir
| improve this answer | |
0

You need the $PATH setting for root. That is no problem with sudo because it has its own $PATH.

su does not change $PATH by default. You have to make it start a login shell:

su -

instead of just

su

If you cannot change the su call then change $PATH before the call. If usermod is located at /usr/sbin/usermod then do this:

PATH=/usr/sbin:$PATH
su
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.