[Linux] Commands that can be executed by root and sub-users, but cannot be executed by sudo (solved)

Full process post https://ask.oceanbase.com/t/topic/35604437/7

1. Question

As mentioned, I encountered the following error when compiling miniob.

[mu@vm-cnt8:~/code/miniob]$ sudo bash build.sh init
build.sh init
HEAD is now at 5df3037d Merge branch 'release-2.1.12-stable-pull' into patches-2.1
build.sh: line 83: cmake: command not found
build.sh: line 91: cmake: command not found
build.sh: line 99: cmake: command not found
build.sh: line 107: cmake: command not found

According to the literal meaning, the cmake command cannot be found, but my system already has a qualified environment; the following is how_to_build.mdthe content in the docs in the gihub/miniob warehouse

MiniOB requires:

  • cmake version >= 3.13
  • gcc/clang gcc recommends 8.3 or above, the compiler needs to support the new c++20 standard
  • flex (2.5+), bison (3.7+) for generating lexical and syntax analysis code

The system I use is the vmware virtual machine of centos8-steam; the commit of the miniob currently used is

76221e46e66ef408771ce886aa0c586a09374b0d

The following are the version numbers of each dependency in my system. You can see that in the sub-user, all commands of the dependencies can be executed normally.

[mu@vm-cnt8:~/code/miniob]$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[mu@vm-cnt8:~/code/miniob]$ cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[mu@vm-cnt8:~/code/miniob]$ flex --version
flex 2.6.1
[mu@vm-cnt8:~/code/miniob]$ bison --version
bison (GNU Bison) 3.8
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

In root, you can also execute these commands normally

[root@vm-cnt8:~]# cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@vm-cnt8:~]# flex --version
flex 2.6.1
[root@vm-cnt8:~]# bison --version
bison (GNU Bison) 3.8
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

But when sudo executes the miniob installation script, the cmake command cannot be found.

2.debug process

After the boss’s teaching, I learned a new sudo usage:sudo -E

sudo -EIt is to inherit the current user's environment variables and run the command after sudo, otherwise the environment variables will be cleared;

However, when using it for the first time, miniob still cannot be compiled successfully, and the cmake command cannot be found.

[mu@vm-cnt8:~/code/miniob]$ sudo -E bash build.sh init
[sudo] password for mu: 
build.sh init
HEAD is now at 5df3037d Merge branch 'release-2.1.12-stable-pull' into patches-2.1
build.sh: line 83: cmake: command not found
build.sh: line 91: cmake: command not found
build.sh: line 99: cmake: command not found
build.sh: line 107: cmake: command not found

[mu@vm-cnt8:~]$ sudo -E cmake --version
[sudo] password for mu: 
sudo: cmake: command not found

3. Final solution: PATH environment variable

The final solution was something I came up with myself (the boss also replied to me with this solution)

sudo -ECurrently, the cmake command cannot be found when using the sub-user .

[mu@vm-cnt8:~]$ sudo -E cmake --version
[sudo] password for mu: 
sudo: cmake: command not found

The PATH environment variables in my system are as follows.

[mu@vm-cnt8:~]$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin

The path of cmake is as follows

[mu@vm-cnt8:~]$ type cmake
cmake is /usr/local/bin/cmake
[mu@vm-cnt8:~]$ whereis cmake
cmake: /usr/local/bin/cmake /usr/share/cmake

Is it possible that the command cannot be found when sudoing because cmake is not in the PATH environment variable? Although cmake can be executed directly under both root and mu users.

So I went to root and executed a soft connection.

[root@vm-cnt8:~]# ls /usr/bin | grep cmake
[root@vm-cnt8:~]# ln -s /usr/local/bin/cmake /usr/bin/cmake
[root@vm-cnt8:~]# ll /usr/bin | grep cmake
lrwxrwxrwx. 1 root root          20 Sep  1 05:57 cmake -> /usr/local/bin/cmake

Try again

$ sudo -E cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Finally, try compiling again. It should be OK. Init was successfully executed and compilation passed without any errors.

image-20230901181050840

Thanks to the miniob community leaders for their help!

4 Conclusion

If a command appears, both root and sub-users can execute it, but sudo in the sub-user cannot find the command. You can try to check whether the path where the command is located does not match the PATH environment variable of the current system!

Guess you like

Origin blog.csdn.net/muxuen/article/details/132631010