How to Setup a sudo Switch to Another User That Has no Password or ssh Key Set in Linux

This post outlines steps to setup a sudo access to a specific user to switch to another user that has no password of ssh key set.

As test user (having privilede to edit sudoers file), add to the sudoers file with visudo:

$ sudo visudo

the following line is added to sudoers file for the kirk user:

kirk ALL=(spock) NOPASSWD: ALL

With this setting the user ‘kirk’ when logged in can switch to user ‘spock’ even if it hasn’t a password or ssh key set:

$ sudo -iu spock
$ pwd
/home/spock

This will run all the shell profile scripts. If it is only required to change the user without running all the initial shell scripts use instead:

$ sudo -su spock
$ pwd
/home/kirk

Current directory, alias and other normally setting done in .bashrc (if using BASH) are kept.

This is similar to when a password is set and ‘su’ command is used. It can be “su – spock” or just “su spock”. In both these 2 it is required the spock user password.

Related Post