You often needs to execute shell scripts on the remote machine if you are using Jenkins to do CD to a remote server from your central server. Sometimes you must prefix your command with sudo in order to complete your deployment, here the problem comes, sudo will prompt you to enter password and this is what you can’t do from Jenkins.


  1. Add a dedicated user for Jenkins, eg: ci
sudo adduser USERNAME
sudo usermod -aG sudo USERNAME
  1. From your local machine, set up private key login.
    Note I’m on MacOS Mojave, this version somehow has a new public/private version, if you simply do ssh-keygen without the parameters below, Jenkins will not be able to use the private key. So if you’re on the latest MacOS, use the below command to generate your key.
ssh-keygen -m PEM -t rsa -b 4096
ssh-copy-id -i PUBLIC_KEY_PATH [email protected]
  1. Go back to your remote host
    Run sudo visudo
    Add a line at the END of the file.
    replace USERNAME with your ci user name.
  2. Save the file.

Now Jenkins should be able to run sudo command smoothly.

Comments are welcomed!

Leave a Reply

Your email address will not be published. Required fields are marked *