bash sudo: java: 从 root 用户退出后找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12309253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sudo: java: command not found after exiting from root user
提问by NSF
I have installed jdk but when I tried to use the java command it said "command not found".
我已经安装了 jdk,但是当我尝试使用 java 命令时,它说“找不到命令”。
So I then set the environment variable for my account in .bashrc and it works okay.
所以我然后在 .bashrc 中为我的帐户设置环境变量,它工作正常。
export JAVA_HOME=/usr/java/jdk1.7.0_03
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/l\
ib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
However when I try to use sudo to execute java it says that again. So I used sudo -sto switch to root, put those into /etc/profile, and executed source /etc/profile
但是,当我尝试使用 sudo 执行 java 时,它又说了一遍。所以我曾经sudo -s切换到root,将它们放入/etc/profile,然后执行source /etc/profile
This works if I keep logged in as root but after I return to my own account and use sudo javait still says sudo: java: command not found and those environment variables become blank.
如果我继续以 root 身份登录,但在我返回到我自己的帐户并使用sudo java它后仍然显示 sudo: java: command not found 并且这些环境变量变为空白,这会起作用。
What should I do to solve the problem? I guess it should be easy to solve.
我应该怎么做才能解决问题?我想应该很容易解决。
回答by jedwards
Some distros, by default, reset your environment variables to ensure programs executed under root run as expected. Any environment variable not specified in a whitelist is not carried into your root session.
默认情况下,某些发行版会重置您的环境变量以确保在 root 下执行的程序按预期运行。任何未在白名单中指定的环境变量都不会带入您的根会话中。
The instruction to reset to environment as well as what variables to allow is defined in /etc/sudoers. (You need permissions to view/edit this).
重置环境的指令以及允许的变量在/etc/sudoers. (您需要权限才能查看/编辑此内容)。
For example, on my Fedora 16 box, I have
例如,在我的 Fedora 16 机器上,我有
Defaults env_reset Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS" Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE" Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES" Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE" Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Note that XAUTHORITYisn't there by default, I had to add it.
请注意,XAUTHORITY默认情况下不存在,我必须添加它。
If you want your JAVA_HOMEenvvar to carry over, you could add it here. However, for this to be all you need to do, you'd need to add PATHto this list and that is really discouraged.
如果您希望您的JAVA_HOMEenvvar 继续使用,您可以在此处添加它。但是,为了让您只需要这样做,您需要将其添加PATH到此列表中,这真的不鼓励这样做。
So also in this file is a line like the following:
所以在这个文件中还有如下一行:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
This will be your $PATHinside your sudo session. Just ensure that you have a symlink to the java executable you want to use in one of those folders, and you'll be fine.
这将是您$PATH的 sudo 会话。只需确保您有一个指向要在其中一个文件夹中使用的 java 可执行文件的符号链接,就可以了。
(Since you are getting the "command not found message", there are no javaexecutables on your secure path, but if there were, you'd need to either remove it, or place your new symlink "higher" (earlier) in the path)
(由于您收到“找不到命令的消息”,因此java您的安全路径上没有可执行文件,但如果有,您需要将其删除,或将新符号链接“更高”(更早)放置在路径中)
Alternative 1
备选方案 1
Add an alias to java(eg alias java='/usr/java/latest/bin/java') inside a file like /etc/bashrc
在文件中添加别名java(例如alias java='/usr/java/latest/bin/java')/etc/bashrc
Alternative 2
备选方案 2
Add an alias to javain your local profile or bashrc files and an alias to sudothat will preserve your aliases.
java在您的本地配置文件或 bashrc 文件中添加别名 to以及sudo将保留您的别名的别名。
回答by jdevelop
man sudo will give you the answer:
man sudo 会给你答案:
-E The -E (preserve environment) option indicates to the security policy that the user wishes
to preserve their existing environment variables. The security policy may return an error
if the -E option is specified and the user does not have permission to preserve the environment.
回答by Dunes
You probably want to use something like update-alternatives(not sure if this is available on all distros).
您可能想要使用类似的东西update-alternatives(不确定这是否适用于所有发行版)。
It creates and manages symlinks in /usr/bin (ie. executables available to all).
它在 /usr/bin 中创建和管理符号链接(即所有人都可以使用的可执行文件)。
Example usage: sudo update-alternatives --install java java /path/to/java/home/bin/java
用法示例: sudo update-alternatives --install java java /path/to/java/home/bin/java
I think java can then extract the location of JAVA_HOMEbased on it's executable location.
我认为java可以JAVA_HOME根据它的可执行位置提取位置。
回答by prtm
Check the permission on the jdk folder, make sure it's rwx.
检查 jdk 文件夹的权限,确保它是rwx.
回答by lyuboe
I solved the problem by
我解决了这个问题
vi /etc/sudoers
and then put $JAVA_HOME/bin in
然后把 $JAVA_HOME/bin 放进去
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:$JAVA_HOME/bin"

