sudo で実行したとき、JAVA_HOME はスクリプトに設定されませんでした。

sudo で実行したとき、JAVA_HOME はスクリプトに設定されませんでした。

Javaをインストールし、環境変数を設定する必要があるインストールスクリプトを実行しようとしていますJAVA_HOME

とからJAVA_HOMEファイルを呼び出しました/etc/profile。私は正しい答えを得ることができ、さらに正しい答えを得ることができます。java.sh/etc/profile.decho $JAVA_HOMEsudo echo $JAVA_HOME

install.sh「実行しようとしています」へecho $JAVA_HOMEsudosudo

なぜこれが起こるのかご存知ですか?

CentOSを実行しています。

ベストアンサー1

セキュリティ上の理由から、sudo環境変数が消去される可能性があり、これが$ JAVA_HOMEを取得できない可能性があります。ファイル/etc/sudoersで参照してくださいenv_reset

からman sudoers

env_reset   If set, sudo will reset the environment to only contain the following variables: HOME, LOGNAME, PATH, SHELL, TERM, and USER (in addi-
           tion to the SUDO_* variables).  Of these, only TERM is copied unaltered from the old environment.  The other variables are set to
           default values (possibly modified by the value of the set_logname option).  If sudo was compiled with the SECURE_PATH option, its value
           will be used for the PATH environment variable.  Other variables may be preserved with the env_keep option.

env_keep    Environment variables to be preserved in the user's environment when the env_reset option is in effect.  This allows fine-grained con-
           trol over the environment sudo-spawned processes will receive.  The argument may be a double-quoted, space-separated list or a single
           value without double-quotes.  The list can be replaced, added to, deleted from, or disabled by using the =, +=, -=, and ! operators
           respectively.  This list has no default members.

したがって、JAVA_HOMEを維持するには、env_keepに以下を追加してください。

Defaults   env_keep += "JAVA_HOME"

またはJAVA_HOMEルートに設定します~/.bash_profile

おすすめ記事