How to run Jenkins as a different user

By default, the Jenkins app runs as user jenkins:jenkins.  This is sometimes well and good, but if you need to run shell commands from within Jenkins as part of a build, generally running the process with user jenkins as the owner causes permissions issues on the filesystem.  So, here is how to change the user that runs it.  I’m using CentOS 6.2.

The jenkins init script in /etc/init.d/jenkins exposes the $JENKINS_USER and $JENKINS_CONFIG environment variables.  While you could simply change the $JENKINS_USER variable within that script, it would be quite confusing to have a mismatch between what appears in the config file and what actually happens.  The correct place to change this is wherever the $JENKINS_CONFIG points.  By default it points to /etc/sysconfig/jenkins.

When editing JENKINS_USER within the Jenkins config file, right above that it warns you that you also need to fix ownership on /var/log/jenkins.  That’s true, but it’s not the whole story. This is what needs to be changed.  If your Jenkins instance is not installed to /var/lib, then adjust the path as needed.

chown -R user:group /var/lib/jenkins
chown -R user:group /var/log/jenkins
chown -R user:group /var/cache/jenkins

Once you’ve made those changes, a restart of Jenkins should change the user for you

service jenkins restart
ps -ef | grep jenkins


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s