Monday, February 14, 2022

Jenkins : Making Jenkins Run Using An Alternative Username

 

CONTEXT

I was once bothered big time by Jenkins and this thing of running in some sort of a context of its own. Given that it was running under its own user name and had it’s own Maven Repo and many more things I could not run a Maven build on some Libraries for one of the organisations I was consulting at.

 

PROBLEM

So I would SSH into the Linux server and try to run a Maven build and that would work but, as soon as I try automate the same in Jenkins, then I would have problems. It’s as if Jenkins was an alien on that server. I don’t know if it’s a normal thing for you as well but, it was really frustrating.

But hey, at the same time I was just finding my way around Jenkins. So after some time I learned about a few Jenkins directories. Let’s try sort that out and see if it will help you.

 

SOLUTION

So one of the several solutions is to try and change the user around Jenkins. Remember that out of the box and the moment Jenkins is running as as user, “jenkins” So you are probably running as “root” or some other username that’s not jenkins.

Assuming that you running as root let’s use the root example.

Mainly you need to assign specific jenkins directories to your username, in this case “root” or whatever user you want to assign this to which is probably safer, security wise. So let’s change the following directories as follows :

 

  • sudo chown -R root:root /var/lib/jenkins
  • sudo chown -R root:root /var/cache/jenkins
  • sudo chown -R root:root /var/log/jenkins

 

Once you are done you need to change the username that jenkins is using to your preferred one, in our case we are using “root”. Using a text editor of your choice look for the main Jenkins Profile, file at, /etc/default/jenkins open it and change the 

 

Jenkins Username Variable

 

As you can see, I have commented out the original JENKINS_USER=$NAME of which Jenkins was picking up it’s own username, which makes sense. I have also replaced this with our context username, “root”

The last thing you need to do is make sure that you save your changed and then restart your Jenkins. After all this you should be able to automate a few tasks that you could not automate given the whole username issue and Jenkins running inside its own world.

 

CONCLUSION

I hope this was helpful and you will find it useful as you are working with tools like Jenkins. Don’t be frustrated anymore hey. Just have more fun with these Software Toys! Cheers!

 

 

No comments:

Post a Comment