How to Switch (su) to Another User Account without Password

In this guide, we will show how to switch to another or a specific user account without requiring a password. For example, we have a user account called postgres (the default PostgreSQL superuser system account), we want every user (typically our PostgreSQL database and system administrators) in the group called postgres to switch to the postgres account using the su command without entering a password.

By default, only the root user can switch to another user account without entering a password. Any other user will be prompted to enter the password of the user account they are switching to (or if they are using the sudo command, they will be prompted to enter their password), if they don’t provide the correct password, they get an “authentication failed” error as shown in the following screenshot.

User Authentication Failure Error
User Authentication Failure Error

You can use any of the two solutions provided below to solve the above issue.

1. Using PAM Authentication Module

PAM (Pluggable authentication modules) are at the core of user authentication on modern Linux operating systems. To allow users in a specific group to switch to another user account without a password, we can modify the default PAM settings for the su command in the /etc/pam.d/su file.

# vim /etc/pam.d/su
OR
$ sudo vim /etc/pam.d/su

Add the following configurations after “auth sufficient pam_rootok.so” as shown in the following screenshot.

auth       [success=ignore default=1] pam_succeed_if.so user = postgres
auth       sufficient   pam_succeed_if.so use_uid user ingroup postgres

In the above configuration, the first line checks if the target user is postgres, if it is, the service checks the current user, otherwise, the default=1 line is skipped and the normal authentication steps are executed.

auth       [success=ignore default=1] pam_succeed_if.so user = postgres

The line that follows checks if the current user is in the group postgres, if yes, the authentication process is considered successful and returns sufficient as a result. Otherwise, the normal authentication steps are executed.

auth       sufficient   pam_succeed_if.so use_uid user ingroup postgres
Configure PAM to Allow Running Su Command without Password
Configure PAM to Allow Running Su Command without Password

Save the file and close it.

Next, add the user (for example aaronk) that you want to su to the account postgres without a password to the group postgres using usermod command.

$sudo usermod -aG postgres aaronk

Now try to su to the postgres account as the user aaronk, you should not be prompted for a password as shown in the following screenshot:

$ su - postgres
Add User to Group
Add User to Group

2. Using Sudoers File

You can also su to another user without requiring a password by making some changes in the sudoers file. In this case, the user (for example aaronk) who will switch to another user account (for example postgres) should be in the sudoers file or in the sudo group to be able to invoke the sudo command.

$ sudo visudo

Then add the following configuration below the line “%sudo ALL=(ALL:ALL) ALL” as shown in the following screenshot.

aaronk ALL=NOPASSWD: /bin/su – postgres
Add User to Sudoers File
Add User to Sudoers File

Save and close the file.

Now try to su to the account postgres as the user aaronk, the shell should not prompt you to enter a password:

$ sudo su - postgres
Switch to Other User Without Password
Switch to Other User Without Password

That’s all for now! For more information, see the PAM manual entry page (man pam.conf) and that of sudo command as well (man sudo).

$ man pam.conf
$ man sudo

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

4 Comments

Leave a Reply
  1. Note: when editing /etc/pam.d/su the place where you insert your rules is important! Do it right after ‘auth sufficient pam_rootok.so’ line at the top of the file, otherwise it will not work.

    Reply
  2. While passwordless login creates convenience, it also creates a security hole. Should a bad actor obtain your userid, (s)he can copy and/or destroy your entire database setup. As the database breaches at Equifax, Target, Yahoo, and hundreds of other companies show, hackers do not need any help, such as passwordless sign-ins, in breaking in.

    From a security standpoint, things should be made more complicated, rather than more convenient. Each user should have his or her own strong password so that in case of a breach it can be known which userid was compromised.

    The price of convenience is security.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.