简体   繁体   English

使用Jsch在远程EC2实例上创建文件夹

[英]create folder on remote EC2 Instance using Jsch

I want to create a folder on a remote EC2 instance from another EC2 Instance and then copy some data into it as well. 我想在另一个EC2实例的远程EC2实例上创建一个文件夹,然后将一些数据也复制到其中。

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. 我试图使用JSch并传递命令sudo mkdir /data创建文件夹,但是我得到的错误是sudo: sorry, you must have a tty to run sudo没有sudo,我也无法创建文件夹。 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). 我尝试使用((ChannelExec) channel).setPty(true)并使用它可以创建文件夹,但是此后我无法复制任何数据甚至无法从命令行SSH EC2实例。(如果我手动创建文件夹,则复制数据已成功完成)。 can someone please guide me that what should be the way to do it.Thanks 有人可以指导我应该怎么做吗?谢谢

下面的例子怎么样?

http://www.jcraft.com/jsch/examples/Sudo.java.html

I am not familiar with JSch. 我对JSch不熟悉。 But if you have root access, you can update your sudoers configuration file to get around this. 但是,如果您具有root用户访问权限,则可以更新sudoers配置文件来解决此问题。 Add the following line to /etc/sudoers 将以下行添加到/ 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. 我们有一个cronjob,它通过rsync备份远程服务器集群,但是要使rsync成功运行,它需要sudo特权。

To get around this I did the following- 为了解决这个问题,我做了以下工作-

  1. Created a user "backupuser" - On both servers local & remote 创建了一个用户“ backupuser”-在本地和远程服务器上
  2. Added the following two lines to /etc/sudoers - Only needed on the server you want to grant sudo privileges to the user on. 在/ etc / sudoers中添加了以下两行-仅在要向其授予用户sudo特权的服务器上需要。 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. 添加这两行将为用户授予rsync命令“ backupuser”的sudo特权,而无需输入密码,也不需要所需的tty连接。

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. USER SPECIFICATION-这也使用户设置了这些选项。

     backupuser 
  2. OPTION_TAG - the tag for the option you what to grant. OPTION_TAG-您要授予的选项的标签。 In this case, the user is granted sudo privileges without having the enter a password. 在这种情况下,无需输入密码即可授予用户sudo特权。 (see man sudoers for a list of tags available) (有关可用标签的列表,请参阅man sudoers

     ALL = NOPASSWD 
  3. CONDITIONS - You also have the option to place conditions on when to grant sudo privileges. 条件-您还可以选择在何时授予sudo特权上设置条件。 In this case, the user only has sudo privileges to run the rsync command. 在这种情况下,用户只有sudo权限才能运行rsync命令。

     : /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). 第二行覆盖了sudo命令的默认要求,即您需要使用终端连接才能运行sudo(tty要求)。 And it grants this privilege only to the user account 'backupuser'. 并且它仅将此特权授予用户帐户“ backupuser”。 (See man sudoers for the other DEFAULTS) (有关其他默认值,请参见man sudoers

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 有关更多信息,您也可以结帐man sudo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM