proper way to sudo over ssh

后端 未结 9 1681
抹茶落季
抹茶落季 2020-11-28 20:33

I have a script which runs another script via SSH on a remote server using sudo. However, when I type the password, it shows up on the terminal. (Otherwise it works fine)

相关标签:
9条回答
  • 2020-11-28 20:58
    echo $VAR_REMOTEROOTPASS | ssh -tt -i $PATH_TO_KEY/id_mykey $VAR_REMOTEUSER@$varRemoteHost 
    echo \"$varCommand\" | sudo bash
    
    0 讨论(0)
  • 2020-11-28 20:58

    Depending on your usage, I had success with the following:

    ssh root@server "script"
    

    This will prompt for the root password and then execute the command correctly.

    0 讨论(0)
  • 2020-11-28 20:59

    Another way is to use the -t switch to ssh:

    ssh -t user@server "sudo script"
    

    See man ssh:

     -t      Force pseudo-tty allocation.  This can be used to execute arbi-
             trary screen-based programs on a remote machine, which can be
             very useful, e.g., when implementing menu services.  Multiple -t
             options force tty allocation, even if ssh has no local tty.
    
    0 讨论(0)
  • 2020-11-28 20:59

    I faced a problem,

    user1@server1$ ssh -q user1@server2 sudo -u user2 rm -f /some/file/location.txt
    
    Output:
    sudo: no tty present and no askpass program specified
    

    Then I tried with

    #1
    vim /etc/sudoers
    Defaults:user1    !requiretty
    

    didn't work

    #2
    user1   ALL=(user2)         NOPASSWD: ALL
    

    that worked properly!

    0 讨论(0)
  • 2020-11-28 21:01

    Assuming you want no password prompt:

    ssh $HOST 'echo $PASSWORD | sudo -S $COMMMAND'
    

    Example

    ssh me@localhost 'echo secret | sudo -S echo hi' # outputs 'hi'
    
    0 讨论(0)
  • 2020-11-28 21:14

    NOPASS in the configuration on your target machine is the solution. Continue reading at http://maestric.com/doc/unix/ubuntu_sudo_without_password

    0 讨论(0)
提交回复
热议问题