1

I tried remote-exec and node-ssh packages, but both are resulting the same as mentioned below.

var rexec = require('remote-exec');

var connection_options = {
       port: 22,
       username: 'user', 
       privateKey: require('fs').readFileSync('privateKey/file.pem'),
       passphrase:''};

var hosts = [
    '54.xxx.xxx.xxx'
];

var cmds = [
   'sudo adduser testuser'
];

rexec(hosts, cmds, connection_options, function(err){

    if (err) {
        console.log(err);
    } else {
        console.log('Great Success!!');
    }
});

It returns the error :

[Error: 554.xxx.xxx.xxx : sudo adduser testuser [Exit 1]]

sudo: sorry, you must have a tty to run sudo

Please advise how to run sudo command using node js.

  • Probably this answer should help - [https://stackoverflow.com/a/5062718/7372769](https://stackoverflow.com/a/5062718/7372769) – Don Jun 30 '17 at 07:07
  • In `connection_options` you probably have to set `pty : true`, but if `sudo` on your server requires a password, it will most likely fail nonetheless. – robertklep Jun 30 '17 at 07:48
  • Thanks for quick response, finally i found the solution. – Venkatesh Kuppusamy Jun 30 '17 at 08:20
  • At least in `ssh2` if you pass `pty: true` in the `exec()` options, it will allocate a pseudo-TTY, which will allow you to write the user's password to the stream's stdin. – mscdex Jun 30 '17 at 15:56

1 Answers1

-1

Comment below line in /etc/sudoers file.

#Defaults requiretty

  • This answer can not be straightforward without any given scenario, some cases server does not have the /etc/sudoers – Abhradip Apr 13 '20 at 09:15