NSF
NSF

Reputation: 2549

sudo: java: command not found after exiting from root user

I have installed jdk but when I tried to use the java command it said "command not found".

So I then set the environment variable for my account in .bashrc and it works okay.

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 -s to switch to root, put those into /etc/profile, and executed source /etc/profile

This works if I keep logged in as root but after I return to my own account and use sudo java it still says sudo: java: command not found and those environment variables become blank.

What should I do to solve the problem? I guess it should be easy to solve.

Upvotes: 3

Views: 18645

Answers (5)

lyuboe
lyuboe

Reputation: 171

I solved the problem by

vi /etc/sudoers

and then put $JAVA_HOME/bin in

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:$JAVA_HOME/bin"

Upvotes: 1

prtm
prtm

Reputation: 1

Check the permission on the jdk folder, make sure it's rwx.

Upvotes: 0

jedwards
jedwards

Reputation: 30260

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.

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).

For example, on my Fedora 16 box, I have

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 XAUTHORITY isn't there by default, I had to add it.

If you want your JAVA_HOME envvar to carry over, you could add it here. However, for this to be all you need to do, you'd need to add PATH to this list and that is really discouraged.

So also in this file is a line like the following:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

This will be your $PATH inside 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.

(Since you are getting the "command not found message", there are no java executables 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)

Alternative 1

Add an alias to java (eg alias java='/usr/java/latest/bin/java') inside a file like /etc/bashrc

Alternative 2

Add an alias to java in your local profile or bashrc files and an alias to sudo that will preserve your aliases.

Upvotes: 5

Dunes
Dunes

Reputation: 40903

You probably want to use something like update-alternatives (not sure if this is available on all distros).

It creates and manages symlinks in /usr/bin (ie. executables available to all).

Example usage: sudo update-alternatives --install java java /path/to/java/home/bin/java

I think java can then extract the location of JAVA_HOME based on it's executable location.

Upvotes: 0

jdevelop
jdevelop

Reputation: 12306

man sudo will give you the answer:

  -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.

Upvotes: 2

Related Questions