# Handling Sudo Permissions in Jenkins Pipeline Agents: Best Practices and Guidance

**URL:** <https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911>\
**Category:** Ask a question\
**Tags:** question, pipeline\
**Created:** [May 8, 2025, 2:21pm UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911 "2025-05-08T14:21:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander.bohn](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexander.bohn](https://community.jenkins.io/u/alexander.bohn)\
**Post date:** [May 8, 2025, 2:21pm UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/1 "2025-05-08T14:21:39Z")

</div>

Hello Jenkins Community,

I don’t understand the recommended way to grant **sudo** permissions to an agent that executes pipeline steps requiring sudo access. Should I always provide the necessary permissions to specific steps using **withCredentials** function? The problem with **withCredentials** is that I do not understand how to propagate the provided password when running a shell command that prompts for a password after the function has been executed.

I have not been able to find clear information on what kind of permissions the agent user should have when executing pipeline steps. Should I always aim to develop pipelines that do not use commands requiring sudo permissions? Is this considered best practice?

Can anyone provide a link to the Jenkins documentation on how to handle sudo permissions in a Pipeline?

Best regards,  
Alexander Bohn

---

<div class="post-metadata">

**Author:** ![alexander.bohn](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexander.bohn](https://community.jenkins.io/u/alexander.bohn)\
**Post date:** [May 13, 2025, 2:21pm UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/2 "2025-05-13T14:21:33Z")

</div>

Hello Jenkins Community,

can anyone provide an answer to the questions?

---

<div class="post-metadata">

**Author:** ![davmano](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davmano/32/14810_2.png) [@davmano](https://community.jenkins.io/u/davmano)\
**Post date:** [May 13, 2025, 3:11pm UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/3 "2025-05-13T15:11:08Z")

</div>

Hi Alexander,

Great question on sudo in Jenkins pipelines! Best practice is to avoid sudo for security and portability, running agents as non-privileged users with minimal permissions. If sudo is needed, configure /etc/sudoers for passwordless execution of specific commands

```auto
jenkins ALL=(ALL) NOPASSWD: /usr/bin/specific_command

```

Using withCredentials for sudo passwords is tricky, as it doesn’t handle interactive prompts well. You can try:

```auto
withCredentials([usernamePassword(credentialsId: 'sudo-creds', passwordVariable: 'PASS')]) {
    sh 'echo $PASS | sudo -S specific_command'
}

```

However, this is less secure; use sudoers instead. Ensure the agent user has workspace access and limit “Configure Agents” permissions to admins. Update Credentials/Pipeline plugins for secure handling in Jenkins 2.504.1 (Java 21-compatible).

**Docs** :

- [Credentials in Pipelines](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials)
- [Security Management](https://www.jenkins.io/doc/book/security/managing-security/)

Best, David Mano

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [May 13, 2025, 3:24pm UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/4 "2025-05-13T15:24:10Z")

</div>

As sudo is a dangerous command it should be only be used when there is no other way.  
If it is just a single command that needs to be executed you could consider creating a script for that command and explicitly put that script in the sudoers file and allow the user that runs the Jenkins agent to execute this script (and only this script) via sudo without even asking for a password.  
If you need more flexibility you will need to pass the password for sudo on each execution.  
Usually sudo is setup so that once you provided the password it will not ask again for the password for 1 or 2 minutes.  
So the following should work

```auto
withCredentials([string(credentialsId: 'sudo', variable: 'SUDO_PW')]) {
  sh ''' 
    echo $SUDO_PW | sudo -v -S
    sudo mycommand ...
  '''
}

```

The exact parameters for sudo might depend on the OS  
Beware what you execute with sudo, e.g. it should not be any parameter that a user can provide to run the job.

---

<div class="post-metadata">

**Author:** ![alexander.bohn](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexander.bohn](https://community.jenkins.io/u/alexander.bohn)\
**Post date:** [May 14, 2025, 6:49am UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/5 "2025-05-14T06:49:24Z")

</div>

Hello David and Markus,

Thank you for the answers you provided.

Best regards,  
Alexander Bohn

---

<div class="post-metadata">

**Author:** ![Artalus](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/artalus/32/11413_2.png) [@Artalus](https://community.jenkins.io/u/Artalus)\
**Post date:** [May 14, 2025, 8:17am UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/6 "2025-05-14T08:17:01Z")

</div>

> [@alexander.bohn](#):
>
> I have not been able to find clear information on what kind of permissions the agent user should have when executing pipeline steps.

There are no hard requirements on this from Jenkins side. Jenkins on its own is perfectly capable of running without `sudo` priveleges – at least unless you add Docker to the equation.

> [@alexander.bohn](#):
>
> Should I always aim to develop pipelines that do not use commands requiring sudo permissions? Is this considered best practice?

If you can get away without needing `sudo` – that’s great, and striving to achieve such state is a noble goal. Both at `$previousJob` and `$currentJob` we had to grant `sudo` access to the agent user, since there was just too many legacy stuff that required it.  
Generally speaking, I would say that it all depends upon where you draw the line of _“if the malicious gets to this point, we are already screwed”_ – in both cases for us that was waaay before the Jenkins.

If you decide to go this way – do invest in VMs for your agents, configured-as-code in such way that you’d be able to recreate one from scratch in no time. So that if an accidental `sudo rm -rf /` would nuke a precious snowflake buildmachine, which is the only one capable of some arcane tasks, you’d halt only for a few hours instead of few days.

---

<div class="post-metadata">

**Author:** ![alexander.bohn](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexander.bohn](https://community.jenkins.io/u/alexander.bohn)\
**Post date:** [May 15, 2025, 6:23am UTC](https://community.jenkins.io/t/handling-sudo-permissions-in-jenkins-pipeline-agents-best-practices-and-guidance/30911/7 "2025-05-15T06:23:01Z")

</div>

Hello Artalus,

Thank you for your answer and for joining the discussion.

Best regards,  
Alexander Bohn
