Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry for my bad English, but I'm really trying to be better. Ok. I've created a new command/alias(changejava) on Ubuntu 12.04, and I need to skip "sudo password" only when I write "$changejava". If I write "$changejava" using terminal, Ubuntu show me what can I do. I just need to press 0, 1 or 2. But if I press anything, terminal says:

"update-alternatives: to use /usr/lib/jvm/java-6-sun/jre/bin/java to provide /usr/bin/java (java). update-alternatives: error: Could not create file '/var/lib/dpkg/alternatives/java.dpkg-tmp': Denied. "

If I try "$sudo changejava", I have: "Type jessegaspar password", and the magic happens. Without "sudo", does not happen. I tried to edit /etc/sudoers with:


...However all users will have access to the total permission "sudo". :/ :/


share|improve this question
Can you not simply specify which version you want to use when launching an application? – Goddard Aug 23 '12 at 18:08
Hello, Goddard! Sure. I wanna choose Java6 or OracleJDK7 without "sudo password". But "sudo" can't be available for all users. – Jesse Gaspar Aug 23 '12 at 21:17
@JesseGaspar he means choose it for the program you are running, not to change the system-wide default. I run many different versions of Java for different programs (even at the same time) without sudo and without changing the system's default java. – dsh Aug 23 '12 at 22:29

First I want to reiterate that you do not need to change the system-wide default Java runtime in order to run a program using a non-default Java runtime.

For example, Eclipse and NetBeans have configuration files where you can specify which Java runtime it should use.

Also typical of Java applications is to honor the $JAVA_HOME environment variable. So at the shell, just run JAVA_HOME=/usr/lib/jvm/java-6-sun/ foo and JAVA_HOME=/usr/lib/jvm/java-7-oracle/ bar for applications foo and bar. You can even put this in a script so you don't have to type it each time.

Since you are focused on changing the system-wide default Java runtime, the Ubuntu Community documentation on sudo is one place to read:

The manpage (ie man sudoers) is another:

You are correct in stating that you do not want %sudo ALL=NOPASSWD: ALL in your sudoers file. Instead try something like this:

    jessegaspar caelum-sala1-9 = (root) NOPASSWD:/usr/local/bin/changejava

This may be more restrictive than you really want, but it is an example. It is probably good practice to list the full path to the command, but that may not be the path to your script on your system.

share|improve this answer
Wow! Thanks, @dsh! I tried "%sudo ALL=NOPASSWD:/usr/bin/changejava". Simple. :O – Jesse Gaspar Aug 27 '12 at 15:54
@JesseGaspar To accept this answer, click the checkmark to the left of it. Thanks! – dsh Aug 28 '12 at 22:41

Open terminal window and type:

sudo visudo

In the bottom of the file,(after "#includedir /etc/sudoers.d") type the follow:



Hope it helps! This will only work with terminal commands normal actions wont be affected.

share|improve this answer
Sorrry for the bad pic but it is basically the same as terminal. Just scroll down with the arrows and you will see the text and ad the line as the picture shows. And I cant garanti it to work when I havent done it myself. – Leo Aug 23 '12 at 18:43
Hello, Leo. Thanks for your attention! I've tried a lot of modifications in /etc/sudoers. But "sudo" can't be available for all users or commands. Just for "changejava". Example: I want to type "changejava" without sudo. But if I try "sudo apt-get install", I need to type the password. Thank you. – Jesse Gaspar Aug 23 '12 at 21:22
If you really want to change the default java with sudo (see other comments on why that is not necessary), then man sudoers. You don't need to give ALL users ALL privileges. Just give your user privilege to that one command. – dsh Aug 23 '12 at 22:30
@dsh Hello! Thanks for your feedback.Let me show you some images: – Jesse Gaspar Aug 24 '12 at 14:50
@JesseGaspar Yes, those images show the shell output that is expected when you don't have sufficient privileges. I just expanded my comments to a full-fledged answer. Maybe that makes it more clear? – dsh Aug 24 '12 at 19:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.