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能用任何命令了。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值