97

I try to transfer files from remote computer using ssh to my computer :

scp My_file.txt user_id@server:/Home

This should put My_file.txt in the home folder on my own computer, right? I get

scp/Home: permission denied

Also when I try: ...@server:/Desktop, in order to copy the files from the remote computer to my desktop.

What am I doing wrong?

1
  • 3
    If you want to copy the file to the home folder of 'user_id' use scp My_file.txt user_id@server: It copies to the users home folder. Sep 12, 2012 at 9:42

16 Answers 16

113

Your commands are trying to put the new Document to the root (/) of your machine. What you want to do is to transfer them to your home directory (since you have no permissions to write to /). If path to your home is something like /home/erez try the following:

scp My_file.txt user_id@server:/home/erez/

You can substitute the path to your home directory with the shortcut ~/, so the following will have the same effect:

scp My_file.txt user_id@server:~/

You can even leave out the path altogether on the remote side; this means your home directory.

scp My_file.txt user_id@server:

That is, to copy the file to your desktop you might want to transfer it to /home/erez/Desktop/:

scp My_file.txt user_id@server:/home/erez/Desktop/

or using the shortcut:

scp My_file.txt user_id@server:~/Desktop/

or using a relative path on the remote side, which is interpreted relative to your home directory:

scp My_file.txt user_id@server:Desktop/

As @ckhan already mentioned, you also have to swap the arguments, it has to be

scp FROM TO

So if you want to copy the file My_file.txt from the server user_id@server to your desktop you should try the following:

scp user_id@server:/path/to/My_file.txt ~/Desktop/

If the file My_file.txt is located in your home directory on the server you may again use the shortcut:

scp user_id@server:~/My_file.txt ~/Desktop/
4
  • - I've tried: "~/" for 'Home' now, and also "~/Desktop/. Both resulted in : My_file.txt 100% 0 0.0 KB/s 00:00, and I cannot see the files in my folders. What am I still doing wrong ? Thanks a lot!
    – Erez
    Sep 12, 2012 at 9:31
  • On your second command example, the ~ should be protected from evaluation so as to be evaluated on the remote host: \~ for example.
    – dan
    Mar 19, 2015 at 12:21
  • 1
    In order to thank you, i registered to the website. Thanks you saved my day:) Jan 17, 2017 at 22:12
  • This helped me,and in hindsight, I had to slap myself abit. Thank you. Dec 16, 2021 at 3:59
26

What fixed the "permission denied" for me was, on the remote server, change the folder ownership to root: (This can happen when you are sending a file to a non-root user, and the directory is owned by root!) On the remote machine (copying dest.):

sudo chown (your username) (remote folder)

Also to be sure, enable all permissions on the remote folder: (Not always necessary):

sudo chmod 777 (remote folder)
3
  • 1
    This was my issue too, but it was a file deep in a subfolder that was the problem, so I had to do this recursively: sudo chown -R (your username) (remote folder) and sudo chmod -R 777 (remote folder)
    – Daniel
    Sep 2, 2021 at 13:24
  • 1
    Please note that on most systems, it is not considered a good idea to change the ownership of the / (root) filesystem node. While the root user (or processes running as such) will not be hampered by it, it will open the system to any number of accidental mishaps by a user mistaking the root directory for his home directory, including renaming or removing files needed for the continued operation of the system. @binfalse described the error and its solution in detail, adding good advice and instruction how to handle scp. Dec 15, 2021 at 19:26
  • I up-voted this answer as it worked for my situation. However, changing the permissions on the file to 777 should not be necessary. If you don't want to muck with the ownership of the file that you wish to transfer; cp the file to /tmp/, and chown the file there.
    – Justin
    Jul 1, 2022 at 11:17
14

I came here for "Transfer files using scp: permission denied" because I had the same error.

In my case, the file downloaded with scp would have overwritten a file owned by root, and I wasn't root. In short, check the ownership of the file being overwritten.

2
  • 2
    In this case it's because he's trying to write to /Home and not being root, he doesn't have write access outside ~/ Oct 9, 2012 at 9:25
  • 1
    You do help me. Mar 25, 2019 at 7:31
7

this seems to be like permissioning issue

The file might not have read permissions as it is delivered to the destination server as the source account.

you need to ensure the file at the source has required permissions especially read permission

-rw-r--r--

chmod 744

then scp the file to destination. This will be resolved

3
  • 4
    -rw-r--r-- is 644, not 744.  Setting executable permission on a file that is not actually executable can cause problems. Dec 10, 2015 at 0:11
  • @G-ManSays'ReinstateMonica' I found that I needed the receiver directory to have x permission for scp to work. e.g. chmod 700 myDir on receiver machine worked for me.
    – Lee
    Feb 14, 2023 at 10:53
  • Yes, of course, you always need to have execute permission to a directory to access / manipulate files in the directory. Feb 25, 2023 at 3:50
3

Permisssion Denied means you are not the root of the server. You just hold an account there. So in that case you need to do this:

sudo scp -r /path2yourFolder/ username@server_Ip:/home/username

This will copy to your home directory on server.

This will also work:

scp -r /path2yourFolder/ username@server_Ip:~/
3

As Jinzai pointed out, the remote folder can be owned by the root. In my case, someone (or something) changed the owner of the user's home folder to root. So, when I tried to send a file to a remote server, a permission denied error occurred.

You can check the owner of the folder with the ls -l command:

drwxr-xr-x 5 root root 4096 Oct 21 08:16 user2

In the example above, the user2 folder is owned by root. You can change the owner like this:

chown user2:user2 user2

There is also a faster way to check if the server is blocking a file by scp or not. Try to create a file in the target folder, for example with touch test_file. If you could not create a file in the folder, then you will not be able to send the file here remotely by the same user.

2

You have the arguments to scp reversed. It's source first, then destination, like cp. man scp for more details.

2

Amazon Linux AMI

Be sure that you need to set absolute server path, from home in current case:

scp file.txt SSH-ALIAS:/home/YOUR_USER/
0

The same permissions apply on both sides of the scp. You will get this error if the user lacks permission to write to the destination. The symptoms appear to implicate the source, however in my case the destination was created by root and lacked write permission for the intended user account.

0

In my case, I also tried to grant all the permission with -R to the folder but still got the error about permission.

I had to remove "/" before the path of the target server and folder. The reason is because Downloads is the first folder I can list out after I ssh to the server, for example: ssh [email protected]

This works:

sudo scp -rp /Users/macintosh.vn/Downloads/bbhs_20190301.zip [email protected]:Downloads

This was not working:

sudo scp -rp /Users/macintosh.vn/Downloads/bbhs_20190301.zip [email protected]:/Downloads

0

Easy fix. Make sure YOU are root. SCP to the remote and it will ask you for that machines root password. Just had this problem that was the solution. You root them root.

2
  • Since root logins are normally disabled in sshd_config, you should show the OP how to change this (although it's not recommended). Nov 21, 2019 at 20:17
  • 2
    Why do you insist on using root to copy a (any) file? Notice also the accepted answer from ~7 years ago...
    – Jeff Schaller
    Nov 21, 2019 at 20:33
0

You probably don't have permission to move the file to the location you've chosen. Instead of changing file ownership/permissions which may have unintended consequences, you need to perform the SCP file upload in two separate parts:

First SCP the file to your home directory:
sudo scp -i sshkey filename.zip [email protected]:~

Then move the file to the desired location:
sudo mv filename.zip /desired/path/

0

Check where your home folder is - pwd.

In my case, I had inconsistent home folders. On some of my hosts, my home folder was mounted at /home/myself, while on others, it was at /usr/local/home/myself.

-1

For anyone still want to copy files to the root folder, try login into root rather than an admin as below:

scp My_file.txt root@server:/somefolderinroot

scp -r My_folder root@server:/somefolderinroot
-2

Install a windows tool "mRemoteG" from www.mremoteng.org/

Create your SSH connection to the desired box.

Click on the "Transfer File" menu.

It will allow to transfer file easily from Windows to Linux.

thanks.

-4

before using scp command, make sure that you give permissions read, write and execute to everyone outside. "chmod 777 file_name"

1
  • you can copy file that are not 777
    – Archemar
    Mar 19, 2015 at 10:49

You must log in to answer this question.

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