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

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