Search code examples
linuxshelljenkinsroot

Run shell command in jenkins as root user?


I have recently started using Jenkins for integration. All was well until I was running jobs on master node without shell command but I have to run jobs on master as well as slave node which contains shell commands to. I am not able to run those shell commands as root user. I have tried

  1. Using SSH Keys.
  2. Setting user name in shell commands.
  3. Using sudo.

I am getting permission denied error every time I use any of the above methods.


Solution

  • You need to modify the permission for jenkins user so that you can run the shell commands. You can install the jenkins as as service (download the rpm package), You might need to change the ports because by default it runs http on 8080 and AJP on 8009 port.



    Following process is for CentOS
    1. Open up the this script (using VIM or other editor):

    vim /etc/sysconfig/jenkins
    

    2. Find this $JENKINS_USER and change to “root”:

    $JENKINS_USER="root"
    

    3. Then change the ownership of Jenkins home, webroot and logs:

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

    4) Restart Jenkins and check the user has been changed:

    service jenkins restart
    ps -ef | grep jenkins
    

    Now you should be able to run the Jenkins jobs as the root user and all the shell command will be executed as root.