如何在shell脚本中进行无密码切换到另一个用户

Cod*_*les 3 linux ubuntu sudo sudoers ubuntu-12.04

我想从hadoopmaster到hduser.当我在终端上这样做时,我做:

su - hduser
Run Code Online (Sandbox Code Playgroud)

然后我要求输入密码,然后我输入密码,然后进入hduser.如何在shell脚本中执行相同操作而不必担心密码?我尝试了sudoers放置的文件方法:

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)
Run Code Online (Sandbox Code Playgroud)

我这样做,当我这样做su hduser或者su - hduser,它再次提示我输入密码.我该怎么办?

Jim*_*utt 8

你不要在sudoers文件中使用"su",你需要使用"sudo".所以,你需要一个命令行,如:

sudo su - hduser

只要你在sudoers文件中有适当的行,你会想要你想要的.像这样的一行:

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

应该做的伎俩.