1

I want to create a folder on a remote EC2 instance from another EC2 Instance and then copy some data into it as well.

I tried to create folder using JSch and passing command sudo mkdir /data but the error I get is sudo: sorry, you must have a tty to run sudo.Without sudo also, I am unable to create folder. I tried to use ((ChannelExec) channel).setPty(true) and by using this I can create the folder but afterwards I am unable to copy any data and even cant ssh the EC2 Instance from commandline .(if i create folder manualy then copying data is done successfully). can someone please guide me that what should be the way to do it.Thanks

waqas
  • 1,115
  • 5
  • 19
  • 32

2 Answers2

0

How about following example?

http://www.jcraft.com/jsch/examples/Sudo.java.html
ymnk
  • 1,145
  • 7
  • 7
  • http://stackoverflow.com/questions/11968805/use-of-jsch-sudo-example-and-channel-setpty-for-running-sudo-command-on-remote-h – waqas Aug 15 '12 at 12:06
0

I am not familiar with JSch. But if you have root access, you can update your sudoers configuration file to get around this. Add the following line to /etc/sudoers

Defaults:USERNAME_HERE !requiretty

Maybe someone else can elaborate on whether this is a bad idea or not, that's beyond the scope of my knowledge, but I'd love to know more?

I only use this approcah in one specific situation. We have a cronjob that backs up a cluster of remote servers via rsync, but for rsync to run successfully, it needs sudo privileges.

To get around this I did the following-

  1. Created a user "backupuser" - On both servers local & remote
  2. Added the following two lines to /etc/sudoers - Only needed on the server you want to grant sudo privileges to the user on. In our case, only on the remote server.

    backupuser ALL = NOPASSWD: /usr/bin/rsync
    DEFAULTS:backupuser !requiretty
    

Adding these two lines grants the user, 'backupuser' sudo privileges for rsync command without the need to enter a password and without the required tty connection.

Here's how it works-

The first line breaks down into two parts.

USER SPECIFICATION OPTION_TAG (: CONDITIONS(opt))
  1. USER SPECIFICATION - this sets the user that these options apply(s) too.

    backupuser
    
  2. OPTION_TAG - the tag for the option you what to grant. In this case, the user is granted sudo privileges without having the enter a password. (see man sudoers for a list of tags available)

    ALL = NOPASSWD
    
  3. CONDITIONS - You also have the option to place conditions on when to grant sudo privileges. In this case, the user only has sudo privileges to run the rsync command.

    : /usr/bin/rsync
    

The second line, overrides the default requirement of the sudo command that you need a terminal connection to run sudo (tty requirement). And it grants this privilege only to the user account 'backupuser'. (See man sudoers for the other DEFAULTS)

DEFAULTS:backupuser !requiretty

Hope this helps answer your question. I know it went on a bit of a tangent, but I wanted to give a full explanation. For more info you can also checkout man sudo

AlanZ
  • 135
  • 2
  • Alan thanks for the detailed answer but I think it wont work for me as I am creating EC2 instances on the run and cant change anything in /etc/sudoers without changing its permission settings and for that again I need to use sudo – waqas Aug 14 '12 at 13:37