This guide explains how to switch between users without a password in Linux.
Switching users in Linux usually requires entering a password. However, there are safe ways to configure your system to allow certain users to switch accounts without a password prompt. This can be useful for administrative scripts, automation, or trusted environments.
Using ‘sudo’ Without a Password
A common method is configuring sudo to allow a specific user-switching command without a password. Access should be restricted to only the commands required, following the principle of least privilege.
- Edit the sudoers file
Open the sudoers file using the visudo command:sudo visudo
- Add a passwordless entry
Add the below line at the termination of the file:username ALL=(targetuser) NOPASSWD: /bin/su - targetuser
- username ? The current user.
- targetuser ? The user you want to switch to.
- Warning Avoid using:
username ALL=(targetuser) NOPASSWD: ALL
This configuration allows the specified user to execute any command as targetuser without authentication.
If targetuser is root or another privileged account, this effectively grants full administrative access and may introduce privilege-escalation risks.
Whenever possible, restrict NOPASSWD permissions to the specific command required for user switching.
- Switch user
Now you can switch users without entering a password:sudo -u targetuser -i
Or:
sudo su - targetuser
These commands should only be permitted through narrowly scoped sudoers rules. Avoid granting NOPASSWD: ALL unless unrestricted access has been explicitly approved.
Using ‘su’ With Passwordless Access (Optional)
You can let ‘su’ switch users without a password by changing PAM (Pluggable Authentication Modules). Warning: This process is less protected.
- Backup PAM configuration
sudo cp /etc/pam.d/su /etc/pam.d/su.bak
- Edit PAM configuration
Open the file:sudo nano /etc/pam.d/su
Comment out the line:
auth required pam_wheel.so
Or adjust it according to your distro to allow specific users.
Note: This is not recommended on production systems due to security risks.
Using SSH Key-Based Authentication (For Remote Switching)
If switching users remotely via SSH:
- Generate SSH keys
ssh-keygen -t rsa
- Copy the public key to the target user:
ssh-copy-id targetuser@localhost
- Switch user via SSH without a password:
ssh targetuser@localhost
Security Considerations:
- Only grant passwordless access to trusted users.
- Avoid enabling passwordless su in production environments.
- Prefer narrowly scoped sudoers rules that allow only the specific command required for user switching.
Avoid NOPASSWD: ALL because it grants unrestricted command execution as the target account and can lead to privilege escalation.
- Always back up configuration files before editing.
This way, you can switch between users without a password in Linux. Hope you liked our article. If you want additional assistance, feel free to contact our support staff.
Want easier access too? Learn How to Enable Passwordless SSH Login in Linux
A Linux VPS Hosting solution gives you full control over user access, account management and administrative privileges in a secure server environment.