To run sudo commands on a ec2 instance

2019-06-22 13:15发布

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.

2条回答
何必那么认真
2楼-- · 2019-06-22 13:31

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.

查看更多
Lonely孤独者°
3楼-- · 2019-06-22 13:43

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.)

查看更多
登录 后发表回答