CentOS 7下通过PuTTY更新Java时编辑alternatives遇权限拒绝问题
alternatives on CentOS 7 Hey there, let's sort out this permission issue you're hitting when trying to update your Java version using alternatives. The core problem here is straightforward—system-wide configuration tools like alternatives require root-level privileges, and your regular user mcserver doesn't have the right to modify those settings by default. Here's how to fix it step by step:
1. Run the Command with Sudo
The easiest way to get the necessary permissions is to prefix the alternatives command with sudo. This temporarily elevates your user privileges to root for that single command:
sudo alternatives --config java
When prompted, enter your mcserver user password (assuming your account has sudo access—if not, skip to the next step). You'll then see the same selection menu you posted, and you can type the number corresponding to the Java version you want to use (like 2 for JDK 1.8.0_171 or 3 for JDK 10.0.1) and press Enter.
2. Switch to the Root User (If Sudo Isn't Available)
If your mcserver account doesn't have sudo permissions, switch directly to the root user first:
su - root
Enter the root password when prompted, then run the alternatives command without sudo:
alternatives --config java
Make your selection from the menu, and the system will update the default Java version.
3. Verify the Configuration Worked
After making your selection, confirm that the default Java version has changed by running:
java -version
You should see output matching the JDK version you chose. If you also need to update the default javac (for compiling Java code), repeat the same steps but replace java with javac in the commands:
sudo alternatives --config javac # Or as root: alternatives --config javac
Optional: Add a New JDK to alternatives (If It's Not Listed)
If your newly installed JDK isn't showing up in the alternatives menu (though you mentioned it's already installed), you can manually add it with these commands (replace paths with your actual JDK location):
# Add java executable sudo alternatives --install /usr/bin/java java /opt/jdk1.8.0_171/bin/java 200 # Add javac executable sudo alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_171/bin/javac 200
The number 200 here is the priority—higher numbers mean the option will be preferred by default if no manual selection is made.
内容的提问来源于stack exchange,提问作者RAD140

