I wrote a script which downloads via scp and installs some packages. SSH keys are added to host and when I run

[root@HostB ~]# ./myscript

everything is ok and packages are downloaded via scp without asking for password.

I need to run this script via ssh but when I try

[root@HostA ~]# ssh root@HostB "./myscript"

I get Permission denied (publickey,password) error.

When I'm trying to use ssh -t I get a password enquiry in place when scp command in myscript is executing.

  • 2
    from HostA, can you ssh root@HostB ? Nov 3, 2014 at 20:18
  • do you have an SSH agent running on HostA? Nov 3, 2014 at 20:24
  • @glenn jackman: sure, there is also key added. Problem with permissions is during executing scp in script running on host B.
    – nars
    Nov 3, 2014 at 20:34
  • Do you get the same results if you use the full path to the script?
    – Centimane
    May 12, 2016 at 10:29
  • Your question is unclear. Your problem has nothing to do with myscript. What you actually need to figure out is why your scp commands complete without a password prompt when you run them locally, but ask for a password when you're running remotely. My guess is you're relying on an SSH agent for keys that is not being forwarded in the remote script execution case.
    – marcan
    May 12, 2016 at 11:36

6 Answers 6


Answer= Add "bash"

How to SSH into a Computer and Execute a Script from Jenkinsfile: The file is called "Shell Script.sh" in directory "/home/username/Shell Script Directory"

From Jenkinsfile:

sh "sudo ssh -i /home/user/myPemFile.pem -o StrictHostKeyChecking=no -n [email protected] \"bash /home/username/Shell\\ Script\\ Directory/Shell\\ Script.sh\" "

My guess would be because you don't have PasswordAuthentication set to yes.

PasswordAuthentication yes

Add this to the sshd_config on the machines and try again. This should prompt for password.

Another solution would be to take the contents of ~/.ssh/id_rsa.pub on HostA and add it to the file ~/.ssh/authorized_keys on the HostB

Now that you have password authentication enabled, you can install the authorized keys with this command:

ssh-copy-id HostB

You mention that you're using scp inside your script to download packages. Your problem is with that bit. The issue is that, when you run ssh in this form:

ssh user@host command

... there is no pseudo terminal allocated to the script that is running. scp tries to prompt for a password, but it needs a pseudo terminal for that, so without one, you get the permission denied error (which is coming from a failed password authentication attempt). If you use ssh -t, then there is a pseudo terminal, and the password prompt works. What you need to figure out is why scp is prompting for a password when run remotely, but not when run from an interactive ssh session. That's a different question.


Permission denied (publickey,password) error

Its got nothing to do with the permissions on the script. You are failing to establish an ssh connection. Try just getting a shell on the remote system first.

ssh -vv root@HostB

BTW it's generally not a good idea to allow direct access to the root account via ssh - and the error you have shown is what one would expect if the sshd is configured with PermitRootLogin=No. The right way to solve this is to provision an acocunt with appropriate sudo privileges and use that to run the command.


It sounds like it still wants you to input the password. When you SSH to HostB from HostA, does it ask for a password?

If it does, you will need to change your ssh_config file (/etc/ssh/sshd_config on RHEL/Centos) to have:

PasswordAuthentication no

If PasswordAuthentication is set to yes, it will always prompt you for a password.

  • No, this is backwards (and may lock you out of the host if you don't have public key auth set up). By disabling password authentication, you aren't adding any ways into a host, you're only locking ways out. If a password prompt appears, that is because other methods have already failed.
    – marcan
    May 12, 2016 at 11:49

Try this:

ssh root@hostb 'bash -s' < local-script.sh
  • OP's problem is about password being asked, not sending the script.
    – Archemar
    May 12, 2016 at 10:38
  • not related to the question.
    – AReddy
    May 12, 2016 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .