1

I have checked many postings regarding 'Permission denied' error in using scp, but couldn't find the answer for my problem.

There are two ubuntu servers (let's say server 'A' and 'B') on the same network in AWS, and when I tried to copy one file to another server, A to B is not working, but from B to A works. (Please see below)

On Server 'A',

ubuntu@server-a ~ $ ls -alt server*
-rwxr-xr-x 1 ubuntu ubuntu 8152 Aug  9 14:26 server.xml.bak

ubuntu@server-a ~ $ scp -P 443 server.xml.bak ubuntu@172.31.34.55:/home/ubuntu/
Permission denied (publickey).
lost connection

ubuntu@server-a ~ $ scp -P 443 /home/ubuntu/server.xml.bak ubuntu@172.31.34.55:/home/ubuntu/
Permission denied (publickey).
lost connection

Also I tried to copy the remote file to here, and it failed as well.

ubuntu@server-a ~ $ scp -P 443 ubuntu@172.31.34.55:/home/ubuntu/sakila.sql .
Permission denied (publickey).

But from Server 'B', all worked.

ubuntu@server-b ~ $ scp -P 443 ubuntu@172.31.45.112:/home/ubuntu/server.xml.bak .
server.xml.bak                                                    100% 8152     8.0KB/s   00:00

ubuntu@server-b ~ $ scp -P 443 sakila.sql ubuntu@172.31.45.112:/home/ubuntu/
sakila.sql                                                        100% 3153KB   3.1MB/s   00:00

As you see, it is not the permission issue - all operations were done by 'ubuntu' on 'ubuntu' home and files are also owned by 'ubuntu' with 755 mode.

So now I'm confused what is wrong.

  • Just to make sure, have you generated/copied a SSH private key on server A for user 'ubuntu' and then exported the corresponding public key counterpart to server B? – lgeorget Aug 9 '18 at 15:37
  • The permission error comes from failed authentication, not from file permissions. Is there an SSH daemon listening on that port on the server? – Kusalananda Aug 9 '18 at 15:51
  • @lgeorget, I got a hint from your comment - I ran the command with scp -v and found it's due to missing priv key. For security reason I removed all priv keys on servers and use Pageant instead for priv key management. I've connected B (bastion server), and opened another PuTTY terminal, connected B there again, and then connect A via ssh. So B to A works (Pageant manages priv key for B) but A to B didn't work... I upload priv key to A for testing and then scp command from A also works. Since I can't keep priv key on servers, I think I should only use scp command on B. – Ikhoon Chon Aug 9 '18 at 16:26
  • Glad you could solve your problem! You can answer your own question and mark the answer as accepted in case it helps someone else. – lgeorget Aug 10 '18 at 7:37
2

As suggested, I move my original comment to the answer with more details in case anyone may have a similar issue.

I got a hint from @lgeorget 's comment - I ran the command with scp -v and found it's due to missing private key.

For security reason, I removed all private keys on servers and use Pageant instead for private key management. In this scenario, I connected to B (B is a bastion server) using PuTTY, opened another PuTTY terminal, connected to B again, and then connected to A via ssh. Therefore the Pageant can handle the private key for B but cannot for A directly (I couldn't recognize this)
The result is, scp command from B to target server A works but scp from A to target server B doesn't work.

I upload the private key to A for testing and then scp command from A also works fine.

Since I can't keep private keys on servers, I think I should only use scp command on server B.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.