How to run Jenkins under a different user in Linux

In /etc/sysconfig/jenkins change:

  1. Change the user that is used to start Jenkins from “Jenkins” user to “oraamp” user
    Change JENKINS_USER=“jenkins” to JENKINS_USER=“oraamp”
    JENKINS_PORT=“9080”
    Environment=“JENKINS_HOME=/var/lib/jenkins”
    chown -R oraamp:oraampg /var/cache/jenkins
    chown -R oraamp:oraampg /var/log/jenkins
  2. add JENKINS_USER=“oraamp” to /etc/systemd/system/jenkins.service.d/override.conf
    After the changes, restart jenkins service.
    When come back to Jenkins, the user is still showing “jenkins”

You’ll want to look at this page to see how to manage the systemd service. Using /etc/sysconfig/jenkins is not recommended if I remember correctly.

Hi slide_o_mix,
Thank you for the information. Yes, that’s how I did in number 2:
2. add JENKINS_USER=“oraamp” to /etc/systemd/system/jenkins.service.d/override.conf
After the changes, restart jenkins service: sudo systemctl restart jenkins

When come back to Jenkins, the user is still showing “jenkins”

Did you use

systemctl edit Jenkins

To edit the overrides?

When I try to open the file, it was saying “No files found for Jenkins.service”, but the file is there.
JENKINS_USER=“oraamp” was added from vi. I am trying to edit through systemctl edit Jenkins

[root@mcdasatest07 ~]# cd /etc/systemd/system/jenkins.service.d
[root@mcdasatest07 jenkins.service.d]# ls -ltr
total 4
-rw-r–r–. 1 root root 804 Aug 2 15:42 override.conf
[root@mcdasatest07 jenkins.service.d]# cat override.conf
[Unit]
Description=My Company Jenkins Controller
#Description=Jenkins on port 9080
[Service]
# Add JVM configuration options
Environment=“JAVA_OPTS=-Djava.awt.headless=true -XX:+UseStringDeduplication”

# Arbitrary additional arguments to pass to Jenkins.
# Full option list: java -jar jenkins.war --help
Environment=“JENKINS_OPTS=–prefix=/jenkins --javaHome=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64”

# Configuration as code directory
Environment=“CASC_JENKINS_CONFIG=/var/lib/jenkins/configuration-as-code/”

# Port to listen on for HTTP requests. Set to -1 to disable.
# To be able to listen on privileged ports (port numbers less than 1024),
# add the CAP_NET_BIND_SERVICE capability to the AmbientCapabilities
# directive below.
Environment=“JENKINS_PORT=9080”
JENKINS_USER=“oraamp”
[root@mcdasatest07 jenkins.service.d]# systemctl edit Jenkins /etc/systemd/system/jenkins.service.d/override.conf
No files found for Jenkins.service.
[root@mcdasatest07 jenkins.service.d]# systemctl edit Jenkins
No files found for Jenkins.service.
[root@mcdasatest07 jenkins.service.d]# systemctl edit Jenkins create /etc/systemd/system/jenkins.service.d/override.conf
No files found for Jenkins.service.
[root@mcdasatest07 jenkins.service.d]#

Be able to make change with “systemctl edit Jenkins”, user won’t change, it keeps staying with original user. no error

I also add a line JENKINS_USER=“oraamp”
to /etc/init.d/jenkins

You need to use lower case

systemctl edit jenkins

NOT Jenkins

use command below,
added
JENKINS_USER= “oraamp” or
Environment=“JENKINS_USER=oraamp”, no luck
command:
systemctl edit jenkins
systemctl restart jenkins

Are you trying to run as that user so that it has access to certain things? If so, it is recommended to run an agent as the user rather than running Jenkins as specific user. You can run an agent on the same machine even so you don’t need any additional compute resources.

Thank you, Alex.
the account we are using is oraamp, we use it for any other purpose.
jenkins user just use it when install Jenkins. after installation, we need to change it to oraamp.

what agent are you referring then? Any suggestion?

See this page for using agents Using Jenkins agents

I can try to install Docker agent.
But wondering why we Jenkins user cannot be changed.
I was able to update port using
systemctl edit jenkins /etc/systemd/system/jenkins.service.d/override.conf

I think you would need to change the service definition, rather than the Jenkins configuration. For example, something like this:

Make it works.
I am able to make change. Thanks Alex for your help.