Newly installed Ubuntu, jdk is installed, environment variables are also configured in the /etc/profile file, but strange things happen, when executed:
java -version
When there is no problem, but execute:
sudo java -version
When, it reported an error:
sudo: java: command not found
Search for solutions on the Internet:
1. Execute the command:
sudo visudo
Open the /etc/sudoers file:
#
# 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
Note: Open /etc/sudoers directly with vi and cannot be edited.
2. Add the bin directory of java to the secure_path configuration:
#
# 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. Use Ctrl+X to exit, and select save
After the above operations, the problem was solved.
But why? It turns out that secure_path defines the value of the PATH environment variable when using sudo. If it is not defined, is the value of the PATH environment variable when sudo is used?
With the spirit of thorough questioning, I tried to comment out the secure_path line, and then I found that sudo can use any command at this time.