How to run a script as root in Jenkins?

前端 未结 4 1661
小蘑菇
小蘑菇 2020-12-07 15:49

I need to run a shell script in Jenkins as root instead of the default user. What do I need to change?

My sudoers file is like this:

# User privilege         


        
相关标签:
4条回答
  • 2020-12-07 16:17

    @Igor Chubin's answer is 100% correct, but never open sudoer file with a normal editor. always use visudo

    just type

    sudo visudo
    

    this will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.

    if you make an error in sudoer file, you will lose sudo access, so always use visudo

    0 讨论(0)
  • 2020-12-07 16:28

    I do realise I'm late to the party on this question, but for reference sake I thought I'd throw my 2c in here: I use the SSH plugin for Jenkins to accomplish this (simply configure a localhost target). In this way, I can contain the script directly within Jenkins (like a normal "Execute shell" step), instead of using sudo to invoke an external script.

    0 讨论(0)
  • 2020-12-07 16:30

    Try adding jenkins user to sudo group

    sudo su -
    usermod -a -G sudo jenkins
    
    0 讨论(0)
  • 2020-12-07 16:33

    You must run the script using sudo:

    sudo /path/to/script
    

    But before you must allow jenkins to run the script in /etc/sudoers.

    jenkins    ALL = NOPASSWD: /path/to/script
    
    0 讨论(0)
提交回复
热议问题