How to fix "SCP Permission Denied" error on Linux

Estimated reading: 4 minutes 318 views

SCP is an open SSH file transfer utility that you can use on Linux and other supported operating systems to transfer files over SSH. It can be used to transfer files between two hosts using an SSH connection.

The SCP ‘permission denied’ error can occur due to a variety of reasons, such as incorrectly specified paths or a writing permission error. We will explore some of the reasons as to why this error occurs and how to resolve it

Cause: Not having write permissions

One reason this error occurs is that your user does not have write permissions for the specific directory on the host where you are trying to write files. To check these permissions, you can use the ‘ls -l’ command, which will display the write permissions.

Look for a ‘w’ in the first four alphabets in the first column of the output. If you see one, it means your user has write permission for that directory. Alternatively, you can always create a file using the touch command in that directory. If you receive no errors, such as ‘permission denied,’ then it means your user has write permissions.

Let’s say you want to copy a file to a remote host using SCP, and you get permission denied because of write permissions.

scp file user@IPaddress:/file path

Solution 1: Use the root account to transfer files

To fix such an error, you need to grant write permissions to your user for that directory. For that, you need root privileges. Another thing you can do is to copy the file to a directory where your user has write permissions.

You can grant permissions write to that file using the chmod command.

chmod 644 /file-or-folder-path

For example:

chmod 644 test.txt

In such a scenario, you either need to allow your current user the write permissions on /home/ or you can use the root user account to copy that file.

To allow the root user to copy a file using scp, you need to allow root login on ssh which is not recommended for security reasons but you can use it anyway in such scenarios and disable it afterwards.

To do that, open the openssh config at /etc/ssh/sshd_config and uncomment PermitRootLogin in a text editor in the remote host and add yes after that

nano /etc/ssh/sshd_config

Afterward, connect to the remote host using its root account.

Then run the command like this:

scp file user@IPaddress:/file path

For example:

Check back on the other end you will see the file test.txt copied successfully to /home

Solution 2: Add your user to the write permissions group

To have a user write on a directory/file in linux, you either need to add a user to the group that owns the directory or file or make the file owned by that user.

In our example, we run ls -l on the /home/ directory on the remote system to see which group owns /home/test.txt

The ls -l command will give you an output, where the contents of the 4th column are the group that owns a corresponding directory/file.

In our example, it’s the root user group that owns the directory /home/text.txt then we can

Simply run this command on the system.

chown <user_name> <file_name>
chown :<group> <file_name>

Then rerun the command again, which in our case would be:

scp file user@IPaddress:/file path

For example:

scp test.txt Green@IPaddress:/home

Solution 3: Use the user that owns that directory to copy the file

The simplest solution of all is to use the user which owns that directory to copy the file to it.

Simply run ls -l on the directory. In our case it would be:

ls -l /home/

The output of the third column will be the user that owns that directory corresponding to the name of the directory you are interested in. In our case, Green owns the directory /home/Cloud as seen in the screenshot above.

Then use that particular account while copying the file using scp to the remote host.

In our case, we run the command as follow:

scp cloud.txt Green@IPaddress:/home/Cloud/Cloud.txt

Check back on the other end you will see the file cloud.txt copied successfully to /home/Cloud/

Conclusion

In this article, we have guided you through the cases to fix the error “scp permission denied” on Linux. Good luck!

Share this Doc

How to fix "SCP Permission Denied" error on Linux

Or copy link

CONTENTS