Google cloud scp permission denied - linux

I am trying to transfer files to my Google cloud hosted Linux (Debian) instance via secure copy (scp). I did exactly what the documentation told to connect from a local machine to the instance. https://cloud.google.com/compute/docs/instances/connecting-to-instance.
Created a SSH keygen
Added the keygen to my instance
I can login successfully by:
ssh -i ~/.ssh/my-keygen [USERNAME]#[IP]
But when I want to copy files to the instance I get a message "permission denied".
scp -r -i ~/.ssh/my-keygen /path/to/directory/ [USERNAME]#[IP]:/var/www/html/
It looks like the user with which I login has no permissions to write files, so I already tried to change the file permissions of /var/www/, but this still gives the permission denied message.
I also tried to add the user to the root group, but this still gives the same problem.
usermod -G root myuser

The command line should be
scp -r -i ~/.ssh/my-keygen /path/to/directory/ [USERNAME]#[IP]:/var/www/html/
Assuming your files are in the local /path/to/directory/ and the /var/www/html/ is on the remote server.
The permissions does not allow to write in the /var/www/html/. Writing to /tmp/ should work. Then you can copy the files with sudo to the desired destination with root privileges.

If SSH isn't working, install gcloud CLI and run the following locally: gcloud compute scp --recurse /path/to/directory [IP] --tunnel-through-iap. This will dump the directory into your /home/[USERNAME]/ folder. Then log into the console and use sudo to move the directory to /var/www/html/.
For documentation, see https://cloud.google.com/sdk/gcloud/reference/compute/scp.

Related

How to properly upload a local file to a server using mobaXterm?

I'm trying to upload a file from my local desktop to a server and I'm using this command:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web
following the structure: scp filename user#ip:/remotePath.
But I get "Permission Denied". I tried using sudo , but I get the same message. I'm being able to download from the server to my local machine, so I assume I have all permissions needed.
What can be wrong in that line of code?
In case your /desired/path on your destination machine has write access only for root, and if you have an account on your destination machine with sudo privileges (super user privileges by prefixing a sudo to your command), you could also do it the following way:
Option 1 based on scp:
copy the file to a location on your destination machine where you have write access like /tmp:
scp file user#destinationMachine:/tmp
Login to your destination machine with:
ssh user#destinationMachine
Move the file to your /desired/path with:
sudo mv /tmp/file /desired/path
In case you have a passwordless sudo setup you could also combine step 2. and 3. to
ssh user#destination sudo mv /tmp/file /desired/path
Option 2 based on rsync
Another maybe even simpler option would be to use rsync:
rsync -e "ssh -tt" --rsync-path="sudo rsync" file user#destinationMachine:/desired/path
with -e "ssh -tt" added to run sudo without having a tty.
Try and specify the full destination path:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web/myFile.txt
Of course, double-check cooluser has the right to write (not just read) in that folder: 755, not 644 for the web parent folder.

SCP not working permission denied even with SSH key given

I am trying to get scp to work and transfer a file from a remote server to my local. I tried looking around and this post helped the most but it still is not working here is the current output.
<HOSTNAME>:chef4-rep
<USERNAME>$ sudo scp -i ./.chef/<NAME>.pem <USERNAME>#<IP>:/home/postgres/post_0604_dump/db0604_schema_and_data.sql ~/
<USERNAME>#<IP>: Permission denied (publickey).
The issue turned out not to be with my command but that I was trying to copy a file in another users directory and it wouldn't work. I ended up SSH'ing in and using sudo to copy the file to my home directory and then used scp with no issues.
Kindly use below command to get it done.
root#localhost# scp -r "source_file/directory" "Destination address- remote-IP/Hostname:/location"
And if you are using passwordless ssh then make sure you are using correct user whose public keys are shared with remote server.
Thanks
I had the same issue with scp and got Permission denied (publickey):
This worked:
ssh -i "mykey.pem" ubuntu#??.??.??.???
But this didn't: scp -i "mykey.pem" test.php ubuntu#??.??.??.???:
I solved it by removing the quotes off my key file:
scp -i mykey.pem test.php ubuntu#??.??.??.???:

rsync over ssh, gets permission denied

I gonna backup some files from a local machine to a remote server trough rsync over ssh. Here is the command I used:
rsync -avh -e "ssh -p 3286" somelocal_folder/ me#x.x.x.x:/data/bu/
The user(me) which I use to ssh to the server has rwx permissions on /data/bu directory but it does not have write permission on /data. It raises permission denied error when I enter my password. I searched a lot in the Internet,but I could not find any thing relevant. Any idea of the cause of the problem?!!

Changing user to root when connected to a linux server and copying files

My script is coded in a way that doesn't allow you to connect to a server directly by root. This code basically copies files from a server to my computer and it works but I don't have access to many files because only root can access them. How can I connect to a server as a user and then copy its files by switching to root?
Code I want to change:
sshpass -p "password" scp -q -r username#74.11.11.11:some_directory copy_it/here/
In other words, I want to be able to remotely copy files which are only accessible to root on a remote server, but don't wish to access the remote server via ssh/scp directly as root.
Is it possible through only ssh and not sshpass?
If I understand your question correctly, you want to be able to remotely copy files which are only accessible to root on the remote machine, but you don't wish to (or can't) access the remote machine via ssh/scp directly as root. And a separate question is whether it could be done without sshpass.
(Please understand that the solutions I suggest below have various security implications and you should weigh up the benefits versus potential consequences before deploying them. I can't know your specific usage scenario to tell you if these are a good idea or not.)
When you ssh/scp as a user, you don't have access to the files which are only accessible to root, so you can't copy all of them. So you need to instead "switch to root" once connected in order to copy the files.
"Switching to root" for a command is accomplished by prefixing it with sudo, so the approach would be to remotely execute commands which copy the files via sudo to /tmp on the remote machine, changes their owner to the connected user, and then remotely copy them from /tmp:
ssh username#74.11.11.11 "sudo cp -R some_directory /tmp"
ssh username#74.11.11.11 "sudo chown -R username:username /tmp/some_directory"
scp -q -r username#74.11.11.11:/tmp/some_directory copy_it/here/
ssh username#74.11.11.11 "rm -r /tmp/some_directory"
However, sudo prompts for the user's password, so you'll get a "sudo: no tty present and no askpass program specified" error if you try this. So you need to edit /etc/sudoers on the remote machine to authorize the user to use sudo for the needed commands without a password. Add these lines:
username ALL=NOPASSWD: /bin/cp
username ALL=NOPASSWD: /bin/chown
(Or, if you're cool with the user being able to execute any command via sudo without being prompted for password, you could instead use:)
username ALL=NOPASSWD: ALL
Now the above commands will work and you'll be able to copy your files.
As for avoiding using sshpass, you could instead use a public/private key pair, in which a private key on the local machine unlocks a public key on the remote machine in order to authenticate the user, rather than a password.
To set this up, on your local machine, type ssh-keygen. Accept the default file (/home/username/.ssh/id_rsa). Use an empty passphrase. Then append the file /home/username/.ssh/id_rsa.pub on the local machine to /home/username/.ssh/authorized_keys on the remote machine:
cat /home/username/.ssh/id_rsa.pub | ssh username#74.11.11.11 \
"mkdir -m 0700 -p .ssh && cat - >> .ssh/authorized_keys && \
chmod 0600 .ssh/authorized_keys"
Once you've done this, you'll be able to use ssh or scp from the local machine without password authorization.

Picloud scp with rsa file not working

I am using picloud which is a high performance cloud service run on top of amazon ec2. I am trying to copy files into a newly created "environment" in my account. I am however unable to use the scp command to copy files from my local machine into picloud env that I have created.
The usual way to SSH into the picloud env is as follows:
ssh -i picloud_rsa picloud#ec2-54-242-89-28.compute-1.amazonaws.com
But when I try to replace ssh with scp using the following format:
scp -r ~/path_to_the_directory -i picloud_rsa picloud#ec2-54-242-89-28.compute-1.amazonaws.com
I get the following error:
cp: -i: No such file or directory
cp: picloud#ec2-54-242-89-28.compute-1.amazonaws.com/picloud_rsa: Permission
And If I try the following:
scp -r ~/Desktop/AllFolders/GMU/Fall\ 2013/yelp_phoenix_academic_dataset_duplicated/ picloud_rsa picloud#ec2-54-242-89-28.compute-1.amazonaws.com
I get just the permission denied error:
cp: picloud#ec2-54-242-89-28.compute-1.amazonaws.com/picloud_rsa: Permission denied
I have absolutely no idea how to use scp in this case and would really appreciate any help.
Thanks in advance!
The query I used was:
scp -i picloud_rsa -r ~/my_path_to_directory picloud#ec2-54-242-89-28.compute-1.amazonaws.com:/home/picloud
The solution was to pass the -i flag along with the rsa file in the beginning right after the scp call.

Resources