What I wanted to do is: run a bash script from my local terminal, to run a jar file in an EC2 instance. But in order to run the script, I'll have to first log in with .pem credentials, and then do su admin and type admin password to gain run access. The command I've figured out so far is like this:

ssh -i ~/.ssh/my_credentials.pem -t ${EC2IP} 'su admin' "ADMIN_PASSWORD" 'cd /tmp' 'java -jar my-program.jar'

But it's still prompting me to type the ADMIN_PASSWORD, and then after that, my terminal will be logged into that EC2 machine, which is not what I wanted, I just want to finish all of the commands, and then continue with another EC2 instance.

How could I achieve that please? Thanks a lot!


there is no proper solution to your problem. Maybe you can change some security rules in /etc/pam.d/su. But I am not sure if and how.

I know why you cant use ssh in combination with su...
su is intended to run on the server terminal, or with other words:
su needs the correct TTY to communicate with.

Passing a password to su also doesnt work.
Most programms that use a password have a option to put the passowrd in it,
unfortunately su use promt only.

A NOT recommended method would be this.

You could avoid using su by connecting via ssh and username admin,
or giving the rights to the user that you were using in the first place.

Hope this gets you going.

  • Not quite accurate that there exists no possible approach -- with expect for instance, one has an emulated TTY that can be scripted. – Charles Duffy Mar 8 '17 at 22:24
  • updated my post – suleiman Mar 8 '17 at 22:25
  • (stackoverflow.com/questions/4780893/…) <-- not recommended – suleiman Mar 8 '17 at 22:42
  • Definitely agreed that expect is the Wrong Thing -- was just making a point that the original language speaking in terms of *possibilities" was inaccurate. – Charles Duffy Mar 8 '17 at 22:47
  • oops, ok. edited my post again. thx for the hint. – suleiman Mar 8 '17 at 22:54

A. Don't do this. Use sudo instead. You'll probably need to comment out the following line from /etc/sudoers on the target:

Defaults    requiretty

B. Assuming that you're going to do this anyway, use expect to do it for you: http://www.tcl.tk/man/expect5.31/expect.1.html

But, seriously, don't do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.