5

I cannot run "sudo su" on my ec2 client , I ssh into the client through a java program and run the command through a program. I can run commands like "ls" and "ifconfig" though.

I get an error saying "sudo: sorry, you must have a tty to run sudo".

How can I run the command, I am using Jsch for ssh to my ec2 instance.

Jaimin Shah
  • 271
  • 1
  • 4
  • 9

2 Answers2

6

You can do "sudo" without pseudo-tty with JSch, by using the -S option to sudo.

See the Sudo.java on the JSch website for a complete example.

(I'm the author of JSch.)

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
ymnk
  • 1,145
  • 7
  • 7
4

Looks like by default Jsch doesn't allocate a pseudo-tty for the remote session - this can break some programs that expect to be run interactively. It looks like you might be able to use the ChannelExec.setPty function to request a pty - but I've not worked with Jsch and can't test this myself.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • Just a note: I created some Javadocs for JSch, you might want to link http://epaul.github.com/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelExec.html#setPty(boolean) instead. – Paŭlo Ebermann Dec 10 '11 at 12:16
  • @all, I am using ChannelExec.setPty but afterwards I fail to ssh and get the message "Read from socket failed: Connection reset by peer". Can you please guide me what I am doing wrong? – waqas Aug 08 '12 at 13:20