To configure passwordless sudo, you need to modify the sudoers file. This file tells Linux which users have administrative powers and how they should be treated.
Warning: Be very careful! A mistake in this file can lock you out of administrative access.
Here are the steps to do it (you can try this in your current VM):
1. Use visudo
Never edit the sudoers file with a regular text editor like nano or vim directly. Always use the visudo command. It checks for syntax errors before saving, which prevents you from breaking your system.
sudo visudo
2. Add the configuration line
Scroll to the bottom of the file and add the following line (replace username with your actual username, like labex):
username ALL=(ALL) NOPASSWD:ALL
What this line means:
username: The user this rule applies to.ALL=(ALL): Allows the user to run commands from any terminal as any user.NOPASSWD:ALL: This is the "magic" part. It tells the system: "Do not ask for a password when this user runs any command with sudo."
3. Save and Exit
- If
visudoopened in Nano (the default in LabEx): PressCtrl+OthenEnterto save, andCtrl+Xto exit. - If it opened in Vim: Press
Esc, type:wq, and pressEnter.
How LabEx is configured:
In your LabEx VM, if you run sudo cat /etc/sudoers.d/labex, you will likely see a file specifically for your user that already has this setting. It’s common for developers and cloud environments to use this to save time!
Practice Task:
Try running sudo whoami. If it returns root without asking for a password, your passwordless sudo is already working!