← Back to list

How to Make Sudo Ask for the Root Password Instead of Your User Password

How to Make Sudo Ask for the Root Password Instead of Your User Password

thowfick official · 2025-03-25 04:32 · 0 claps · 3.0 min read
#visudo #root-password #sudo-password #linux-root #change-root-password
Open on Medium ↗
Wiki topics: 🔓 · Open Source

How to Make Sudo Ask for the Root Password Instead of Your User Password

How to Make Sudo Ask for the Root Password Instead of Your User Password

How to Make Sudo Ask for the Root Password Instead of Your User Password

How to Make Sudo Ask for the Root Password Instead of Your User Password

Introduction

By default, when using sudo in Linux, the system asks for the user's password to verify their identity before executing commands with superuser privileges. However, in some cases, it may be more secure or convenient for sudo to prompt for the root password instead. This configuration can be beneficial in multi-user environments where users have sudo access but should not have full administrative control.

This guide provides a step-by-step explanation of how to modify sudo behavior so that it requires the root password instead of the user’s password.

Step 1: Setting the Root Password

Before configuring sudo to ask for the root password, the root account must have a password set. If a root password has not been set, it can be done using the following command:

sudo passwd root

After running this command, the system will prompt for a new root password and then ask for confirmation. Ensure the password is strong and secure.

Step 2: Editing the sudoers File

Understanding the sudoers File

The sudoers file (/etc/sudoers) controls sudo behavior, defining:

  • Which users can execute commands with sudo privileges
  • Which commands they are allowed to run
  • Whether authentication is required and which password should be used

Modifying this file incorrectly can cause issues, including losing sudo access. To avoid misconfigurations, always use visudo, a specialized tool that ensures correct syntax.

Opening the sudoers File

Use the following command to safely open the sudoers file:

sudo visudo

What Happens When This Command is Run?

  • sudo grants temporary root privileges to execute visudo.
  • visudo opens the /etc/sudoers file in a controlled environment.
  • If any syntax errors exist, visudo will display warnings and prevent saving incorrect configurations.

Step 3: Modifying the sudoers File

Inside the visudo editor, locate the following line:

Defaults    env_reset

This line is part of the default security settings that reset most environment variables when running sudo. It does not need to be modified, but serves as a reference point.

Adding the Required Line

Directly below Defaults env_reset, add the following line:

Defaults    rootpw

Explanation of This Change

  • By default, sudo prompts for the user’s password.
  • The rootpw option overrides this default behavior.
  • With this configuration, sudo will now prompt for the root password instead of the user’s password.

Step 4: Saving and Exiting visudo

Once the necessary changes have been made, the sudoers file must be saved and closed properly.

For nano editor (the default on many systems):

  • Press CTRL+X to exit.
  • Press Y to confirm saving the changes.
  • Press Enter to finalize.

For vi/vim editor:

  • Press ESC to enter command mode.
  • Type :wq (write and quit).
  • Press Enter to save and exit.

It is important to ensure that the file is saved correctly. Any syntax errors in the sudoers file could cause issues with sudo functionality.

Step 5: Testing the Configuration

To verify that the changes have been applied successfully, run:

sudo su

Instead of asking for the user’s password, the system should now prompt for root’s password:

[sudo] password for root:

If the root password is entered correctly, the command will execute with root privileges.

Understanding How It Works Internally

  1. When a sudo command is executed, the system checks /etc/sudoers to determine authentication requirements.
  2. Since Defaults rootpw has been set, sudo now prompts for the root password instead of the user's password.
  3. Upon successful authentication with the root password, the command is executed with root privileges.

This method ensures that only those with direct knowledge of the root password can execute sudo commands, providing an extra layer of security in some scenarios.

Conclusion

By making this simple change to the sudoers file, it is possible to configure the system to prompt for the root password instead of the user’s password when using sudo. This setup can be particularly useful in shared environments where stricter control over privileged commands is necessary.

Following these steps carefully ensures that sudo remains functional while enforcing root password authentication. For more Linux security tips and configuration guides, stay tuned for future updates!


메타데이터
post_id
78180c3a4eae
slug
how-to-make-sudo-ask-for-the-root-password-instead-of-your-user-password-78180c3a4eae
url
https://medium.com/@thowfickofficial/how-to-make-sudo-ask-for-the-root-password-instead-of-your-user-password-78180c3a4eae
canonical_url
https://medium.com/@thowfickofficial/how-to-make-sudo-ask-for-the-root-password-instead-of-your-user-password-78180c3a4eae
author_url
https://medium.com/@thowfickofficial
status
ok
fetched_at
2026-07-20 13:32:42