sudo: java: command not found引发的关于sudo命令执行权限的一点记录

新装的Ubuntu,安装了jdk,在/etc/profile文件中也配置了环境变量,但是奇怪的事情发生了,当执行:

java -version

时,没有任何问题,但执行:

sudo java -version

时,却报错:
sudo: java: command not found

在网上搜索到解决方案:

1、执行命令:

 sudo visudo 

打开/etc/sudoers文件:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

注意:用vi 直接打开/etc/sudoers无法编辑
2、将java的bin目录加入到secure_path配置中:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/opt/jdk1.8.0_221/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

3、用Ctrl+X退出,并选择保存

通过以上操作后,问题得到了解决。
但是为什么呢,原来是因为secure_path定义了使用sudo时PATH环境变量的值,那么如果不定义呢,是否sudo时PATH的值就是本来环境变量的值呢。
抱着刨根问底的精神,我试着将secure_path那行注释掉,然后我发现这个时候sudo能用任何命令了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章