Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:

%sudo ALL=NOPASSWD: ALL

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

Thanks.

share|improve this question
1  
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

2 Answers 2

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: https://help.ubuntu.com/community/Sudoers

The manpage (ie man sudoers) is another: http://www.sudo.ws/sudo/sudoers.man.html

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! askubuntu.com/faq#howtoask –  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:

username ALL=(ALL) NOPASSWD: ALL

terminal

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
1  
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: i.stack.imgur.com/RooIX.jpg i.stack.imgur.com/V6Lbl.jpg i.stack.imgur.com/5Lx82.jpg –  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

 
discard

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.