Linux – scp to location on remote that requires sudo gives Permission denied error

linux

I have a ubuntu server to which I login as admin which can do sudo . On this server I have created dir /opt/myapp. I had to do sudo mkdir otherwise it would not work. Now from my desktop I want to do:

scp myfile admin@example.com:/opt/myapp

but I get Permission denied. But if I

scp myfile admin@example.com:

works fine and myfile is in /home/admin directory.

How to solve this problem?

Best Answer

First... maybe this is not a good idea. It may be that the permissions are set this way to stop people from doing exactly what you are trying to do for some good reason. That said, you are the captain of your destiny.

Find out who can write to that dir. Do:

ls -ld /opt/myapp

and you will get something like:

drwxr-xr-x 6 myapp_user myapp_group 55 Sep 10  2016 /opt/myapp

Potential Option 1: switch to the user account that is built for that app. eg:

scp myfile myapp_user@example.com:/opt/myapp

But you likely do not have the password for that user. Potentially, you can solve that by authenticating with a key. https://www.techrepublic.com/article/how-to-use-secure-copy-with-ssh-key-authentication/

Potential Option 2: Change the permissions of the dir '/opt/myapp' to 774 (or 770)

chmod 774 /opt/myapp

Then make the admin user a member of the group that now has write permissions. https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/ This one is not great as you may run into trouble with being blocked from over-writing files (depending on what is going on).

Related Question