How to run a script as root in Jenkins?
Asked Answered
L

5

60

I need to run a shell script in Jenkins as root instead of the default user. What do I need to change?

My sudoers file is like this:

# User privilege specification
root    ALL=(ALL) ALL
igx     ALL=(ALL) ALL
%wheel  ALL=(ALL) ALL

# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move
%sudo   ALL=(ALL) ALL
#
#includedir /etc/sudoers.d

# Members of the admin group may gain root privileges
%admin  ALL=(ALL) NOPASSWD:ALL
root    ALL=(ALL) ALL
jenkins ALL=NOPASSWD: /var/lib/jenkins/workspace/ing00112/trunk/source/
jenkins ALL=(ALL) NOPASSWD:ALL
#Defaults:jenkins !requiretty
Lula answered 9/8, 2012 at 9:8 Comment(1)
possible duplicate of how to run jenkins as super user?Seumas
M
99

You must run the script using sudo:

sudo /path/to/script

But before you must allow jenkins to run the script in /etc/sudoers.

jenkins    ALL = NOPASSWD: /path/to/script
Milliner answered 9/8, 2012 at 9:10 Comment(8)
@Igore Thanks ... You are 100% correct ... And i used expect command to run script as a super user :)Lula
If you want to execute shell script change to: "jenkins ALL=NOPASSWD: /bin/sh, /path/to/script" and then you can run "sudo sh /path/to/script"Jugum
for use only a command the link atrixnet.com/… can help; im my case I needed to use not a script but a command 'scp'; you tips were helpful too; thxInsufficient
That executes an script as sudo but not as root, which is not the same for some cases.Licht
Do you mean /etc/sudoers.d/ directory? I only find a README file there... where do I add that line?Enfranchise
@nirvanaswap: sudoers.d is a directory with files that are automatically included in /etc/sudoersMilliner
@Igor Chubin's answer is 100% correct, but never open sudoer file with a normal editor. always use visudo sudo visudo. This will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.f you make an error in sudoer file, you will lose sudo access, so always use visudoDotson
@contactlp: But I didnt't state that we use a normal editot to edit /etc/sudoers. I just said that we need this line in this fileMilliner
D
21

@Igor Chubin's answer is 100% correct, but never open sudoer file with a normal editor. always use visudo

just type

sudo visudo

this will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.

if you make an error in sudoer file, you will lose sudo access, so always use visudo

Dotson answered 29/3, 2017 at 14:34 Comment(0)
W
8

I do realise I'm late to the party on this question, but for reference sake I thought I'd throw my 2c in here: I use the SSH plugin for Jenkins to accomplish this (simply configure a localhost target). In this way, I can contain the script directly within Jenkins (like a normal "Execute shell" step), instead of using sudo to invoke an external script.

Wickerwork answered 6/11, 2015 at 19:9 Comment(0)
C
2

Try adding jenkins user to sudo group

sudo su -
usermod -a -G sudo jenkins
Chervonets answered 15/6, 2020 at 11:17 Comment(0)
E
2

Easy way to do it
enter image description here

$ sudo visudo
## Now add the below lines in your sudoers file :
jenkins ALL=(ALL) NOPASSWD: ALL

$service jenkins start
Edelstein answered 21/1, 2021 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.