Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run jenkins as a different user

I have been trying to follow tutorials and this one: Deploy as Jenkins User or Allow Jenkins To Run As Different User?

but I still can't for the love of the computing gods, run as a different user. Here are the steps of what I did:

  1. download the macosx pkg for jenkins(LTS)
  2. setup plugins etc and git
  3. try to build it

I keep getting a can't clone error because jenkins keeps starting as anonymous:

Started by user anonymous

How do I set it up so that jenkins runs as me? I was using the jenkins web UI so it was in localhost:8080

I tried logging in also using /login but I can't even login using my name or as root.

The people tab doesn't even have a create user link, so yeah I've been stuck. Help please?

like image 496
corroded Avatar asked Jul 14 '11 11:07

corroded


People also ask

How do I run a Jenkins job as a different user?

To run a job by a specific user, you have to enable security options in jenkins. May be you have not used the enable security options in Jenkins and that is why it says started by anonymous user. You can create any number of users in Jenkins by providing their credentials.

How do you check which user is running Jenkins?

If you have access to the gui, you can go to "manage jenkins" > "system information" and look for "user.name".


2 Answers

The "Issue 2" answer given by @Sagar works for the majority of git servers such as gitorious.

However, there will be a name clash in a system like gitolite where the public ssh keys are checked in as files named with the username, ie keydir/jenkins.pub. What if there are multiple jenkins servers that need to access the same gitolite server?

(Note: this is about running the Jenkins daemon not running a build job as a user (addressed by @Sagar's "Issue 1").)

So in this case you do need to run the Jenkins daemon as a different user.

There are two steps:

Step 1

The main thing is to update the JENKINS_USER environment variable. Here's a patch showing how to change the user to ptran.

BEGIN PATCH
--- etc/default/jenkins.old     2011-10-28 17:46:54.410305099 -0700 +++ etc/default/jenkins 2011-10-28 17:47:01.670369300 -0700 @@ -13,7 +13,7 @@  PIDFILE=/var/run/jenkins/jenkins.pid   # user id to be invoked as (otherwise will run as root; not wise!) -JENKINS_USER=jenkins +JENKINS_USER=ptran   # location of the jenkins war file  JENKINS_WAR=/usr/share/jenkins/jenkins.war --- etc/init.d/jenkins.old      2011-10-28 17:47:20.878539172 -0700 +++ etc/init.d/jenkins  2011-10-28 17:47:47.510774714 -0700 @@ -23,7 +23,7 @@   #DAEMON=$JENKINS_SH  DAEMON=/usr/bin/daemon -DAEMON_ARGS="--name=$NAME --inherit --env=JENKINS_HOME=$JENKINS_HOME --output=$JENKINS_LOG -   -pidfile=$PIDFILE"  +DAEMON_ARGS="--name=$JENKINS_USER --inherit --env=JENKINS_HOME=$JENKINS_HOME --output=$JENKINS_LOG --pidfile=$PIDFILE"    SU=/bin/su 
END PATCH

Step 2

Update ownership of jenkins directories:

chown -R ptran /var/log/jenkins chown -R ptran /var/lib/jenkins chown -R ptran /var/run/jenkins chown -R ptran /var/cache/jenkins 

Step 3

Restart jenkins

sudo service jenkins restart 
like image 92
Peter Tran Avatar answered Oct 06 '22 12:10

Peter Tran


ISSUE 1:

Started by user anonymous

That does not mean that Jenkins started as an anonymous user.

It just means that the person who started the build was not logged in. If you enable Jenkins security, you can create usernames for people and when they log in, the

"Started by anonymous" 

will change to

"Started by < username >". 

Note: You do not have to enable security in order to run jenkins or to clone correctly.

If you want to enable security and create users, you should see the options at Manage Jenkins > Configure System.


ISSUE 2:

The "can't clone" error is a different issue altogether. It has nothing to do with you logging in to jenkins or enabling security. It just means that Jenkins does not have the credentials to clone from your git SCM.

Check out the Jenkins Git Plugin to see how to set up Jenkins to work with your git repository.

Hope that helps.

like image 37
Sagar Avatar answered Oct 06 '22 14:10

Sagar