Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run remote ssh session from Jenkins with sudo rights?

Using 'Execute shell script on remote host using ssh' option and need sudo rights on remote server to change permissions and remove protected files. How to run session with this rights?

Getting message

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

when trying to run sudo command.

like image 373
Jokerius Avatar asked Oct 15 '25 20:10

Jokerius


2 Answers

To run sudo remotely you have 2 options

  1. Allow the user to run sudo commands without a password.

Append username ALL=(ALL) NOPASSWD: ALL the /etc/sudoers file with sudo visudo. Alternatively you can modify this line to only allow certain sudo commands to be run without a password

  1. Use the pseudo-tty to emulate tty remotely and enter your sudo password when requsted.

To do this run ssh -t username@host command_to_execute

like image 112
Eric Avatar answered Oct 18 '25 11:10

Eric


If the remote server accepts the direct login of the root user you can simply do:

ssh -l root yourserver command_to_execute

Similar syntax is:

ssh root@yourserver command_to_execute

Mind that allowing the login of the root user via ssh to a remote server isn't always a good solution. A better solution would be change the owner / permissions to allow a non-root user to modify the protected files.

like image 40
Atropo Avatar answered Oct 18 '25 09:10

Atropo