【Linux】root和子用户都能执行的命令,sudo无法执行(已解决)

全流程帖子 https://ask.oceanbase.com/t/topic/35604437/7

1.问题

如题,在编译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

根据字面意思,是cmake命令找不到,但是我的系统里面已经有了符合条件的环境;以下是gihub/miniob仓库中docs里面how_to_build.md的内容

MiniOB 需要使用:

  • cmake 版本 >= 3.13
  • gcc/clang gcc建议8.3以上,编译器需要支持c++20新标准
  • flex (2.5+), bison (3.7+) 用于生成词法语法分析代码

我使用的系统是centos8-steam的vmware虚拟机;当前使用的miniob的commit为

76221e46e66ef408771ce886aa0c586a09374b0d

以下是我的系统中各个依赖项的版本号,可以看到在子用户中,依赖项的所有命令都可以正常执行

[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.

在root中,也可以正常执行这些命令

[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.

但是sudo执行miniob的安装脚本的时候,却找不到cmake命令

2.debug过程

经过大佬的教学,知道了一个新的sudo用法:sudo -E

sudo -E 是继承当前用户的环境变量运行sudo后面的命令,否则环境变量会被清理掉;

但是在初次使用的时候,依旧无法成功编译miniob,也找不到cmake命令

[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.最终解决:PATH环境变量

最终的解决办法是我自己想出来的(大佬也回复了我这个解决方案)

当前在子用户使用 sudo -E是找不到cmake命令的。

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

我的系统里面的PATH环境变量如下。

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

而cmake的路径如下

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

有没有可能,是因为cmake不在PATH环境变量里面,导致sudo的时候找不到命令呢?虽然在root和mu用户下都可以直接执行cmake。

于是我就去root里面执行了一下软连接

[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

再来试试

$ sudo -E cmake --version
cmake version 3.27.4

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

最后再来试试编译,应该是OK了,init成功执行,编译也通过了,没有报错

image-20230901181050840

感谢miniob社区大佬们的帮助!

4.结论

如果出现一个命令,root和子用户都可以执行,但是子用户中sudo却找不到此命令,可以尝试检查一下该命令所在路径是否与当前系统的PATH环境变量不符合!

猜你喜欢

转载自blog.csdn.net/muxuen/article/details/132631010