IT

ssh를 극복하는 적절한 방법

lottoking 2020. 6. 28. 17:58
반응형

ssh를 극복하는 적절한 방법


sudo를 사용하여 원격 서버에서 SSH를 통해 다른 스크립트를 실행하는 스크립트가 있습니다. 그러나 암호를 입력하면 터미널에 표시됩니다. (그렇지 않으면 잘 작동합니다)

ssh user@server "sudo script"

입력하는 동안 비밀번호가 표시되지 않고 SSH를 통해 sudo의 비밀번호를 입력 할 수 있도록이를 수행하는 올바른 방법은 무엇입니까?


다른 방법은 -t스위치를 사용하여 다음 을 수행하는 것입니다 ssh.

ssh -t user@server "sudo script"

참조 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.

다음 명령으로 완전히 자동화 할 수있었습니다.

echo pass | ssh -tt user@server "sudo script"

장점 :

  • 비밀번호 프롬프트 없음
  • 원격 컴퓨터 bash 기록에서 암호를 표시하지 않습니다

에 대해서는 보안 :커트는 말했다,이 명령을 실행하면 해당 지역의 bash는 역사에 대한 암호를 보여주고, 그것의 .sh 파일에있는 모든 명령을 다른 파일이나 저장 암호를 저장하고 실행하는 것이 좋습니다 것입니다. 참고 : 허용 된 사용자 만 액세스 할 수 있도록 파일에 올바른 권한이 있어야합니다.


비밀번호 프롬프트를 원하지 않는다고 가정합니다 .

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

ssh me@localhost 'echo secret | sudo -S echo hi' # outputs 'hi'

비밀번호를 전달하는 SSH를 통한 Sudo는 tty가 필요하지 않습니다.

sudo에게 대화식 암호가 필요하지 않고 stdin에서 암호를 가져 오도록 지시하여 ssh에 의사 tty (ssh "-t"스위치를 사용하지 않고)를 갖지 않고 ssh를 통해 sudo를 사용할 수 있습니다. sudo의 "-S"스위치를 사용하여이 작업을 수행합니다. 이로 인해 sudo는 stdin에서 비밀번호를 청취하고 개행이 표시되면 청취를 중지합니다.

예 1-간단한 원격 명령

이 예에서는 간단한 whoami명령 을 보냅니다 .

$ ssh user@server cat \| sudo --prompt="" -S -- whoami << EOF
> <remote_sudo_password>
root

우리는 sudo에게 프롬프트를 발행하지 말고 stdin으로부터 입력을 받도록 지시하고 있습니다. 이렇게하면 sudo 비밀번호가 완전히 자동으로 전달되므로 사용자가받는 유일한 응답은의 출력입니다 whoami.

이 기술은 stdin 입력이 필요한 ssh over sudo를 통해 프로그램을 실행할 수 있다는 이점이 있습니다. sudo가 stdin의 첫 번째 줄에서 암호를 사용하고 있기 때문에 실행중인 프로그램이 stdin을 계속 잡을 수 있기 때문입니다.

예 2-자체 stdin이 필요한 원격 명령

다음 예에서, 원격 명령 "cat"은 sudo를 통해 실행되며 원격 고양이가 표시하기 위해 stdin을 통해 추가 라인을 제공합니다.

$ ssh user@server cat \| sudo --prompt="" -S -- "cat" << EOF
> <remote_sudo_password>
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2

출력은 <remote_sudo_password>라인이 sudo에 의해 소비되고 있으며 원격으로 실행 된 cat이 여분의 라인을 표시하고 있음을 보여줍니다.

이것이 유리한 곳의 예는 ssh를 사용하여 명령 행을 사용하지 않고 권한있는 명령에 암호를 전달하려는 경우입니다. ssh를 통해 원격 암호화 컨테이너를 마운트하려면

예 3-원격 VeraCrypt 컨테이너 마운트

이 예제 스크립트에서는 별도의 프롬프트 메시지없이 sudo를 통해 VeraCrypt 컨테이너를 원격으로 마운트합니다.

#!/bin/sh
ssh user@server cat \| sudo --prompt="" -S -- "veracrypt --non-interactive --stdin --keyfiles=/path/to/test.key /path/to/test.img /mnt/mountpoint" << EOF
SudoPassword
VeraCryptContainerPassword
EOF

It should be noted that in all the command-line examples above (everything except the script) the << EOF construct on the command line will cause the everything typed, including the password, to be recorded in the local machine's .bash_history. It is therefore highly recommended that for real-world use you either use do it entirely through a script, like the veracrypt example above, or, if on the command line then put the password in a file and redirect that file through ssh.

Example 1a - Example 1 Without Local Command-Line Password

The first example would thus become:

$ cat text_file_with_sudo_password | ssh user@server cat \| sudo --prompt="" -S -- whoami
root

Example 2a - Example 2 Without Local Command-Line Password

and the second example would become:

$ cat text_file_with_sudo_password - << EOF | ssh va1der.net cat \| sudo --prompt="" -S -- cat
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2

Putting the password in a separate file is unnecessary if you are putting the whole thing in a script, since the contents of scripts do not end up in your history. It still may be useful, though, in case you want to allow users who should not see the password to execute the script.


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


The best way is ssh -t user@server "sudo <scriptname>", for example ssh -t user@server "sudo reboot". It will prompt for password for user first and then root(since we are running the script or command with root privilege.

I hope it helped and cleared your doubt.


echo $VAR_REMOTEROOTPASS | ssh -tt -i $PATH_TO_KEY/id_mykey $VAR_REMOTEUSER@$varRemoteHost 
echo \"$varCommand\" | sudo bash

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!


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.

참고URL : https://stackoverflow.com/questions/10310299/proper-way-to-sudo-over-ssh

반응형