Jenkins Create Scheduled Builds Permission denied publickey

Hi @Ayman, @rahul456, and everyone,

Once again, being new to Jenkins I am again stuck.

On this particular task I am ending up with a permission denied error.

Steps that I have taken so far:

1 => Login to UI 

2 => update plugins

3 => install plugin `SSH Build Wrappers`

4 => Jenkins --> Manage Jenkins --> Credentials --> add credentials
      
      - add `banner` + `natasha` + passwords

5 => Jenkins --> Manage Jenkins --> configure system --> go to section ( ssh remote hosts) -- add hostnames + port 22 

       - stapp03
       - ststor01

6 => add users to sudoer file

   ssh banner@stapp03    BigGr33n
   # add
   sudo visudo
   banner ALL=(ALL) NOPASSWD: ALL

   ssh natasha@ststor01   Bl@kW 
   sudo visudo
   # add
   natasha ALL=(ALL) NOPASSWD: ALL
  
7 => Create a Jenkins jobs named `copy-logs`

=> click (in UI) => `build periodically`

# add
*/11 * * * *

8 => execute remote shell w/ssh

sudo mkdir /tmp/logs

sudo cp /var/log/httpd/access_log /tmp/logs

sudo cp /var/log/httpd/error_log /tmp/logs

sudo scp /tmp/logs/access_log -o StrictHostKeyChecking=no natasha@ststor01:/usr/src/sysops

sudo scp /tmp/logs/httpd/error_log -o StrictHostKeyChecking=no natasha@ststor01:/usr/src/sysops

sudo rm -rf /tmp/logs

Status: Permission denied publickey

Any help would be awesome! Thanks in advance to anyone who can help:-)

PS. I also tried:

echo "BigGr33n" | sudo mkdir /tmp/logs

echo "BigGr33n" | sudo cp /var/log/httpd/access_log /tmp/logs

echo "BigGr33n" | sudo cp /var/log/httpd/error_log /tmp/logs

echo "BigGr33n" | sudo chown -R jenkins:jenkins /tmp/logs

echo "BigGr33n" | sudo chmod -R 777 /tmp/logs

scp -r -o StrictHostKeyChecking=no /tmp/logs/access_log/* natasha@ststor01:/usr/src/sysops

echo "BigGr33n" | sudo rm -rf /tmp/logs

…which also failed to execute the script & copy the files

If you are supplying sudo password via shell command, then you need to use sudo -S option. For example, your command will look like this:
echo "BigGr33n" | sudo -S mkdir /tmp/logs

But I’m not sure if the ‘public key’ error you mentioned is because of this. But certainly, your sudo will not work in its current form.

1 Like

Thanks @phantomsg! I will take note of that.

One question:

Do you know why the -S tag is needed in this case?

Cheers:-)

Yes. -S allows sudo command to read password from stdin. See the Linux Man entry for sudo command

1 Like

Thank you @phantomsg for explaining that to me (and thanks for the link)! I get it now. :smile:

Hey @juliettet were you able to finish this task? DId you find out root cause for public_key error? I am stuck on that. Trying different ways but dont help.

Hi @oqtay.tasinov,

It’s been a while…I did finish the task. Can you give me a run down on which steps you have taken so far so that I can compare what you have with my notes to see what you may be missing?

Cheers:-)

Thanks for replying back @juliettet. I DM you please check. :slightly_smiling_face:

I have just messaged you:-)

@oqtay.tasinov Hi, Can you guys help me with where i went wrong with, as i see output is successful but the task failed.

  • Installed Plugin SSH Build wrappers
  • Added credentials
  • Created Job copy-logs
  • added build periodically according to question
    and execute below
    echo “BigGr33n”| sudo -S mkdir -p /tmp/logs1

echo “BigGr33n” | sudo -S cp /var/log/httpd/access_log /tmp/logs1

echo “BigGr33n” | sudo -S cp /var/log/httpd/error_log /tmp/logs1

echo “BigGr33n” | sudo -S chmod 777 /tmp/logs1

echo “BigGr33n” | sudo -S chown -R root:root /tmp/logs1

echo “BigGr33n” | sudo -S yum install sshpass -y

sshpass -p “Bl@kW” scp -o ‘StrictHostKeyChecking=no’ /tmp/logs1/access_log natasha@ststor01:/usr/src/data

sshpass -p “Bl@kW” scp -o ‘StrictHostKeyChecking=no’ /tmp/logs1/error_log natasha@ststor01:/usr/src/data

[SSH] executing…
[sudo] password for banner: Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile

[SSH] completed
[SSH] exit-status: 0

Finished: SUCCESS

The task is failed , don’t think i missed anything.

hi @sudheer, seems like you have completed the task.

1 Like

Hi @sudheer,

@rahul456 says that you have completed the task. Congratulations and great work!!

@rahul456 Yes, i did complete the task.

Thank you!

1 Like