Search code examples
linuxamazon-web-servicessshamazon-ec2scp

AWS Ec2 - Scp File Transfer Permission Denied


I have followed the documentation provided to be able to transfer local files to a directory found within the ec2 instance, but I keep getting a Permission denied with the path and file in my ec2 instance prepended to the error.

This shouldn't be an issue with credentials, because I ssh with the same commands without any issue. What I am curious about is possibly the file and folder permissions within my ec2 instance or locally and if that is preventing the transfer.

Sidenote: Not sure if this matters, but when I ssh with same credentials, I am brought to /home/ec2-user and when I run ls -a I don't see /var/app listed, but can cd into it without any issues and find my app.

Full error message:

scp: /var/app/current/config/pk-cfappkey.pem: Permission denied

Here is my scp command:

scp -vvv -i /Users/user/.ssh/app-key-pair /Users/user/Desktop/Projects/node/project/config/pk-cfappkey.pem ec2-user@ec2-[id].compute-1.amazonaws.com:/var/app/current/config

Here are my folder and file permissions

Ec2 Instance folder path:

/var/app/current/config
drwxr-xr-x  2 nodejs nodejs 4096 Oct  9 14:35 config

local file (pk-cfappkey.pem):

-rw-r--r--@ 1 user  staff  1706 Sep 24 15:09 pk-cfappkey.pem

Detailed error message (summarized):

debug1: Connecting to ec2-[id].compute-1.amazonaws.com [id] port 22.
debug1: Connection established.
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug1: Sending command: scp -v -t /var/app/current/config
debug2: channel 0: request exec confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
Sending file modes: C0644 1706 pk-cfappkey.pem
debug2: channel 0: rcvd ext data 45
Sink: C0644 1706 pk-cfappkey.pem
debug2: channel 0: written 45 to efd 8
scp: /var/app/current/config/pk-cfappkey.pem: Permission denied
debug2: channel 0: read<=0 rfd 6 len 0
debug2: channel 0: read failed
debug2: channel 0: close_read
debug2: channel 0: input open -> drain
debug2: channel 0: ibuf empty
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed

Solution

  • scp: /var/app/current/config/pk-cfappkey.pem: Permission denied
    

    Your user ec2-user does not have write access to the directory /var/app/current/config/. You should copy the file elsewhere:

    scp -vvv -i /Users/user/.ssh/app-key-pair \
      /Users/user/Desktop/Projects/node/project/config/pk-cfappkey.pem \
      ec2-user@ec2-[id].compute-1.amazonaws.com:
    

    then connect to the server:

    ssh ec2-user@ec2-[id].compute-1.amazonaws.com
    

    and move it to the proper location with appropriate sudo access:

    sudo cp pk-cfappkey.pem /var/app/current/config/