2013年4月9日 星期二

ssh using sudo without password

[MOTIVATION]
Sometimes we want to use ssh command to remote machine for some operations,
but it's inconvenient that we need to key the password for "sudo" command.
Especially when we need to create a shell script. We can't key the password directly on it.

[METHOD]
Step:

1. On the remote machine, execute
    $sudo visudo

    and add following line at the end( Change "hadoop" to your username)
    hadoop ALL=(ALL) NOPASSWD: ALL
   

    and Ctrl + X to leave


    type Y
   

    press ENTER to save and leave

2. Return and try
    $ ssh -t user@hostname "sudo ls"

    for example
    $ ssh -t hadoop@192.168.100.1 "sudo ls"

    If you did it right, it just doesn't need to key the password again!
    (And we could use it on shell script directly ~)

    [note]
    Why we use "-t" for ssh here is that it may cause:
    sudo: no tty present and no askpass program specified



[REFERENCE]
http://jeromejaglale.com/doc/unix/ubuntu_sudo_without_password
http://superuser.com/questions/117870/ssh-execute-sudo-command

沒有留言:

張貼留言