How to make passwordless switch to another user in a shell script

21.3k views Asked by At

I want to su from hadoopmaster to hduser. When I do it on the terminal, I do:

su - hduser

which then asks me for the password and I enter it and I get in to hduser. How can I do the same in a shell script without having to worry about the password? I tried the sudoers file method where I put:

root ALL=(ALL:ALL) ALL
hduser ALL=(ALL:ALL) ALL
hadoopmaster ALL=(ALL:ALL) NOPASSWD: /bin/su hduser 
(here I tried different combinations of the line with and without the '-' and also 
with and without /bin)

I did this and when I do su hduser or su - hduser, it prompts me for the password again. What do I do?

2

There are 2 answers

2
Jim Nutt On BEST ANSWER

You don't use "su" with the sudoers file, you need to use "sudo". So, you'd want a command line like:

sudo su - hduser

which would do want you want, provided you had the appropriate lines in the sudoers file. A line like this:

hadoopmaster ALL=(ALL:ALL) NOPASSWD: su - hduser

should do the trick.

1
Ranjithkumar T On

I have add user= ranjith on /etc/sudoers file like,

 ranjith ALL=(ALL)       NOPASSWD: ALL

Now we can use,

 "sudo, sudoedit - execute a command as another user"

 [ranjith@ranjith ~]$ sudo -i
 # id
 uid=0(root) gid=0(root) groups=0(root)

Its working for me and Now i can login to root from my local user "ranjith" directly.