1

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

5

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
Hauke Laging
  • 90,279
1

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
Akhil
  • 1,290